Sub topic 16
Troubleshooting a misbehaving bus
This chapter gives you a fixed order for diagnosing an I2C bus that does not respond. It assumes you have read the earlier chapters on open-drain wiring, pull-up resistors, addressing, the Wire library, and the address scanner. You should be comfortable with the Serial Monitor, hexadecimal numbers, and breadboard wiring.
Why a fixed order helps
Most I2C faults look the same from the sketch. The scanner finds nothing, or requestFrom returns fewer bytes than you asked for, or the values are garbage. The causes sit in different layers: power, wiring, electrical levels, addressing, and software. If you change several things at once, you cannot tell which change fixed the problem.
Work from the bottom layer up. Check power and wiring first, then the electrical state of the lines, then the address, then the protocol use in your code.
Step 1: Power, ground and wiring
Check these before anything else, because they cause more failures than any protocol detail.
- Every device must be powered within its supported voltage range.
- Every device must share a common ground with the Arduino. A sensor powered from a separate supply with no shared ground will never ACK.
- SDA goes to SDA and SCL goes to SCL. On an Arduino Uno R3 these are A4 (SDA) and A5 (SCL), and also the dedicated SDA and SCL header pins near AREF. On other boards the pins differ, so check the board's pinout.
- Breadboard rows and jumper wires fail more often than you expect. Reseat each wire, or swap in a different jumper.
A common mistake is swapping SDA and SCL. The scanner then reports nothing, and nothing is damaged, so swapping them back is a safe first experiment.
Step 2: Check the idle state of the lines
When no transaction is running, both SDA and SCL should sit at the supply voltage because the pull-up resistors hold them high. A multimeter on the SDA pin, measured against ground, gives a quick check.
| Reading on SDA or SCL (idle) | Likely cause |
|---|---|
| Near the supply voltage (3.3 V or 5 V) | Pull-ups present, line is healthy |
| Near 0 V | A device or wire is holding the line low, or the line is shorted to ground |
| Floating, wandering value | No pull-up on the line |
| Well below supply, such as 1.5 V on a 5 V bus | Pull-ups too weak, or several devices pulling down through leakage, or a damaged device |
If a line is stuck low, disconnect devices one at a time. When the line returns high, the last device you removed was holding it down. A device that was reset mid-transaction can also hold SDA low while it waits for clock pulses that never come. Power-cycling the device usually clears this.
If a line floats, add pull-ups. Many breakout boards include them, so a board you add may already provide 4.7 kΩ or 10 kΩ resistors. Several boards in parallel lower the combined resistance, which is covered in the next section.
Step 3: Pull-up values and bus capacitance
Pull-ups can be wrong in two directions.
- Too weak (too large a resistance): the line rises slowly after a device releases it. At 100 kHz this often still works. At 400 kHz the signal may not reach a valid high level before the next clock edge, and you see random NACKs or corrupted data.
- Too strong (too small a resistance): the line is pulled up so hard that a device cannot pull it all the way low. The I2C specification limits the sink current of a device to 3 mA for standard-mode and fast-mode, so the resistor must keep the current below that.
Resistors in parallel combine as R = 1 / (1/R1 + 1/R2 + ...). Three breakout boards each with 4.7 kΩ pull-ups give about 1.57 kΩ. On a 5 V bus that draws roughly 3.2 mA, which is already above the limit. The symptom is a low level that is not low enough, and devices that work alone fail when combined.
The fix is to remove the pull-ups from all but one board, by cutting a solder jumper or desoldering the resistors, or to use a mid-range value for the whole bus. Common choices are 4.7 kΩ for 100 kHz on short wiring and 2.2 kΩ to 3.3 kΩ for 400 kHz or longer wires.
Long wires add capacitance, which also slows the rise. If a bus works on the bench and fails when you attach a one-meter cable, lower the clock speed first, then lower the pull-up resistance within the sink-current limit.
Step 4: Voltage mismatch
A 3.3 V sensor connected directly to a 5 V Arduino can fail in two ways. The bus may appear to work and the sensor degrade or die, or it may not work at all.
- If the pull-ups connect to 5 V, the sensor's pins see 5 V, which can exceed their absolute maximum rating.
- If the pull-ups connect to 3.3 V and the Arduino runs at 5 V, the Arduino may see 3.3 V as a marginal high. An AVR running at 5 V requires roughly 0.7 × VCC, or 3.5 V, to read a high reliably, so 3.3 V falls below that.
Use a bidirectional level shifter built for open-drain buses, with one side pulled up to each supply voltage. A shifter designed for push-pull signals, such as a one-direction buffer, does not work on SDA, because SDA carries data in both directions.
Step 5: Run the scanner and read the result
With the wiring confirmed, upload the scanner from earlier in this tutorial. Interpret its output as follows.
| Scanner result | Meaning and next action |
|---|---|
| No devices found | Power, ground, swapped lines, missing pull-ups, or a voltage mismatch. Return to steps 1 to 4. |
| Expected address found | The bus layer works. Move on to the register code. |
| A different address than the datasheet states | Use the scanned address. Check address pins and whether the datasheet lists the address with or without the read/write bit. |
| Many addresses found, or every address responds | SDA is likely stuck low or shorted, so every address appears to be acknowledged. Check the idle level. |
| Device appears and disappears | Loose connection, marginal pull-ups, or a brown-out on the device supply. |
Datasheets sometimes list an 8-bit address such as 0x90 for a device the Wire library sees at 0x48. The 8-bit form includes the read/write bit in the lowest position. Wire expects the 7-bit address, so shift the datasheet value right by one bit: 0x90 >> 1 is 0x48.
Test with one device at a time. A device that scans correctly alone but fails on the full bus points to an address conflict, pull-up loading, or capacitance. Two devices at the same address both answer, and their data is merged on the wire, so the reading looks plausible but wrong. The remedy is an address pin or a TCA9548A multiplexer, as covered earlier.
Step 6: Check what endTransmission tells you
Wire.endTransmission() returns a status code. Print it instead of discarding it.
| Code | Meaning |
|---|---|
| 0 | Success |
| 1 | Data too long for the transmit buffer |
| 2 | NACK received on the address byte |
| 3 | NACK received on a data byte |
| 4 | Other error |
| 5 | Timeout (on cores that support it) |
Code 2 means nobody answered at that address, so check wiring and address. Code 3 means the device exists but rejected a data byte, often because you wrote to a register that is read-only or does not exist. Code 1 means you wrote more than the buffer holds, which is 32 bytes on AVR boards.
Step 7: Check the register code
If the scanner finds the device but values are wrong, the cause is usually in the protocol use.
- No repeated START: many devices need
endTransmission(false)beforerequestFrom. With the defaulttrue, the STOP releases the bus and some devices forget the register pointer. - Wrong byte order: most 16-bit registers are big-endian, high byte first. If your result has its bytes swapped, change the shift order.
- Short reads:
requestFromreturns the number of bytes received. Compare it with the count you requested, as the earlier register example does. - Reading too soon: some sensors need a conversion time after a command. Reading early returns the old value or a NACK.
Here is a diagnostic sketch that combines these checks. It reads a register and reports the exact failure.
#include <Wire.h>
const uint8_t DEVICE_ADDRESS = 0x48; // change to the address your scanner found
const uint8_t REGISTER_POINTER = 0x00;
void setup() {
Serial.begin(9600);
Wire.begin();
Wire.setClock(100000); // start slow while debugging
}
void loop() {
Wire.beginTransmission(DEVICE_ADDRESS);
Wire.write(REGISTER_POINTER);
byte status = Wire.endTransmission(false);
if (status != 0) {
Serial.print("endTransmission error: ");
Serial.println(status);
} else {
byte received = Wire.requestFrom(DEVICE_ADDRESS, (uint8_t)2);
if (received != 2) {
Serial.print("Short read, got ");
Serial.print(received);
Serial.println(" bytes");
} else {
uint16_t value = (uint16_t)Wire.read() << 8;
value |= Wire.read();
Serial.print("Register value: 0x");
Serial.println(value, HEX);
}
}
delay(1000);
}Start at 100 kHz. If the problem disappears at 100 kHz and returns at 400 kHz, the cause is electrical (pull-ups, capacitance, or a slow device), not your code.
Step 8: Hung bus and blocking calls
On older AVR cores, a stuck bus can make Wire calls block forever, so the sketch seems to freeze. Recent cores provide Wire.setWireTimeout() on AVR, which lets a call give up and report a timeout. If your board's core does not offer it, a watchdog timer is the fallback.
If a device holds SDA low after a reset of the Arduino, the controller cannot generate a START. The standard recovery is to switch SCL to a plain output, pulse it up to nine times until SDA releases, then send a STOP and call Wire.begin() again. Power-cycling the stuck device is simpler during development.
Quick reference
| Symptom | First check |
|---|---|
| Scanner finds nothing | Power, ground, SDA/SCL swap, pull-ups |
| Everything "answers" | SDA stuck low or shorted |
| Works alone, fails with others | Address conflict or pull-up loading |
| Works at 100 kHz, fails at 400 kHz | Pull-ups too weak, capacitance |
| endTransmission returns 2 | Wrong address or no device |
| endTransmission returns 3 | Write to invalid register or busy device |
| Values byte-swapped | Byte order in the read code |
| Sketch freezes | Hung bus, add timeout or recovery |
Exercise
Three breakout boards, each with 4.7 kΩ pull-ups on SDA, are connected to one 5 V bus. Each device works alone, but the devices fail when all three are connected. Calculate the combined pull-up resistance, state why this causes failures, and give two fixes.
Show answer
Three 4.7 kΩ resistors in parallel give 4.7 / 3, about 1.57 kΩ. At 5 V the pull-up current when a device holds the line low is about 5 / 1570, roughly 3.2 mA. That is above the 3 mA sink-current limit, so devices cannot pull the line down to a valid low level. Fixes: remove the pull-ups from two of the boards (cut the solder jumper or remove the resistors), leaving one set; or replace all of them with a single pair of external pull-ups of about 4.7 kΩ on the bus.
Exercise
A datasheet gives the device address as 0xD0 (8-bit, including the write bit). The scanner reports a device at 0x68. Explain the relationship, and say which value you pass to Wire.beginTransmission.
Show answer
The 8-bit value 0xD0 is the 7-bit address shifted left by one with the read/write bit in the lowest position. Shifting 0xD0 right by one gives 0x68, which matches the scanner. Pass 0x68 to Wire.beginTransmission, because the Wire library takes the 7-bit address and adds the read/write bit itself.
Exercise
A sketch reads a sensor register with beginTransmission, write(reg), endTransmission(), then requestFrom. The scanner finds the device, but the values are always zero or stale. What change should you try first, and why?
Show answer
Change endTransmission() to endTransmission(false) so the controller sends a repeated START instead of a STOP. Some devices lose the register pointer when a STOP occurs, so the following read returns data from the wrong place. Also check that requestFrom returns the number of bytes requested.