ESP32 + OLED显示屏实现信息滚动展示

2025-06发布5次浏览

在物联网和嵌入式开发中,ESP32 和 OLED 显示屏的结合是非常常见的应用场景之一。本文将详细介绍如何使用 ESP32 和 OLED 显示屏实现信息滚动展示功能。我们将从硬件连接、软件配置以及代码实现等多个方面进行深入解析。


一、硬件准备与连接

硬件清单

  1. ESP32 开发板:作为主控芯片。
  2. OLED 显示屏:通常为 SSD1306 或 SH1106 驱动的 0.96 英寸 I2C 接口显示屏。
  3. 杜邦线:用于连接 ESP32 和 OLED 显示屏。
  4. 面包板(可选):用于简化电路搭建。

硬件连接

ESP32 和 OLED 显示屏通过 I2C 协议通信,具体的引脚连接如下:

  • SCL:ESP32 的 GPIO22 连接到 OLED 的 SCL 引脚。
  • SDA:ESP32 的 GPIO21 连接到 OLED 的 SDA 引脚。
  • VCC:ESP32 的 3.3V 输出连接到 OLED 的 VCC 引脚。
  • GND:ESP32 的 GND 连接到 OLED 的 GND 引脚。

确保连接正确后,可以开始软件部分的配置。


二、软件环境配置

开发工具

  1. Arduino IDE:推荐使用 Arduino IDE 来编写和上传代码。
  2. ESP32 开发板支持:在 Arduino IDE 中添加 ESP32 支持。
    • 打开 Arduino IDE,进入 文件 -> 首选项,在“附加开发板管理器网址”中添加以下链接:
      https://dl.espressif.com/dl/package_esp32_index.json
      
    • 然后进入 工具 -> 开发板 -> 开发板管理器,搜索并安装 ESP32

必要库

  • Wire 库:用于 I2C 通信,Arduino IDE 默认包含。
  • Adafruit_GFX 和 Adafruit_SSD1306 库:用于驱动 OLED 显示屏。
    • 安装方法:进入 工具 -> 管理库,搜索并安装上述两个库。

三、代码实现

以下是实现信息滚动展示的核心代码:

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

String message = "Welcome to the world of IoT with ESP32 and OLED!";
int x = 0; // Starting position of the text on the screen
int y = 0; // Vertical position of the text

void setup() {
  Serial.begin(115200);
  
  // Initialize the OLED display
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for most OLEDs
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }
  display.clearDisplay();
  display.setTextSize(1);      // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE); // Set text color to white
  display.setCursor(0, 0);    // Start at top-left corner
  display.display();
}

void loop() {
  display.clearDisplay(); // Clear the display buffer

  // Draw the scrolling text
  display.setCursor(x, y);
  display.println(message);

  // Update the position of the text
  x--;
  if (x < -message.length() * 6) { // Reset position when text goes off-screen
    x = SCREEN_WIDTH;
  }

  display.display(); // Send the buffer to the display
  delay(100);       // Adjust speed of scrolling
}

四、代码解析

  1. 初始化 OLED 显示屏

    • 使用 display.begin() 方法初始化 OLED 显示屏,指定 I2C 地址(通常为 0x3C)。
    • 设置文本大小、颜色及初始位置。
  2. 滚动逻辑

    • 每次循环中,通过 display.setCursor(x, y) 设置文本的起始位置。
    • x 坐标逐渐减小以实现向左滚动的效果。
    • 当文本完全移出屏幕时(x < -message.length() * 6),重置 x 坐标到屏幕右侧。
  3. 刷新显示

    • 调用 display.display() 方法将缓冲区内容更新到 OLED 屏幕上。
    • 使用 delay() 控制滚动速度。

五、扩展讨论

1. 动态信息展示

可以通过 WiFi 或蓝牙模块让 ESP32 获取动态信息(如天气、时间等),并在 OLED 上滚动展示。例如:

  • 使用 NTP 协议获取当前时间。
  • 调用 OpenWeatherMap API 获取天气数据。

2. 多行滚动

如果需要在多行上实现滚动效果,可以通过调整 y 坐标实现多行文字的滚动。

3. 流程图表示

以下是实现滚动展示的主要流程图:

flowchart TD
    A[初始化] --> B[设置显示屏参数]
    B --> C[清空屏幕]
    C --> D{是否需要滚动}
    D --是--> E[绘制文本]
    E --> F[更新坐标]
    F --> G[刷新屏幕]
    G --> H[延时]
    H --> D
    D --否--> I[结束]