I2C Address Hunt

This little routine is usable on any Arduino system. It scans all possible I2C addresses and tells you which are active. This is a great way to check if you wired your bus correctly (I always switch the data and clock pins the first time I wire it.) It also tells you what is out there. For instance, I use a 4-line 20-char LCD display in many of my projects. It has an I2C interface. But each display can have one of two I2C addresses, and I need to know which address my current display uses before I initialize it. This routine tells me what I need to know.

/*****************************************************
I2C_AddressHunt is a utility for exploring the I2C bus of an Arduino system.

There are many sources of information on how I2C works on YouTube and/or on the web in general.
Therefore I will not attempt any detailed explaination here. But here it is in a nutshell.

In an I2C bus there is one “master” and as many “slaves” as you please. Of course, the Arduino
is the master in most cases. The Master will send out the address of the slave it wishes to command
and then wait for an acknowledgement before sending the rest of the request. The master will then
wait for for a response, if one is expected.

I2C addresses are eight bits long, therefor they can range from 0 to 127. Address 0 is reserved
and should not be used, leaving addresses 1 through 127 to be tested. This sketch will try to
communicate with each of those addresses, and send a message to the serial port if an ackmowledgement
if received.

This sketch can be used first to verify that your I2C connections are correct, and second to discover
the addresses of any slaves attached to the bus. This is a tool that should be in the tool kit of any
Arduino experimenter.

The original version of this sketch is something I found and saved a long time ago. I did not write it.
********************************************************/

#include <Wire.h> // Make sure Wire.h is loaded as a library

void setup()
{
Wire.begin(); // Initialize the I2C software
Serial.begin(9600); // Initialize the COM port
Serial.println("\nI2C Scanner"); // Send a “hello” message to the serial port
}

void loop()
{
byte error, address; // Set up some local variables
int nDevices;

Serial.println(“Scanning…”);
nDevices = 0;
for(address = 1; address < 127; address++ ) // Step through addresses 1 to 127
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address); // Set up an empty packet to that address
error = Wire.endTransmission(); // Send that packet and store the result
if (error == 0) // Result 0 means the packet was acknowledged
{
Serial.print(“I2C device found at address 0x”); // Start sending a message
if (address<16) // Prepend a ‘0’ is the address is one digit
Serial.print(“0”);
Serial.print(address,HEX); // Report the address
Serial.println(" !");
nDevices++;
}
else if (error==4) // Questionable results
{
Serial.print(“Unknow error at address 0x”); // Report this
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
}

1 Like