0.96寸 OLED SSD1306 I2C版

元器件
显示屏
库存 800

介绍

0.96英寸OLED显示模块,驱动IC SSD1306,128×64分辨率,I2C接口,蓝色/黄蓝双色/白色可选,自发光低功耗,对比度极高,广泛用于嵌入式显示、可穿戴设备、小型仪表

规格参数

参数
供电3.3V-5V
功耗~20mA全屏点亮
尺寸0.96英寸
接口I2C (默认地址0x3C/0x3D)
驱动ICSSD1306
分辨率128×64
使用寿命>10000小时
像素尺寸0.154×0.154mm
可视角度>160°
响应时间≤10μs
外形尺寸27mm×27mm
工作温度-40°C~85°C
显示颜色蓝/黄蓝双色/白

代码例程

0.96寸OLED SSD1306 多平台代码例程.md

# 0.96寸OLED SSD1306 多平台代码例程

---

## 平台一:Arduino (U8g2库)

```cpp
// U8g2 SSD1306 I2C 128x64 完整示例
#include <Wire.h>
#include <U8g2lib.h>

// 构造函数: U8G2_SSD1306_128X64_NONAME_F_HW_I2C(rotation, reset)
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);

void setup() {
  Wire.begin();                // SDA=A4, SCL=A5 (Uno)
  // Wire.begin(21, 22);       // ESP32
  u8g2.begin();
  u8g2.enableUTF8Print();
}

void loop() {
  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_ncenB08_tr);
  u8g2.drawStr(0, 10, "Hello OLED!");
  u8g2.drawLine(0, 16, 127, 16);
  u8g2.setFont(u8g2_font_6x10_tf);
  u8g2.setCursor(0, 30);
  u8g2.print("Millis: ");
  u8g2.print(millis());
  u8g2.sendBuffer();
  delay(100);
}
```

---

## 平台二:ESP32 (Adafruit SSD1306)

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

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_ADDR 0x3C

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

void setup() {
  Wire.begin(21, 22);  // SDA=21, SCL=22
  display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  display.println(F("ESP32 + SSD1306"));
  display.display();
}

void loop() {
  display.clearDisplay();
  display.setCursor(0, 0);
  display.println("Vibe Electronics");
  display.drawRect(0, 16, 128, 48, SSD1306_WHITE);
  display.setCursor(4, 22);
  display.printf("Uptime: %lu s", millis() / 1000);
  display.display();
  delay(500);
}
```

---

## 平台三:STM32 (HAL库 + U8g2)

```c
// STM32 HAL库 SSD1306 底层驱动
#include "u8g2.h"
#include "i2c.h"

// SSD1306 I2C 发送字节回调
uint8_t u8x8_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
    static uint8_t buffer[32];
    static uint8_t buf_idx;
    switch(msg) {
        case U8X8_MSG_BYTE_SEND:
            while(arg_int--)
                buffer[buf_idx++] = ((uint8_t *)arg_ptr)[arg_int];
            HAL_I2C_Master_Transmit(&hi2c1, 0x3C << 1, buffer, buf_idx, 100);
            buf_idx = 0;
            break;
        case U8X8_MSG_BYTE_START_TRANSFER: buf_idx = 0; break;
        case U8X8_MSG_BYTE_END_TRANSFER:   break;
    }
    return 1;
}

// GPIO回调
uint8_t u8x8_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
    switch(msg) {
        case U8X8_MSG_GPIO_AND_DELAY_INIT: break;
        case U8X8_MSG_DELAY_MILLI: HAL_Delay(arg_int); break;
    }
    return 1;
}

u8g2_t u8g2;
void oled_init(void) {
    u8g2_Setup_ssd1306_i2c_128x64_noname_f(&u8g2, U8G2_R0, u8x8_byte_hw_i2c, u8x8_gpio_and_delay);
    u8g2_InitDisplay(&u8g2);
    u8g2_SetPowerSave(&u8g2, 0);
}

void oled_display_demo(void) {
    u8g2_ClearBuffer(&u8g2);
    u8g2_SetFont(&u8g2, u8g2_font_6x10_tf);
    u8g2_DrawStr(&u8g2, 0, 10, "STM32 + SSD1306");
    u8g2_DrawHLine(&u8g2, 0, 14, 128);
    u8g2_DrawStr(&u8g2, 4, 28, "HAL I2C OK");
    char buf[20];
    sprintf(buf, "Tick: %lu", HAL_GetTick());
    u8g2_DrawStr(&u8g2, 4, 42, buf);
    u8g2_SendBuffer(&u8g2);
}
```

---

## 平台四:MicroPython (ESP32/RP2040)

```python
# SSD1306 MicroPython 驱动 (ssd1306.py)
from machine import Pin, I2C
import ssd1306
import time

# ESP32 初始化
i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=400000)
oled = ssd1306.SSD1306_I2C(128, 64, i2c)

# 显示文本
oled.fill(0)
oled.text("MicroPython", 0, 0, 1)
oled.text("SSD1306 OLED", 0, 10, 1)
oled.hline(0, 22, 128, 1)
oled.text("Vibe Electronics", 0, 30, 1)
oled.show()

# 动画示例
import framebuf
fbuf = framebuf.FrameBuffer(bytearray(1024), 32, 32, framebuf.MONO_VLSB)
while True:
    for x in range(96):
        oled.fill(0)
        oled.text("Scrolling...", x - 96, 10, 1)
        oled.fill_rect(x, 40, 32, 24, 1)
        oled.show()
        time.sleep_ms(20)
```

参考资料

暂无参考文献