基于Arduino的ESP32-S3 + 1.3寸OLED(4pin)

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

随着物联网技术的快速发展,OLED显示屏凭借其高对比度、低功耗等优势,在嵌入式开发中应用广泛。本文将以ESP32-S3-DevKitC-1开发板与1.3寸OLED屏(4pin,SH1306 )为例,详解从硬件连接到代码实现的完整驱动方案。相比常见的0.96寸SH1106方案,1.3寸版本在可视面积增大30%的同时保持相同分辨率(128x64),更适合显示更多的信息。

一、硬件准备

1.1 硬件对比

在这里插入图片描述

1.2 硬件连接方案

接线示意:
GND-------------GND
VCC-------------3.3V / 5V
SCL--------------21
SDA--------------47

二、驱动实现

2.1 开发环境配置

添加库文件
esp8266-oled-ssd1306-master.zip

对应调用的头文件:

#include <Wire.h>
#include <SH1106Wire.h>

2.2 引脚定义

代码如下(示例):

#define SDA 47  // GPIO47
#define SCL 21  // GPIO21

2.3 I2C地址扫描

注:
参考淘宝店铺risym

1.3寸:0X3C / 0X3D
0.96寸:0X78 / 0X7A (0.96寸未使用,未验证)

通过地址扫描代码验证设备连接:
代码如下(示例):

#include <Wire.h>
#define SDA_PIN 47
#define SCL_PIN 21

void setup() {
  Serial.begin(9600);
  Wire.begin(SDA_PIN, SCL_PIN);
}

void loop() {
  scanI2CDevices();
  delay(5000);
}

// I2C扫描函数(支持0x01-0x7F范围)
void scanI2CDevices() {
  byte error, address;
  int foundDevices = 0;
  
  for(address = 1; address <= 0x7F; address++) {
    Wire.beginTransmission(address);
    error = Wire.endTransmission();
    if (error == 0) {
      Serial.printf("发现设备 → 0x%02X\n", address);
      foundDevices++;
    }
  }
  Serial.printf("共发现%d个设备\n", foundDevices);
}

三、完整驱动代码

代码如下(示例):

#include <Arduino.h>
#include <SH1106Wire.h>
 
#define SDA 47
#define SCL 21
SH1106Wire display(0x3c,SDA,SCL);
 
 
float temp; 


void oledbegin();
void displaysensor() ;
void displayTemp();
 
void setup() {
  Serial.begin(9600);
  oledbegin();
}
 
void loop() {
  // put your main code here, to run repeatedly:
 displaysensor() ;
 displayTemp();
 
}


void oledbegin() {
// OLED显示屏初始化检测
if(!display.init()) {                    // 尝试初始化OLED显示屏[1,5](@ref)
  Serial.println("OLED init failed");    // 初始化失败时通过串口输出错误信息
  while (1) delay(100);                 // 进入死循环阻止程序继续执行[1](@ref)
} 
// 初始化成功后执行以下设置
display.flipScreenVertically();          // 将屏幕显示内容垂直翻转(0→180度)[5](@ref)
display.clear();                         // 清空屏幕显示内容和显存缓冲区[4,7](@ref)
display.setFont(ArialMT_Plain_10);      // 设置显示字体为Arial 10号等宽字体[7](@ref)
}

void displaysensor() {
  display.drawString(0, 0, "Hello World");
  display.drawString(0, 10, "oled");
  display.drawString(0, 20, "电磁阀");
  display.display();
}

void displayTemp() {
  temp = 0.0 ;
  display.drawString(0, 20, String(temp)+"℃"); 
  display.display();
}

注: 中文无法正常显示,后文补充
在这里插入图片描述

关键函数解析

display.init();               初始化屏幕并检测通信状态,返回false时需检查I2C连接
flipScreenVertically();       实现180°屏幕旋转,适配不同安装方向
display.setFont();            支持ArialMT_Plain_10/16/24 三种字体尺寸

补充中文库

见我的下篇博客
基于Arduino的ESP32-S3 + OLED(4pin)的文字取模

参考来源

B站 普中科技
OLED液晶显示实验

CSDN博主 thief_1
ESP32-S3+1.3寸OLED+SH1106

总结

