I2C Scanner Using Arduino with Code

Posted on

I2C Scanner

I2C, or Inter-Integrated Circuit, is a serial communication protocol that allows devices to communicate with each other over a two-wire bus. This bus is made up of a clock line (SCL) and a data line (SDA), and devices connected to the bus are assigned a unique address. One of the tools commonly used to debug and troubleshoot I2C communication issues is an I2C scanner.

An I2C scanner is a program that scans the I2C bus for connected devices and detects their addresses.

To use an this scanner, the microcontroller or microprocessor that is connected to the I2C bus must be programmed to send out a series of addresses. The scanner then checks if an acknowledge signal is received from a device at that address. If an acknowledge signal is received, it indicates that a device is present at that address.

This scanner can be used to verify that all the devices on the bus are responding as expected, or to identify the addresses of devices that have been added or removed from the bus. It can also be used to detect address conflicts, which occur when two or more devices have the same address.

Wiring

With Arduino, we can easily get the device address with a program. Before we go to the program code, please connect the I2C device to the Arduino with the following simple configuration:

I2C Address Scanner Device Using Arduino

Code

After you connect the I2C device to the Arduino, it’s time to check the address. The following is the I2C Scanner program code, please upload it to your Arduino board. Program Code:

#include <Wire.h>

void setup() {
  Serial.begin (9600);
  while (!Serial) 
    {
    }
  Serial.println ();
  Serial.println ("Search I2C Address ...");
  byte count = 0;
  
  Wire.begin();
  for (byte i = 8; i <120; i++)
  {
    Wire.beginTransmission (i);
    if (Wire.endTransmission () == 0)
      {
      Serial.print ("Address is: ");
      Serial.print ("0x");
      Serial.println (i, HEX);
      count++;
      delay (1);  
      } 
  } 
  Serial.println ("Done.");
  Serial.print ("Found ");
  Serial.print (count, DEC);
  Serial.println (" Device.");
} 
void loop() {}

After the upload is complete, now open the Serial Monitor in your Arduino IDE. In this case, I connect a 20×4 I2C LCD and after finishing doing Scanner, I got the result that the address of the LCD I was using was 0x3F.

In summary, the I2C scanner is a simple yet efficient tool for debugging and troubleshooting I2C communication issues. It helps to detect the device addresses connected to the bus and also verifies if all the devices on the bus are responding as expected. I hope this article is useful. Done.