Help, multiple i2c buses esp32

Hello, I want to use an i2c oled screen through the GPIO32 and GPIO33 ports of the esp32 and leave the GPIO21 and GPIO22 ports free for other i2c connections, but I can't get the screen to show an image, this is my code:

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define OLED_RESET    -1
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define I2C_FREQ 400000
#define SDA_1 21
#define SCL_1 22
#define SDA_2 33
#define SCL_2 32
TwoWire I2C_1 = TwoWire(0);
TwoWire I2C_2 = TwoWire(1);


void setup() {

  I2C_1.begin(SDA_1, SCL_1, I2C_FREQ);
  I2C_2.begin(SDA_2, SCL_2, I2C_FREQ);

  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) 
  {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }

}
 
void loop() {

  display.clearDisplay(); 
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setCursor(0,0); 
  display.print("I2C Scan por Aaronfix");
  display.display(); 
}

Hello yhomer

Why?

You are using a I²C bus system to connect addtional I²C hardware already.

Have a nice day and enjoy coding in C++.

I'm not using any hardware, only the esp32, I want the esp32 to have two independent i2c connections, one for the screen and the other for an i2c scanner, can you tell me why not use a single i2c bus? The answer is that if I connect the screen to the same scanner bus creates conflicts for me that's why I want 2 i2c buses

That´s very simple.

Do it - check it !

Well, up to now I have not been able to get the screen to show an image working on the 2 buses, working on a single bus, it does not matter in which ports it manages to give an image

ESP32 is hardware. So is a screen.

What do you mean by that? Please post a link to the spec of this scanner. Please explain why this scanner needs to be on a different bus to the screen.

No, we cannot answer that. Can you tell us why you believe you cannot use a single bus?

What conflicts? The most common conflict with i²c bus is using multiple i²c devices that have the same address on the bus. Often, this can be overcome by changing the addresses so that they are different. But not all devices allow this to be done.

Please post your complete test code and a schematic showing how the circuit is connected in this situation.

Maybe this could be the problem:

With the screen, you are using the standard "Wire" object. But later you create 2 i2c objects, one of which may interfere with the standard Wire object.

Can you not use the standard "Wire1" object for your second i2c port, instead of creating the 2 new i2c ports?

ESP32 I2C Communication: Set Pins, Multiple Bus Interfaces and Peripherals | Random Nerd Tutorials.

Using TwoWire in the sketch is no longer required.
See my post here: https://forum.arduino.cc/t/using-twowire-i2c2-with-esp32-two-pressure-sensors/1061318/10

I2C Bus is a two-wire communication network that allows multiple devices to be connected in parallel; however, the condition is that 7-bit slaveAddress of a device must be different from that of other devices. Please,

1. Connect your OLED Display Unit alone with ESP32 Board and run the "Scanner Program/Sketch" of Step-5; note down the reported slaveAddress and post it.

2. Disconnect the OLED from I2C Bus.

3. Connect your Scanner Device alone with ESP32 Board and run the "Scanner Program/sketch' of Step-5; note down the reported slaveAddress and post it.

4. Check that the addresses of the OKED and Scanner devices are different and then connect both devices with the I2C Bus of ESP32 Board. Run the "Scanner Program/Sketch" of Step-5 and check that the slaveAddress of the devices are reported correctly.

5. Scanner Program/Sketch

#include<Wire.h>
byte busStatus;

void setup()
{
  Serial.begin(9600);
  Wire.begin(21, 22);  //SDA, SCK
  for (int i2cAddress = 0x00; i2cAddress < 0x80; i2cAddress++)
  {
    Wire.beginTransmission(i2cAddress);
    busStatus = Wire.endTransmission();
    if (busStatus == 0x00)
    {
      Serial.print("I2C Device found at address: 0x");
      Serial.println(i2cAddress, HEX);
    }

    else
    {
      Serial.print("I2C Device not found at address: 0x");
      Serial.println(i2cAddress, HEX);
    }
  }
}

void loop()
{

}

Output with my BMP280

I2C Device not found at address: 0x75
I2C Device found at address: 0x76
I2C Device not found at address: 0x77

Thanks for your support guys, I already managed to make my project work, I exchanged the connection order, the oled screen to the main i2c of the esp32 ports 21 and 22 and the i2c scanner to ports 32 and 33 and it started to work, I don't understand why the screen oled does not connect with wire1 layers its default library takes the main wire connection of esp32 but exchanging it worked

your example helped me a lot thank you

You would have certainly got the answer if you would have exercised the tasks of post #9!

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