Sub topic 8
Finding Devices with an I2C Bus Scanner
Assume you know basic Arduino sketches, Serial Monitor use, resistors, and hexadecimal numbers. This chapter uses the Arduino Wire library. The scanner helps you discover which 7-bit I2C addresses answer on a connected bus.
What the scanner does
An I2C scanner tests possible addresses one at a time. For each address, it sends a START condition, the address, and a write direction bit. A target device that recognizes the address sends an acknowledge, or ACK. The scanner reports that address when Wire.endTransmission() returns 0.
The scanner does not identify the device model. For example, an answer at 0x68 might come from several different kinds of sensor. Use the device's documentation to confirm the address and its register map.
#include <Wire.h>
void setup() {
Serial.begin(9600);
Wire.begin(); // Start as the I2C controller
Serial.println("I2C scanner");
}
void loop() {
byte found = 0;
for (byte address = 1; address < 127; address++) {
Wire.beginTransmission(address);
byte error = Wire.endTransmission();
if (error == 0) {
Serial.print("Device found at 0x");
if (address < 16) {
Serial.print('0');
}
Serial.println(address, HEX);
found++;
}
}
if (found == 0) {
Serial.println("No devices found");
}
Serial.println("Scan done\n");
delay(5000);
}Open the Serial Monitor at 9600 baud. A working bus might produce output like this:
I2C scanner
Device found at 0x3C
Device found at 0x68
Scan doneThe 0x prefix indicates hexadecimal notation. The scanner checks addresses from 1 through 126. It skips address 0, which is reserved for general calls, and address 127, which is not a normal target address.
Reading scanner results
| `endTransmission()` result | Meaning |
|---|---|
| `0` | A device acknowledged the address |
| `2` | No device acknowledged the address |
| `3` | The device rejected transmitted data |
| `4` | Another I2C error occurred |
A scanner result is electrical evidence, not proof that the device is configured correctly. A device may acknowledge its address but still return invalid data because it is in reset, uses the wrong register protocol, or needs initialization.
Common causes of an empty scan
Check the physical bus before changing the program:
- Connect the Arduino SDA pin to the target SDA pin.
- Connect SCL to SCL and connect the grounds.
- Confirm that the target has power.
- Check that SDA and SCL have pull-up resistors to the correct voltage.
- Confirm the board's actual SDA and SCL pins. They are not always the pins labeled with familiar digital numbers.
- Check whether the target's address pins change its address.
- Disconnect other modules temporarily in case one device holds a line low.
Many breakout boards already include pull-up resistors. Several boards in parallel can make the total pull-up resistance too low, however. Start with a short connection and one known-good device.
> callout note > A device that appears at an unexpected address may be using a different address option, a shifted address from its data sheet, or a decimal value displayed as hexadecimal. Always compare the scanner output with the manufacturer's documentation.
Exercise
The scanner reports 0x3C, but your display documentation lists address 60. Is this necessarily a mismatch?
Show answer
No. Decimal 60 is hexadecimal 0x3C, so the values describe the same 7-bit address.