Sub topic 16
Troubleshooting a Misbehaving I2C Bus
Assume you know basic Arduino sketches, Serial Monitor use, resistors, breadboard wiring, and hexadecimal numbers. I2C faults are usually caused by wiring, voltage levels, pull-up resistors, or an incorrect address. Test one cause at a time.
Start with the electrical checks
I2C uses two shared lines:
- SDA carries data.
- SCL carries the clock.
- Both lines need pull-up resistors to the bus voltage.
I2C devices pull a line low but do not actively drive it high. Without pull-ups, SDA and SCL may remain low or change too slowly. Check that every device shares ground, SDA connects to SDA, and SCL connects to SCL.
A device supplied with 3.3 V may not safely accept 5 V signals. Use a bidirectional level shifter when the voltage ranges are incompatible. Also check whether breakout boards already include pull-up resistors. Several boards in parallel can create a pull-up value that is too low.
Use the scanner first
A scanner tells you whether a device acknowledges its address. It does not prove that the device is configured correctly or that its registers work.
#include <Wire.h>
void setup() {
Serial.begin(9600);
Wire.begin();
}
void loop() {
byte found = 0;
for (byte address = 1; address < 127; address++) {
Wire.beginTransmission(address);
byte error = Wire.endTransmission();
if (error == 0) {
Serial.print("Found 0x");
if (address < 16) Serial.print('0');
Serial.println(address, HEX);
found++;
}
}
Serial.print("Devices found: ");
Serial.println(found);
delay(5000);
}If the scanner finds nothing, inspect power, ground, SDA, SCL, pull-ups, and voltage compatibility before checking application code. Confirm the device's actual 7-bit address from its datasheet. Do not use an address shown as an 8-bit read or write value.
Interpret transaction failures
endTransmission() reports the result of sending an address and data. On common Arduino implementations, zero means success. A result of 2 usually means the target did not acknowledge its address. A result of 3 means it did not acknowledge data.
requestFrom() returns the number of bytes received. Check this value before calling Wire.read(). A wrong register address, missing repeated START, or incorrect byte count can make a working device appear faulty.
| Symptom | Likely causes | First check |
|---|---|---|
| No devices found | Wiring, power, pull-ups, wrong address | Scanner and continuity |
| One device works alone but not together | Address conflict or excessive pull-ups | Addresses and resistor totals |
| SDA or SCL stays low | Short circuit, unpowered device, stuck target | Disconnect devices one at a time |
| Works slowly but fails fast | Capacitance, weak pull-ups, speed | Lower the clock |
| Data values are wrong | Register format or byte order | Datasheet and logic analyzer |
Reduce speed and wiring problems
The default clock is often 100 kHz. Try 100 kHz before using 400 kHz. Long wires and many devices add bus capacitance, which slows rising edges. Short wires, a common ground, suitable pull-ups, and fewer connectors improve signal quality.
Exercise
A scanner finds a sensor at 0x68, but register reads fail at 400 kHz and work at 100 kHz. What should you investigate?
Show answer
Investigate bus capacitance, wire length, and pull-up strength. Keep the bus at 100 kHz unless every device and the wiring support the faster rate.