Configuring Two I2C serial busses
Posted: Wed Apr 27, 2022 2:09 pm
I have a ESP32-C3-WROM-02 and have been trying to get two I2C buses one on Pins 0, 1 and the other on Pins 2, 3 (or 18,19) to work simultaneously.
I use the Arduino IDE and the Wire.h code. I get each bus to work but when I try to run them together I only get results from the first one. I used the following code and it returns the address of the slaves on IC2Cone (or Wire) only. It seems that I can not run two controllers at the same time. Is this correct?
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL343.h>
// extern TwoWire Wire1;
// extern TwoWire Wire;
//logMessage is my logger.. replace this by Serial.println or something..
//
TwoWire I2Cone = TwoWire(0);
TwoWire I2Ctwo = TwoWire(1);
void setup (){
uint32_t freq = 100000;
Serial.begin(115200);
Serial.println("test");
I2Cone.begin(0, 1, freq); // Wire.begin(0,1);
I2Ctwo.begin(18, 19, freq); // Wire1.begin(18,19)
Serial.println("I2C scanner. Scanning bus 0...");
for (byte i = 8; i < 127; i++)
{
I2Cone.beginTransmission(i);
if (I2Cone.endTransmission() == 0)
{
Serial.println("Found address: " + String(i));
// check address
if ((i == 32) or (i == 39))
{
// found device, do something..
}
delay(1);
}
}
I2Cone.endTransmission();
Serial.println("I2C scanner. Scanning bus 1...");
for (byte i = 8; i < 127; i++)
{
I2Ctwo.beginTransmission(i);
if (I2Ctwo.endTransmission() == 0)
{
Serial.println("Found address: " + String(i));
// check address
if (i == 64)
{
// found device, do something..
}
delay(1);
}
}
I2Ctwo.endTransmission();
Serial.println("I2C device check DONE!\n");
}
void loop() {
setup();
delay(10000);
}
I use the Arduino IDE and the Wire.h code. I get each bus to work but when I try to run them together I only get results from the first one. I used the following code and it returns the address of the slaves on IC2Cone (or Wire) only. It seems that I can not run two controllers at the same time. Is this correct?
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL343.h>
// extern TwoWire Wire1;
// extern TwoWire Wire;
//logMessage is my logger.. replace this by Serial.println or something..
//
TwoWire I2Cone = TwoWire(0);
TwoWire I2Ctwo = TwoWire(1);
void setup (){
uint32_t freq = 100000;
Serial.begin(115200);
Serial.println("test");
I2Cone.begin(0, 1, freq); // Wire.begin(0,1);
I2Ctwo.begin(18, 19, freq); // Wire1.begin(18,19)
Serial.println("I2C scanner. Scanning bus 0...");
for (byte i = 8; i < 127; i++)
{
I2Cone.beginTransmission(i);
if (I2Cone.endTransmission() == 0)
{
Serial.println("Found address: " + String(i));
// check address
if ((i == 32) or (i == 39))
{
// found device, do something..
}
delay(1);
}
}
I2Cone.endTransmission();
Serial.println("I2C scanner. Scanning bus 1...");
for (byte i = 8; i < 127; i++)
{
I2Ctwo.beginTransmission(i);
if (I2Ctwo.endTransmission() == 0)
{
Serial.println("Found address: " + String(i));
// check address
if (i == 64)
{
// found device, do something..
}
delay(1);
}
}
I2Ctwo.endTransmission();
Serial.println("I2C device check DONE!\n");
}
void loop() {
setup();
delay(10000);
}