Esp32 S3 and two I2C buses with Adafruit SSD1306 library (Two OLED 128x64 displays)

I finally got it to work. I couldn't find working example from net and this took hours to figure out even when the solution looks really simple. Both OLEDs have the same address.

I tried all the TwoWire examples, but got nowhere with it.

(Code is just copy-pasted together from my larger set, but it has all the important parts.)

// OLED
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

//Define pins
#define I2C_A_SDA 8 // GPIO8
#define I2C_A_SCL 9 // GPIO9

#define I2C_B_SDA 17 // GPIO17
#define I2C_B_SCL 18 // GPIO18

// OLED parameters
#define SCREEN_WIDTH 128     // OLED display width, in pixels
#define SCREEN_HEIGHT 64     // OLED display height, in pixels
#define OLED_RESET -1        // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C  // Change if required
#define ROTATION 0           // Rotates text on OLED 1=90 degrees, 2=180 degrees

// Define display object
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_SSD1306 display2(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire1, OLED_RESET);

void setup() {
  Serial.begin(115200);
  while (!Serial)
    ;
   Wire.begin(I2C_A_SDA, I2C_A_SCL);
   Wire1.begin(I2C_B_SDA, I2C_B_SCL);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("Display1 SSD1306 allocation failed"));
    for (;;)
      ;  // Don't proceed, loop forever
  }
  
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if (!display2.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("Display2 SSD1306 allocation failed"));
    for (;;)
      ;  // Don't proceed, loop forever
  }

// Show splash screen
  display.display();
  display2.display();
  delay(2000); // Pause for 2 seconds

  // Display settings
  display.clearDisplay();
  display.setTextSize(2);             // Normal 1:1 pixel scale
  display.setTextColor(WHITE);        // Draw white text
  display.setCursor(0,0);             // Start at top-left corner
  display.setRotation(ROTATION);      // Set screen rotation

  // Display settings
  display2.clearDisplay();
  display2.setTextSize(2);             // Normal 1:1 pixel scale
  display2.setTextColor(WHITE);        // Draw white text
  display2.setCursor(0,0);             // Start at top-left corner
  display2.setRotation(ROTATION);      // Set screen rotation

  display.print("Display1 ");
  display2.print("Display2 ");

  display.display();
  display2.display();

}


void loop() {
}

2 Likes

Welcome to the forum and thank you for the example.

That is indeed the right way to do it :smiley: Use the already defined "Wire" and "Wire1" and initialize both displays.
Some people try to add a bus with "TwoWire" or "HardwareSerial" things, but those people are years behind.

I tried to put your sketch in the Wokwi simulation, but the Wokwi servers are too busy at this moment to compile it:

On some processors, the SDA and SCL needs to be in pairs, starting at an even pin. Instead of using 17 and 18 for Wire1, 16 and 17 can be used. But it does not matter for the ESP32-S3.

Some ESP32-S3 boards have the pin numbers written below the pin. Isn't that confusing for you ?

Hi

Thank you for your positive comments! I was just so happy, when I got this to work :laughing:

I'm quite new to whole arduino&ESP32 scene, only played with it couple of years. I have to try that wokwi.com thingie...

S3 chip is using GPIO Matrix, so you can assign most IO's to almost anything. Espressif changed GPIO names on S3 comparing to original ESP32 and now its very confusing.

I looked up long time proper pinout schematics and found these:

http://wiki.fluidnc.com/en/hardware/ESP32-S3_Pin_Reference

It compiles now:

You can change the source code at Wokwi and try it, without even being logged in.
If you want to save a project, then you should get a free account to save it to your projects.

1 Like

Hi
Thanks exactly what i try to use
Regards Doron

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.