範例十-兩組 software I2C 顯示兩片 SSD1306 OLED

此範例簡單說明如何用 ESP32 + Gyro 擴充板利用兩組 software I2C 顯示兩片 OLED

-> ESP32 有 software i2c 功能,所以讓相關的 gpio 設定變的很有彈性.


由於市面上的 SSD1306 OLED I2C 腳位排列方式很多種,可借用左右兩邊 L9110S的插座,再加上 software I2C 宣告好對應的 SCL/SDA 腳位就可以用了.

L9110 腳位接腳如附圖,供大家參考.

-> 接硬體設備時,請先確認模組的"正""負" 極是否正確,以免造成板子或模組損壞,使用時請特別注意

// 範例十九:

// 使用兩組 software I2C 顯示不同的中文字在兩片 SSD1306 OLED 上

// 多組 I2C 參考資料 https://randomnerdtutorials.com/esp32-i2c-communication-arduino-ide/?fbclid=IwAR1_q10gJbICC0lgNN2aSXnhd2TSyZ8B6mGJqDEKPLRm2AvF4rvy2Q7LxSI

// 需 library : U8g2lib

// FB : https://www.facebook.com/mason.chen.1420


#include "Wire.h"

#include "U8g2lib.h"


#define SDA1 32 // 請自行定義第一組用到的 I2C 腳位

#define SCL1 33


#define SDA2 27 // 請自行定義第二組用到的 I2C 腳位

#define SCL2 14


U8G2_SSD1306_128X64_NONAME_1_SW_I2C u8g2_1(U8G2_R0, /* clock=*/ SCL1, /* data=*/ SDA1, /* reset=*/ U8X8_PIN_NONE); // All Boards without Reset of the Display

U8G2_SSD1306_128X64_NONAME_1_SW_I2C u8g2_2(U8G2_R0, /* clock=*/ SCL2, /* data=*/ SDA2, /* reset=*/ U8X8_PIN_NONE); // All Boards without Reset of the Display


void setup() {

u8g2_1.begin();

u8g2_1.enableUTF8Print();

u8g2_1.setFont(u8g2_font_6x10_tf);

u8g2_1.setFontDirection(0);


u8g2_1.setDisplayRotation(U8G2_R3);


u8g2_2.begin();

u8g2_2.enableUTF8Print();

u8g2_2.setFont(u8g2_font_6x10_tf);

u8g2_2.setFontDirection(0);


u8g2_2.setDisplayRotation(U8G2_R1);


u8g2_1.setFont(u8g2_font_unifont_t_chinese1);

u8g2_1.firstPage();

do {

u8g2_1.setCursor(0, 26);

u8g2_1.print(String("新年快樂").c_str());

u8g2_1.setCursor(0, 82);

u8g2_1.print(String("Mason").c_str());

u8g2_1.sendBuffer();

} while (u8g2_1.nextPage());


u8g2_2.setFont(u8g2_font_unifont_t_chinese1);

u8g2_2.firstPage();

do {

u8g2_2.setCursor(0, 26);

u8g2_2.print(String("福虎生豐").c_str());

u8g2_2.setCursor(0, 82);

u8g2_2.print(String("2022-").c_str());

u8g2_2.setCursor(0, 98);

u8g2_2.print(String("02/11").c_str());

u8g2_2.sendBuffer();

} while (u8g2_2.nextPage());

}


void loop() {


}