raw Software
RAW Software Drivers I2C Communication

Arduino I2C Bus Scanner

Robert Eisele

An I2C scanner is a quick way to verify wiring and discover the 7-bit addresses of devices connected to an Arduino. The sketch below probes every normal device address and prints each address that acknowledges the request.

#include <Wire.h>

void setup() {
  Serial.begin(115200);
  Wire.begin();

  byte found = 0;
  byte busErrors = 0;

  Serial.println(F("Scanning the I2C bus ..."));

  // 0x00-0x07 and 0x78-0x7F are reserved I2C addresses.
  for (uint8_t address = 0x08; address <= 0x77; address++) {
    Wire.beginTransmission(address);
    const uint8_t result = Wire.endTransmission();

    if (result == 0) {
      Serial.print(F("Device at 0x"));
      if (address < 0x10) {
        Serial.print('0');
      }
      Serial.print(address, HEX);
      Serial.print(F(" ("));
      Serial.print(address, DEC);
      Serial.println(')');
      found++;
    } else if (result == 4) {
      Serial.print(F("Bus error near 0x"));
      if (address < 0x10) {
        Serial.print('0');
      }
      Serial.println(address, HEX);
      busErrors++;
    }
  }

  Serial.print(F("Scan complete: "));
  Serial.print(found);
  Serial.print(F(" device(s), "));
  Serial.print(busErrors);
  Serial.println(F(" bus error(s)."));
}

void loop() {
}

How the Scan Works

Wire.beginTransmission(address) prepares an address frame, and Wire.endTransmission() sends it without a data payload. A return value of 0 means that a device acknowledged the address. Return value 2 means that no device acknowledged it, which is expected for unused addresses; 4 indicates another bus-level error.

The usable 7-bit I2C device range is normally 0x08 through 0x77. The remaining addresses are reserved by the I2C specification and are intentionally skipped.

If No Device Is Found

An address scan confirms that a device responds on the bus, but it does not identify the device model or prove that its registers are working correctly. Compare the reported hexadecimal address with the address table in the device data sheet.