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.
I2C scanner sketch in 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.
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); } }
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); } }
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.
#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 }
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.
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.
