Search code examples
arduinoesp32i2cadafruitfeatherwing

Which Pins to use for second I2C-Bus on Adafruit Featherwing


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()


Solution

  • 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 a TwoWire (the class of Wire and Wire1) object:

    Wire1.set_pins(PIN_SDA, PIN_SCL);
    

    It's important to call this before calling the begin() method. Doing this allows you to configure Wire1 before passing it to a library which may call begin().

    You can also set the pins when you call begin():

    Wire1.begin(PIN_SDA, PIN_SCL);
    

    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.