Please fill the info fields, it helps to get you faster support ;)
If you have a Guru Meditation Error, please decode it:
https://github.com/me-no-dev/EspExceptionDecoder
----------------------------- Remove above -----------------------------
Board: ?ESP32 Dev Module?
Core Installation/update date: ?11/jul/2017?
IDE name: ?Arduino IDE? ?Platform.io? ?IDF component?
Flash Frequency: ?40Mhz?
Upload Speed: ?115200?
Hello.
I am doing a test program in Arduino for ESP32 in which I would like to use the two I2C buses that I have, separately I can read and write data, but what I can not do in my program is being able to change from one I2C bus to another to send or receive data, I have used the wire.h library that comes for it in Arduino.
Greetings BlackTiger44.
You might define two Wire ports, and then access them as needed by their name.
TwoWire I2Cone = TwoWire(0);
TwoWire I2Ctwo = TwoWire(1);
void setup() {
I2Cone.begin(21,22,100000); // SDA pin 21, SCL pin 22, 100kHz frequency
I2Ctwo.begin(16,17,400000); // SDA pin 16, SCL pin 17, 400kHz frequency
}
void loop() {
I2Cone.beginTransmission(0x38);
I2Cone.write(0x00 | 0x80);
I2Cone.write(0x03);
I2Cone.endTransmission();
I2Ctwo.beginTransmission(0x42);
I2Ctwo.write(140);
I2Ctwo.endTransmission();
I2Ctwo.requestFrom(0x42,1);
if (I2Ctwo.available() == 1) {
byte value = I2Ctwo.read();
}
}
Code is not tested, but that is how I would approach it.
Have you tried running each bus in its own task? If that doesn't work you might want to look into bitbanging the buses yourself, it's not as hard as it might at first seem.
From: Bernd Giesecke notifications@github.com
Sent: Sunday, January 7, 2018 11:18:36 PM
To: espressif/arduino-esp32
Cc: Subscribed
Subject: Re: [espressif/arduino-esp32] Simultaneously use the two I2C bus of the ESP32. (#977)
You might define two Wire ports, and then access them as needed by their name.
TwoWire I2Cone = TwoWire(0);
TwoWire I2Ctwo = TwoWire(1);
void setup() {
I2Cone.begin(21,22,100000); // SDA pin 21, SCL pin 22, 100kHz frequency
I2Ctwo.begin(16,17,400000); // SDA pin 16, SCL pin 17, 400kHz frequency
}
void loop() {
I2Cone.beginTransmission(0x38);
I2Cone.write(0x00 | 0x80);
I2Cone.write(0x03);
I2Cone.endTransmission();
I2Ctwo.beginTransmission(0x42);
I2Ctwo.write(140);
I2Ctwo.endTransmission();
I2Ctwo.requestFrom(0x42,1);
if (I2Ctwo.available() == 1) {
byte value = I2Ctwo.read();
}
}
Code is not tested, but that is how I would approach it.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHubhttps://github.com/espressif/arduino-esp32/issues/977#issuecomment-355896603, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AB8o7rhxI73ID4CdBbKUDzV06KAqGr94ks5tIcFLgaJpZM4RVw8c.
Hello again.
beegee-tokyo I have tested your code and it works perfectly, thank you very much for your help.
TheFlyingZephyr if I use the two buses in the same task, as my level is basic I just do not understand the idea you want to convey to me.
Although I did not explain the reason why I wanted to use the two I2C bus, I feel obliged to say it, I am making an application that uses the Blynk APP (http://www.blynk.cc/) on the one hand and on the other hand the ESP32 with two ports PCF8574, one used as digital inputs and the other as outputs, to read the input port I use an interruption, and when I perform a write operation on the output port and immediately I get the interruption of the port entries, reading the port of entries is wrong, so I want to try to put each of them on a different I2C bus.
Regards, and thank you very much
BlackTiger 44
Thanks beegee-tokyo. With your help, i could modify a I2C Scanner to this dual channel I2C Scanner. Tested with ESP32 TTGO TQ. OLED on second, BME280 on first.
/* I2C slave Address Scanner
for 5V bus
* Connect a 4.7k resistor between SDA and Vcc
* Connect a 4.7k resistor between SCL and Vcc
for 3.3V bus
* Connect a 2.4k resistor between SDA and Vcc
* Connect a 2.4k resistor between SCL and Vcc
Kutscher07: Modified for TTGO TQ board with builtin OLED
*/
#include <Wire.h>
#include <dummy.h> //for esp32
#define SDA1 21
#define SCL1 22
#define SDA2 5
#define SCL2 4
TwoWire I2Cone = TwoWire(0);
TwoWire I2Ctwo = TwoWire(1);
void scan1(){
Serial.println("Scanning I2C Addresses Channel 1");
uint8_t cnt=0;
for(uint8_t i=0;i<128;i++){
I2Cone.beginTransmission(i);
uint8_t ec=I2Cone.endTransmission(true);
if(ec==0){
if(i<16)Serial.print('0');
Serial.print(i,HEX);
cnt++;
}
else Serial.print("..");
Serial.print(' ');
if ((i&0x0f)==0x0f)Serial.println();
}
Serial.print("Scan Completed, ");
Serial.print(cnt);
Serial.println(" I2C Devices found.");
}
void scan2(){
Serial.println("Scanning I2C Addresses Channel 2");
uint8_t cnt=0;
for(uint8_t i=0;i<128;i++){
I2Ctwo.beginTransmission(i);
uint8_t ec=I2Ctwo.endTransmission(true);
if(ec==0){
if(i<16)Serial.print('0');
Serial.print(i,HEX);
cnt++;
}
else Serial.print("..");
Serial.print(' ');
if ((i&0x0f)==0x0f)Serial.println();
}
Serial.print("Scan Completed, ");
Serial.print(cnt);
Serial.println(" I2C Devices found.");
}
void setup(){
Serial.begin(115200);
I2Cone.begin(SDA1,SCL1,400000); // SDA pin 21, SCL pin 22 TTGO TQ
I2Ctwo.begin(SDA2,SCL2,400000); // SDA pin 5, SCL pin 4 builtin OLED
}
void loop(){
scan1();
Serial.println();
delay(100);
scan2();
Serial.println();
delay(5000);
}
Added to the Wiki
Thanks for sharing your work Kutscher07.
Greetings BlackTiger
This issue is closed, because it looks as if it is not a bug or problem with the ESP32 Arduino core or its support libraries. For general API usage questions or help on specific coding challenges, please visit the arduino-esp32 Gitter channel. If you feel this issue was closed in error, reopen it and comment, why you think this is a bug in the Arduino-Core.
Can anyone confirm that the second i2c channel is really working? When I try to use the I2C Scanner example above I can only use the first TwoWire(0) Channel. When I change the SDA1 / 2 and SCL1 / 2 the scanner detects my second I2C String but I can't use two at the same time. Second one is always empty. Even when I Change
TwoWire I2Cone = TwoWire(0);
TwoWire I2Ctwo = TwoWire(1);
to
TwoWire I2Cone = TwoWire(1);
TwoWire I2Ctwo = TwoWire(0);
only the I2Cone Channel is successfully used.
I keep trying to get ESP32 and BME_280 working on a second I2C-Bus using
and
Any solutions?
I've tried different BME_280 libraries. They all do not seem to work for ESP32.
In the Wire.cpp lib for the ESP32 there are already two available I2C ports:
TwoWire Wire = TwoWire(0);
TwoWire Wire1 = TwoWire(1);
To use in your project just initialise separately e.g.
Wire.begin(16, 17, 400000);
Wire1.begin(21, 22, 400000);
then use as usual.
Thanks guys ! I was almost there this put me on the way !
In the Wire.cpp lib for the ESP32 there are already two available I2C ports:
TwoWire Wire = TwoWire(0);
TwoWire Wire1 = TwoWire(1);To use in your project just initialise separately e.g.
Wire.begin(16, 17, 400000);
Wire1.begin(21, 22, 400000);then use as usual.
This solved the problem for me! Thank you Tim!!
Most helpful comment
Thanks beegee-tokyo. With your help, i could modify a I2C Scanner to this dual channel I2C Scanner. Tested with ESP32 TTGO TQ. OLED on second, BME280 on first.