When purchasing a I2C device, the seller would send along the Hex address that is needed to established a communication with the device. Unfortunately sometimes the seller does not know the Hex address or the address received is not the correct address. The best thing to do is to scan for the address. This way there’s no guessing on the correct address.
I’ve put some code together to probe for the address of the I2C devices (up to 127). It uses the Wire.beginTransmission and the Wire.endTransmission command. The Wire.endTransmission is set to send a “0” byte when it closes a transmission successfully so it’ll be captured and used to determine if an address is present or not.
Open up Arduino and then copy/past the code below into your new sketch. Make sure your I2C devices one at a time. You may connect up to 127 devices but it will be hard to determine with hex address goes to which device. Just leave one connected and determine that one first before moving to the next one.
Here’s the code:
//I2C Address Scanner
//Used to find the Hex address of the I2C device
//By using the Wire.beginTransmission and Wire.endTransmission
//We can determine if an address exist or not. Address that exist will send
//back a 0 byte when releasing
#include <Wire.h>
byte returnbyte; //Set returnbyte to byte
byte address; //Set address to byte
int totaladdress; //Set totaladdress to integer
void setup() {
Serial.begin(9600);//set baud rate for Serial.begin()
Wire.begin();//Initialise wire library
}
void loop() {
totaladdress = 0;
Serial.println("Address Scanning in Progress");
for (address = 1; address < 127; address++ ) {//Start with address 1 and increment to 127 testing one address at a time
Wire.beginTransmission(address);//Communicate with address 1 and then 2 and then 3 and so on
returnbyte = Wire.endTransmission();//stores the returned byte if the hex address exist.
//The returned byte from the Wire.endTranmission will return a "2" for received no acknowledgement on transmit of address
//and then when closing, it will send a "0" which will be captured in the variable returnbyte
if (returnbyte == 0) {//When the Wire.endTransmission closes connection, it sends a 0
Serial.print("I2C address found at 0x");
if (address<16) //Condition to determine if the address is less than 16. If so, put a leading 0
Serial.print("0");//Print the leading 0 if less than 16
Serial.println(address,HEX);//Print the address
totaladdress++;//Increment device
}
else if (returnbyte==4) {//Check if the returned byte is equal to 4 (other)
Serial.print("Wire.endTransmission sent Error 4 at address 0x");
if (address<16) //Condition to determine if the address is less than 16. If so, put a leading 0
Serial.print("0");//Print the leading 0 if less than 16
Serial.println(address,HEX);//Print the address
}
}
if (totaladdress == 0) {//If total address is 0, then print the message "No hex address found" return
Serial.println("No hex address found\n");
}
else {//otherwise print "Finish Scanning"
Serial.println("Finish Scanning\n");
}
delay(3000); // wait 5 seconds for next scan
}
Get the latest from my blog when you sign up. Special offers are only offered to those who sign up. Specific DIY are emailed to users when they become available. Sign up today to get notified.
This works great.
So easy and straight forward.
Really helps eliminate guess at the I2C hex address.
Thanks