ESP32 OLED屏幕

作者:追风剑情 发布于:2026-5-27 21:50 分类:物联网

111111.png

接线
OLED 模块 ESP32 引脚 说明
GND GND 共地
VCC 3.3V 模块供电,注意不要接到5V,以免损坏
SCL GPIO 22 I2C 时钟线
SDA GPIO 21 I2C 数据线

小贴士:某些开发板或模块可能将SCL和SDA引脚定义在不同的位置,但最通用的组合就是GPIO 21 (SDA) 和 GPIO 22 (SCL)。

安装驱动
idf.py add-dependency "chill-sam/ssd1306^1.1.0"


oled_control.h

#ifndef OLED_CONTROL_H
#define OLED_CONTROL_H

#include <stdbool.h>

// 初始化 OLED 屏幕(内部完成 I2C 总线初始化)
void oled_init(void);

// 按行显示文本(行号 0-7,每行最多 16 个英文字符)
void oled_show_text(int line, const char *text);

// 清屏
void oled_clear(void);

// 在指定像素坐标显示字符串(x: 0-127, y: 0-63)
void oled_draw_string(int x, int y, const char *text);

// 显示温湿度数据(专用接口,自动清除旧内容并刷新)
void oled_display_temp_humid(float temperature, float humidity);

#endif


oled_control.c

#include "oled_control.h"
#include <string.h>
#include <driver/i2c_master.h>
#include <ssd1306.h>
#include <esp_log.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>

// I2C 引脚配置
#define I2C_PORT        I2C_NUM_0
#define I2C_SDA_PIN     GPIO_NUM_21
#define I2C_SCL_PIN     GPIO_NUM_22
#define I2C_CLK_SPEED   400000          // 400 kHz

// OLED 显示参数
#define OLED_WIDTH      128
#define OLED_HEIGHT     64
#define OLED_I2C_ADDR   0x3C            // 常用地址

// 全局资源
static const char *TAG = "OLED";
static i2c_master_bus_handle_t i2c_bus = NULL;
static ssd1306_handle_t oled_handle = NULL;
static bool oled_ready = false;

// 初始化 I2C 总线(新版驱动)
static bool init_i2c_bus(void)
{
    i2c_master_bus_config_t bus_cfg = {
        .i2c_port = I2C_PORT,
        .sda_io_num = I2C_SDA_PIN,
        .scl_io_num = I2C_SCL_PIN,
        .clk_source = I2C_CLK_SRC_DEFAULT,
        .glitch_ignore_cnt = 7,
        .flags.enable_internal_pullup = true,
    };
    esp_err_t ret = i2c_new_master_bus(&bus_cfg, &i2c_bus);
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "Failed to create I2C bus: %s", esp_err_to_name(ret));
        return false;
    }
    ESP_LOGI(TAG, "I2C master bus created (SDA=%d, SCL=%d)", I2C_SDA_PIN, I2C_SCL_PIN);
    return true;
}

// 初始化 OLED 设备(使用新版驱动)
static bool init_oled_device(void)
{
    ssd1306_config_t cfg = {
        .bus = SSD1306_I2C,
        .width = OLED_WIDTH,
        .height = OLED_HEIGHT,
        .fb = NULL,               // 让驱动自动分配帧缓冲
        .fb_len = 0,
        .iface.i2c = {
            .port = I2C_PORT,
            .addr = OLED_I2C_ADDR,
            .rst_gpio = GPIO_NUM_NC,   // 无复位引脚
        },
    };
    esp_err_t ret = ssd1306_new_i2c(&cfg, &oled_handle);
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "Failed to create OLED device: %s", esp_err_to_name(ret));
        return false;
    }
    ESP_LOGI(TAG, "OLED device created (I2C addr=0x%02X)", OLED_I2C_ADDR);
    return true;
}

void oled_init(void)
{
    ESP_LOGI(TAG, "Initializing OLED...");
    
    if (!init_i2c_bus()) {
        oled_ready = false;
        return;
    }
    
    if (!init_oled_device()) {
        oled_ready = false;
        return;
    }
    
    // 清屏并刷新
    ssd1306_clear(oled_handle);
    ssd1306_display(oled_handle);
    
    oled_ready = true;
    ESP_LOGI(TAG, "OLED ready");
}

void oled_show_text(int line, const char *text)
{
    if (!oled_ready || oled_handle == NULL) {
        ESP_LOGW(TAG, "OLED not ready, cannot show text");
        return;
    }
    
    // 每行高度按 8 像素计算(适合默认字体)
    int y = line * 8;
    // 可选:清除该行区域(绘制黑色矩形)
    ssd1306_draw_rect(oled_handle, 0, y, OLED_WIDTH, 8, true);
    // 绘制文本(白色)
    ssd1306_draw_text(oled_handle, 0, y, text, true);
    // 刷新显示
    ssd1306_display(oled_handle);
}

void oled_clear(void)
{
    if (!oled_ready || oled_handle == NULL) return;
    ssd1306_clear(oled_handle);
    ssd1306_display(oled_handle);
}

void oled_draw_string(int x, int y, const char *text)
{
    if (!oled_ready || oled_handle == NULL) return;
    ssd1306_draw_text(oled_handle, x, y, text, true);
    ssd1306_display(oled_handle);
}

void oled_display_temp_humid(float temperature, float humidity)
{
    if (!oled_ready || oled_handle == NULL) {
        ESP_LOGE(TAG, "OLED not ready, cannot display data");
        return;
    }
    
    char buffer[32];
    
    // 清屏
    ssd1306_clear(oled_handle);
    
    // 标题(居中显示,需要计算起始X坐标,字体宽度约6像素,标题长度16字符)
    // 简单起见,从 x=16 开始显示
    ssd1306_draw_text(oled_handle, 16, 0, "Environment Data", true);
    
    // 温度
    snprintf(buffer, sizeof(buffer), "Temp: %.1f C", temperature);
    ssd1306_draw_text(oled_handle, 0, 24, buffer, true);
    
    // 湿度
    snprintf(buffer, sizeof(buffer), "Humidity: %.1f %%", humidity);
    ssd1306_draw_text(oled_handle, 0, 40, buffer, true);
    
    // 更新时间戳(底部)
    ssd1306_draw_text(oled_handle, 0, 56, "Updated", true);
    
    // 刷新显示
    ssd1306_display(oled_handle);
    
    ESP_LOGD(TAG, "Display updated: %.1f°C / %.1f%%", temperature, humidity);
}


main.c

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "led_control.h"
#include "servo_control.h"
#include "wifi_soft_ap.h"
#include "tcp_server.h"
#include "oled_control.h"

#define TCP_PORT 8888

static const char *TAG = "MAIN";

void app_main(void)
{
    // 1. 初始化 OLED(最先显示启动信息)
    oled_init();
    
    // 显示 IP 地址
    char ip_msg[32];
    snprintf(ip_msg, sizeof(ip_msg), "IP:%s", wifi_get_ip_str());
    oled_show_text(6, ip_msg);
    oled_show_text(7, "TCP:8888");
    
    while (1) {
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}


标签: IoT

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号