Bird
0
0
Arduinoprogramming~5 mins

I2C scanner sketch in Arduino

Choose your learning style9 modes available
Introduction

An I2C scanner sketch helps you find devices connected to your Arduino's I2C bus. It tells you the addresses of all connected I2C devices so you know where to talk to them.

You want to check if your sensor or module is properly connected via I2C.
You are unsure about the I2C address of a new device you connected.
You want to find all I2C devices connected to your Arduino for troubleshooting.
You want to avoid address conflicts between multiple I2C devices.
You want to verify wiring and connections before writing code to communicate.
Syntax
Arduino
for (byte address = 1; address < 127; address++) {
  Wire.beginTransmission(address);
  byte error = Wire.endTransmission();
  if (error == 0) {
    // Device found at this address
  }
}

This loop tries to communicate with every possible I2C address from 1 to 126.

If the device responds without error, it means a device is present at that address.

Examples
This example initializes I2C and prints the address of each found device in hexadecimal.
Arduino
Wire.begin();
Serial.begin(9600);
for (byte address = 1; address < 127; address++) {
  Wire.beginTransmission(address);
  byte error = Wire.endTransmission();
  if (error == 0) {
    Serial.print("Device found at address 0x");
    Serial.println(address, HEX);
  }
}
This example scans a smaller range of addresses and prints them in decimal.
Arduino
Wire.begin();
Serial.begin(9600);
for (byte address = 8; address < 120; address++) {
  Wire.beginTransmission(address);
  byte error = Wire.endTransmission();
  if (error == 0) {
    Serial.print("Found device at ");
    Serial.println(address);
  }
}
Sample Program

This complete sketch scans all possible I2C addresses and prints each found device's address in hexadecimal format. It also prints how many devices were found or if none were found.

Arduino
#include <Wire.h>

void setup() {
  Wire.begin();
  Serial.begin(9600);
  while (!Serial); // Wait for Serial to be ready
  Serial.println("I2C Scanner starting...");

  byte count = 0;
  for (byte address = 1; address < 127; address++) {
    Wire.beginTransmission(address);
    byte error = Wire.endTransmission();
    if (error == 0) {
      Serial.print("I2C device found at address 0x");
      if (address < 16) Serial.print("0");
      Serial.println(address, HEX);
      count++;
    }
  }
  if (count == 0) {
    Serial.println("No I2C devices found.");
  } else {
    Serial.print(count);
    Serial.println(" device(s) found.");
  }
}

void loop() {
  // Nothing to do here
}
OutputSuccess
Important Notes

Make sure your I2C devices are powered and connected correctly before running the scanner.

Some devices may not respond to scanning if they require special initialization.

The scanner uses the Wire library, which is standard for Arduino I2C communication.

Summary

An I2C scanner helps find all devices connected to the I2C bus by checking each address.

It prints the addresses of devices found so you can use them in your programs.

Running this sketch is a quick way to check wiring and device presence.