0
0
AutocadHow-ToBeginner · 3 min read

How to Scan I2C Devices Using Arduino: Simple Guide

To scan I2C devices using Arduino, use the Wire library to try connecting to all possible addresses from 1 to 127. Devices that respond will show their address, which you can print to the Serial Monitor for easy identification.
📐

Syntax

The basic syntax to scan I2C devices involves using the Wire.beginTransmission(address) and Wire.endTransmission() functions. You loop through all possible I2C addresses (1 to 127) and check if a device acknowledges communication.

  • Wire.beginTransmission(address): Starts communication with a device at the given address.
  • Wire.endTransmission(): Ends communication and returns 0 if a device responded.
  • Loop through addresses to find all connected devices.
arduino
for (byte address = 1; address < 127; address++) {
  Wire.beginTransmission(address);
  byte error = Wire.endTransmission();
  if (error == 0) {
    // Device found at this address
  }
}
💻

Example

This example scans all I2C addresses and prints the addresses of detected devices to the Serial Monitor. It helps you find the address of any connected I2C device.

arduino
#include <Wire.h>

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

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

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

  for(address = 1; address < 127; 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++;
    }
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("Done scanning\n");

  delay(5000); // Wait 5 seconds before next scan
}
Output
I2C Scanner starting... Scanning I2C bus... I2C device found at address 0x3C ! Done scanning
⚠️

Common Pitfalls

Common mistakes when scanning I2C devices include:

  • Not calling Wire.begin() before scanning, which initializes the I2C bus.
  • Using incorrect baud rate for Serial Monitor, causing unreadable output.
  • Not waiting for Serial Monitor to open before printing, missing initial messages.
  • Devices with unusual addresses or multiple devices on the bus causing conflicts.
  • Wiring errors like missing pull-up resistors or wrong SDA/SCL pins.

Always check wiring and use the correct pins for your Arduino model.

arduino
/* Wrong way: Missing Wire.begin() */

void setup() {
  Serial.begin(9600);
  // Wire.begin(); // This is missing
}

void loop() {
  for (byte address = 1; address < 127; address++) {
    Wire.beginTransmission(address); // This will fail
    byte error = Wire.endTransmission();
    if (error == 0) {
      Serial.print("Found device at ");
      Serial.println(address, HEX);
    }
  }
  delay(5000);
}

/* Right way: Add Wire.begin() in setup() */
📊

Quick Reference

  • Use Wire.begin() in setup() to start I2C.
  • Scan addresses from 1 to 127 to find devices.
  • Check Wire.endTransmission() returns 0 to confirm device presence.
  • Use Serial Monitor at 9600 baud to see results.
  • Ensure correct wiring with pull-up resistors on SDA and SCL lines.

Key Takeaways

Always call Wire.begin() before scanning I2C devices.
Loop through addresses 1 to 127 and check for devices with Wire.endTransmission() == 0.
Use Serial Monitor at 9600 baud to view detected device addresses.
Check wiring carefully, including SDA, SCL pins and pull-up resistors.
Scanning helps identify device addresses for further communication.