本文实现了ESP32-S3对1.3寸OLED屏的完整驱动方案,相比传统0.96寸方案,更大的显示面积。
开发过程中需特别注意GPIO冲突问题(如SD卡模块与I2C引脚复用),建议通过I2C扫描程序先行验证硬件连接。后续可扩展实现动态图表绘制、多级菜单交互等高级功能。

1.3OLED全套资料 132 X 64 Dot Matrix OLED/PLED Preliminary Segment/Common Driver with Controller 1 V0.2 Features „ Support maximum 132 X 64 dot matrix panel „ Embedded 132 X 64 bits SRAM „ Operating voltage: - Logic voltage supply: VDD1 = 1.65V - 3.5V - DC-DC voltage supply: VDD2 = 3.0V – 4.2V - OLED Operating voltage supply: External VPP supply = 6.4V - 13.0V Internal VPP generator = 6.4V - 9.0V „ Maximum segment output current: 200μA „ Maximum common sink current: 27mA „ 8-bit 6800-series parallel interface, 8-bit 8080-series parallel interface, 3-wire & 4-wire serial peripheral interface, 400KHz fast I2C bus interface „ Programmable frame frequency and multiplexing ratio „ Row re-mapping and column re-mapping (ADC) „ Vertical scrolling „ On-chip oscillator „ Programmable Internal charge pump circuit output „ 256-step contrast control on monochrome passive OLED panel „ Low power consumption - Sleep mode: <5μA - VDD1=0V,VDD2=3.0V – 4.2V: <5μA - VDD1,2=0V,VPP=3.0V – 4.2V: <5μA „ Wide range of operating temperatures: -40 to +85°C „ Available in COG form, thickness: 300μm General Description SH1106 is a single-chip CMOS OLED/PLED driver with controller for organic/polymer light emitting diode dot-matrix graphic display system. SH1106 consists of 132 segments, 64 commons that can support a maximum display resolution of 132 X 64. It is designed for Common Cathode type OLED panel. SH1106 embeds with contrast control, display RAM oscillator and efficient DC-DC converter, which reduces the number of external components and power consumption. SH1106 is suitable for a wide range of compact portable applications, such as sub-display of mobile phone, calculator and MP3 player, etc.
### ESP32-S3 设备在小智配网过程中搜索不到的解决方案 当遇到ESP32-S3设备在小智配网过程中无法被发现的情况时,可以采取一系列措施来排查并解决问题。 #### 1. 检查硬件连接 确保所有物理连接稳固无误。特别是Wi-Fi模块与主板之间的连接应保持良好接触状态[^1]。任何松动都可能导致通信失败,进而影响到网络配置过程中的表现。 #### 2. 验证固件版本 确认所使用的固件是最新的稳定版。旧版本可能存在兼容性问题或是Bug,这些都会干扰正常的联网操作。可以通过官方渠道获取最新发布的固件文件,并按照说明文档完成升级工作。 #### 3. 调整无线环境设置 尝试改变路由器信道或减少附近其他电子产品的干扰源数量。有时候复杂的电磁环境下会影响信号传输质量,从而使得设备难以成功加入指定网络。 #### 4. 修改代码逻辑 如果上述方法均未能有效改善状况,则需考虑是否存在程序编写上的失误。以下是调整后的Python示例代码片段用于优化扫描功能: ```python import network def connect_wifi(ssid, password): wlan = network.WLAN(network.STA_IF) wlan.active(True) # 增加超时机制防止无限等待 timeout = 10 wlan.connect(ssid, password) while not wlan.isconnected() and timeout > 0: print(&#39;Waiting for connection...&#39;) time.sleep(1) timeout -= 1 if wlan.isconnected(): print(&#39;Connected to&#39;, ssid) print(&#39;Network config:&#39;, wlan.ifconfig()) else: print(&#39;Connection failed&#39;) ``` 此段脚本增加了对于连接请求响应时间的限制,避免因长时间未收到回复而导致进程卡死现象发生。 #### 5. 使用专用工具辅助诊断 利用专门设计用来调试IoT装置连通性的软件来进行更深入细致的问题定位分析。这类应用程序通常具备更强的数据捕捉能力和错误检测精度,有助于快速找出潜在隐患所在之处。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

承前智

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值