I2C/Addresses/Scanning the bus

Scanning the bus

A scan calls every address from 0x08 to 0x77 and listens on the ninth clock of each, where a chip that heard its own address pulls SDA low. It is the first thing to run when a module does not answer.

After every byte, the receiver says whether it got it. The controller sends a ninth clock pulse and lets go of SDA for it. A chip that received the byte pulls SDA low during that pulse: an acknowledge, ACK. If nobody pulls it low, the line stays high, and that is a NACK.

So the controller can ask, for any address, whether anyone is there: send the address and see whether the ninth clock comes back low.

A scan does that for every address in turn, 0x08 to 0x77, and lists the ones that answered. At 100 kHz each call takes about a tenth of a millisecond, so all 112 take around a hundredth of a second. On an ESP32:

#include <Wire.h>

void setup() {
  Serial.begin(115200);
  Wire.begin(21, 22);  // SDA, SCL
  for (uint8_t a = 0x08; a <= 0x77; a++) {
    Wire.beginTransmission(a);
    if (Wire.endTransmission() == 0) Serial.printf("found 0x%02X\n", a);
  }
}

void loop() {}

endTransmission() returns 0 when the address was acknowledged. On Linux, i2cdetect from the lm-sensors project does the same and prints the table the bench draws.

What a scan cannot tell you

A scan hears that someone answered. It does not know which chip that was, and two chips answering at the same address look exactly like one. If a module does not show up at all, suspect the wiring before the module: SDA and SCL swapped, no pull-up resistors (lesson 9), or no power.

Edit this page — content/fundamentals/i2c/scanning-the-bus.mdx

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Fundamentals on the forum →