There is an excellent I2C scanner sketch available from the old Arduino Playground here:

Here is a slightly modified version that allows easily specifying an alternate I2C bus. See below for more info. The default Wire should hopefully work for most boards though.

// --------------------------------------
// i2c_scanner
//
// Modified from https://playground.arduino.cc/Main/I2cScanner/
// --------------------------------------

#include <Wire.h>

// Set I2C bus to use: Wire, Wire1, etc.
#define WIRE Wire

void setup() {
  WIRE.begin();

  Serial.begin(9600);
  while (!Serial)
     delay(10);
  Serial.println("\nI2C Scanner");
}


void loop() {
  byte error, address;
  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;
  for(address = 1; address < 127; address++ ) 
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    WIRE.beginTransmission(address);
    error = WIRE.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");

      nDevices++;
    }
    else if (error==4) 
    {
      Serial.print("Unknown error at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");

  delay(5000);           // wait 5 seconds for next scan
}

Load and run that sketch as you would any other sketch onto your Arduino board.

Specify Alternate I2C Bus

The example sketch above uses the default Wire bus. To run the I2C scan on a different bus, change this line of code:

#define WIRE Wire

to specify the bus to use. For example, to use Wire1, change the line to this:

#define WIRE Wire1

** Arduino DUE Caution **

Note that the Arduino DUE places Wire1 where most other boards place Wire. Be sure to change scanner sketch code as needed.

Normal Behavior

If all goes well, you should get a list of addresses for each device found. In the example below, an Adafruit BMP280 breakout is attached to an Arduino UNO.

The BMP280's I2C address of 0x77 shows up as expected.

Missing Device or Pull Ups

If the device is disconnect and/or the pull up resistors are missing, the results will look like this:

Note that the sketch runs just fine, but reports no addresses.

A mis-wired board may hang the sketch after the "Scanning..." text is printed

This guide was first published on Sep 16, 2021. It was last updated on Sep 16, 2021.

This page (Arduino) was last updated on May 20, 2022.

Text editor powered by tinymce.