Sub topic 8
Finding devices with a bus scanner
This chapter assumes you can write a basic Arduino sketch and use the Serial Monitor. It also assumes you know what a 7-bit address is and have wired SDA and SCL with pull-up resistors. A bus scanner is the first thing to run on any new I2C setup. It tells you which addresses answer, so you can check your wiring and your assumptions about each device before you write real driver code.
How a scan works
The I2C protocol has no "list all devices" command. A controller can only address one target at a time. To find out who is on the bus, the scanner tries every address and watches for an ACK.
For each candidate address, the scanner does three things:
- It sends a START condition and the address byte with the write bit.
- A target that owns that address pulls SDA low during the ninth clock pulse. This is the ACK.
- The scanner sends STOP without sending any data.
On Arduino, Wire.beginTransmission(address) followed immediately by Wire.endTransmission() produces exactly this sequence. endTransmission() returns a status code, and 0 means the address was acknowledged.
The basic scanner
This sketch tries every address from 1 to 126 and prints the ones that answer.
#include <Wire.h>
void setup() {
Serial.begin(9600);
Wire.begin();
}
void loop() {
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);
}
}
Serial.println("Scan done");
delay(5000);
}Open the Serial Monitor at 9600 baud. A typical result with a BME280 sensor and a small OLED display looks like this:
Device found at 0x3C
Device found at 0x76
Scan doneThe if (address < 16) line pads single-digit values, so you see 0x08 and not 0x8. Without the padding, addresses are hard to read and compare against datasheets.
Return codes from endTransmission
The return value carries more information than "found" or "not found". The Arduino documentation defines these codes:
| Code | Meaning | What it usually tells you |
|---|---|---|
| 0 | Success | A target ACKed the address |
| 1 | Data too long for the transmit buffer | Not possible in a scan, since you send no data |
| 2 | NACK on transmit of address | Nothing at this address. This is the normal result for empty addresses |
| 3 | NACK on transmit of data | A device ACKed the address but refused a data byte |
| 4 | Other error | Often a bus problem, such as lost arbitration or a bus stuck low |
| 5 | Timeout | Returned by some cores when a timeout is enabled |
Codes 2 and 0 are the ones you expect. A scan that prints many 4s, or hangs, points to an electrical fault and not an absent device. Look at the troubleshooting chapter for those cases.
A scanner that reports errors
The basic version hides code 4 and scans addresses that the I2C specification reserves. This version scans only the usable range, counts devices, and reports unexpected codes.
#include <Wire.h>
const uint8_t FIRST_ADDRESS = 0x08; // 0x00-0x07 are reserved
const uint8_t LAST_ADDRESS = 0x77; // 0x78-0x7F are reserved
void printHex2(uint8_t value) {
Serial.print("0x");
if (value < 16) Serial.print('0');
Serial.print(value, HEX);
}
void scanBus() {
uint8_t found = 0;
Serial.println("Scanning...");
for (uint8_t address = FIRST_ADDRESS; address <= LAST_ADDRESS; address++) {
Wire.beginTransmission(address);
uint8_t error = Wire.endTransmission();
if (error == 0) {
Serial.print(" Device at ");
printHex2(address);
Serial.println();
found++;
} else if (error != 2) {
Serial.print(" Unexpected error ");
Serial.print(error);
Serial.print(" at ");
printHex2(address);
Serial.println();
}
}
if (found == 0) {
Serial.println("No I2C devices found. Check wiring and pull-ups.");
} else {
Serial.print(found);
Serial.println(" device(s) found.");
}
}
void setup() {
Serial.begin(9600);
Wire.begin();
delay(100); // give slow-starting devices time to power up
}
void loop() {
scanBus();
delay(5000);
}The loop condition uses <= with LAST_ADDRESS because 0x77 is a valid address. The type is uint8_t, so the loop cannot overflow, since 0x77 is far below 255.
Reading the results
A scan gives you addresses, not device names. The same address can belong to several chips, so treat the result as a clue. The table lists addresses you will often see. Always confirm against the datasheet or breakout board documentation.
| Address | Common devices |
|---|---|
| 0x27 or 0x3F | PCF8574-based character LCD backpacks |
| 0x3C or 0x3D | SSD1306 OLED displays |
| 0x40 | INA219 current sensor, PCA9685 PWM driver, HTU21D humidity sensor (defaults vary) |
| 0x48 to 0x4B | ADS1115 analog-to-digital converter, selected by the ADDR pin |
| 0x68 | DS3231 real-time clock, MPU-6050 motion sensor |
| 0x70 | TCA9548A multiplexer (default) |
| 0x76 or 0x77 | BME280 and BMP280 sensors, selected by the SDO pin |
Some boards carry more than one chip. A DS3231 module often also has an EEPROM, so a scan shows two addresses for one board. That is normal.
When the scan does not match expectations
The scan result is a good diagnostic when you compare it with what you expect.
- Nothing found. The usual causes are swapped SDA and SCL, no common ground, a device without power, or missing pull-ups. Many breakout boards include pull-ups, but a bare chip does not.
- Address differs from the datasheet. Some documents give 8-bit addresses with the read/write bit included. A device listed as 0x78 for writes has the 7-bit address 0x3C. Wire expects the 7-bit form.
- A device appears only sometimes. Suspect a loose breadboard contact, a long wire, or a device that needs more start-up time. Add a delay before the scan and retry.
- Every address answers. Often SDA is held low or shorted. A faulty device or a wiring short can also cause it. Disconnect devices one at a time until the output becomes sensible.
- The sketch hangs inside the scan. A stuck bus can block
endTransmission(). On AVR boards with a recent core, you can enable a timeout withWire.setWireTimeout()so that a stuck bus returns an error code and does not hang. Check that your board package supports it.
Using the scanner in practice
The scanner is most useful at three moments.
- Before writing code for a new device. Confirm the address and find out whether jumpers or solder pads changed it from the documented default.
- After adding a second device. Scan again and check that both addresses appear. If one disappears when the other connects, they may share an address or the added capacitance may be too high.
- When a sketch stops working. Run the scanner as the first diagnostic. If a device that worked yesterday is missing, the fault is physical, not in your driver code.
The scan also shows you address conflicts in a particular way. Two devices with the same address do not produce two lines in the output. They produce one line, because both ACK together. A scanner cannot detect a conflict directly. If you connect two identical sensors and see only one address, a conflict is the likely cause. The next chapters cover this in detail.
Exercise
You connect two identical BME280 breakouts to one bus, both at the default address. The scanner prints one line, "Device at 0x76", and "1 device(s) found." Why does the scan not show two devices, and what should you try?
Show answer
Both sensors answer the same address, and the scanner only observes whether SDA was pulled low during the ACK bit. Both devices pull it low at once, so the controller sees one ACK and cannot tell that two devices responded. The scan shows one address either way. Fix it by changing one sensor's address (for the BME280, connect its SDO pin to supply so it uses 0x77 instead of 0x76), or by putting each sensor on its own channel of a multiplexer such as the TCA9548A. Then scan again and check that both addresses appear.
Exercise
A datasheet says a sensor's write address is 0xD0. What value do you pass to Wire.beginTransmission(), and which address should the scanner report?
Show answer
Wire takes the 7-bit address, so shift the 8-bit value right by one bit: 0xD0 >> 1 = 0x68. Pass 0x68, and the scanner reports 0x68.