I am trying to multiplex at least 6 i2c devices using a TCA9548A. I got the scanner code from the Adafruit website. The code has a knack for working one moment and then not working the next. For instance, the scanner was working perfectly yesterday, but today it's like the scanner is on vacation. I check my wiring, and that was fine (there are only 4 wires). I would appreciate input into what I can do to reinforce the code or if y'all have a better way to scan I2C devices with a TCA9548A.
/**
TCA9548 I2CScanner.ino -- I2C bus scanner for Arduino
Based on https://playground.arduino.cc/Main/I2cScanner/
*/
#include "Wire.h"
#define TCAADDR 0x70
void tcaselect(uint8_t i) {
if (i > 7) return;
Wire.beginTransmission(TCAADDR);
Wire.write(1 << i);
Wire.endTransmission();
}
// standard Arduino setup()
void setup()
{
while (!Serial);
delay(1000);
Wire.begin();
Serial.begin(115200);
Serial.println("\nTCAScanner ready!");
for (uint8_t t = 0; t < 8; t++) {
tcaselect(t);
Serial.print("TCA Port #");
Serial.println(t);
for (uint8_t addr = 0; addr <= 127; addr++) {
if (addr == TCAADDR) continue;
Wire.beginTransmission(addr);
if (!Wire.endTransmission()) {
Serial.print("Found I2C 0x"); Serial.println(addr, HEX);
}
}
}
Serial.println("\ndone");
}
void loop()
{
}
The Scanning sketch seems okay. You can run the scanning in the loop(), then you can wiggle wires and see if that helps.
You forgot to tell/show a few things: Which Arduino board ? Which unknown devices (please give a link to where you bought them). Can you show a photo ? Can you show a schematic ? Breadboards have bad contacts and jumper wires can be broken. Do you use the multiplexer as a level shifter ? Are there long wires for the I2C bus ? Do the (sub) I2C buses have pullup resistors ? and so on, and so on.
I tried putting the scanning portion, in the loop(), and it started to work. I wiggled the wire once, and it stopped working. Now, I cannot upload the code.
Other info: Board: I alternate btwn the Uno and Mega, but I mostly use the Uno for this code. Devices: 4 Honeywell SEK001 with HSC pressure/temp sensors and 1 Sensirion SHT4X
(Mind you the devices scanned yesterday all 5 devices were scanned) Wire Diagram:
About 5-6 inch can already be a problem when you have a flat ribbon cable with SDA next to SCL.
If wiggling the wires make a different, then bad contacts might be one of the problems. I don't know if you have a breadboard.
The HoneyWell HSC pressure sensors are available in 3.3V and 5V versions. I don't know which one you have.
You have everywhere a 5V I2C bus, but the SHT4X sensor is a 3.3V sensor. Maybe you bought a module with a onboard level shifter ?
The 2k2 resistors in the signal path of the I2C bus prevents the I2C bus from working.
Your multiplexer module has pullup resistors for the I2C bus between the Uno and the multiplexer, but none on all the eight sub-I2C buses. Do you know if the Honeywell sensors have internal pullup resistors ? I don't think so, I could not find it in the datasheet. That means that all those sub-I2C buses are not working.
You see, everything matters. The I2C bus works best when the voltage levels are the same everywhere and pullup resistors are neccessary.