I have trouble defining the second I2C-bus on my Adafruit Featherwing Huzzah32 ESP32. The standard I2C-bus uses SDA Pin 23 & SCL Pin 22.
Are there predefined pins for the second bus (maybe 33&32?) or is it possible to use any GPIO as long as my software defines them using e.g. Wire1.setPins()
Which Pins to use for second I2C-Bus on Adafruit Featherwing
832 views Asked by karovier At
2
There are 2 answers
0
On
Only one is configured by default in the Feather Arduino IDE support shown here
A better bet would be to address other devices as slaves using their slave address and the same I2C pins like this. I'm assuming you're trying to reach multiple breakout boards, so your Featherwing should run the master code. Then replace the slave address number with the one associated with the breakout board you want to address.
Most ESP32 breakout boards don't define default pins for the second I2C controller, so you'll need to define them when you configure it.
As the question mentioned, you can use the
set_pins()
method on aTwoWire
(the class ofWire
andWire1
) object:It's important to call this before calling the
begin()
method. Doing this allows you to configureWire1
before passing it to a library which may callbegin()
.You can also set the pins when you call
begin()
:You can also remap
Wire
(the first I2C controller) to different pins this way.Unfortunately, some libraries do not allow you to pass a
TwoWire
object to them, so you'll probably need to make a local copy of any library like this and modify it.You can use most, but not all, GPIO pins on the ESP32. The pins to avoid are the strapping pins, which determine how the processor boots (GPIO0 and GPIO12 - 12 may work for SCL but I would avoid it if possible), and the high numbered ADC pins, which are input-only (GPIO34 - GPIO39). You might also want to avoid whatever pin is connect to the on board LED, if there is one.
Random Nerd Tutorials has a good overview of what pins are usable on ESP32 CPUs. The usable pins will vary if you're using other variations of the ESP32 like the S2 or S3.
As a couple of people have pointed out, you can connect multiple I2C devices to one controller, but there are valid reasons for using multiple I2C busses, which is what the question asked.
Sometimes an I2C device will not operate well with others (especially if it uses clock stretching). You can also operate two different busses at different speeds. And if you use multiple busses, if a device on one bus crashes and jams the bus it won't interfere with other busses.
And of course, if you need to use two devices which have the same I2C address and can't change the address, using the second built-in I2C controllers is easier than using an I2C multiplexor.