I added code to the Master I2C device: 2560 to find all the I2C devices. I don’t recommend leaving it running. I really slows down the device trying to find all 127 devices. I will be adding the I2C LEDs soon. I know I have a 16×2 and 16×4 in one of the boxes. You need to find the addresses to set them up.
// Include the required Wire library for I2C
#include
int x = 0;
int LED = 13;
void setup(){
// I2C counter for LEDs
pinMode (LED, OUTPUT);
// Start the I2C Bus as Master
Wire.begin();
// I2C counter for LEDs
// I2C Scanner Code
Serial.begin(9600);
while (!Serial); // wait for serial monitor
Serial.println(“\nI2C Scanner”);
// I2C Scanner Code
}
void loop() {
// I2C counter for LEDs
x++; // Increment x
//From Left to Right
// First UNO
Wire.beginTransmission(1); // transmit to device #1
Wire.write(x); // sends x
Wire.endTransmission(); // stop transmitting
//Second UNO
Wire.beginTransmission(2); // transmit to device #2
Wire.write(x); // sends x
Wire.endTransmission(); // stop transmitting
//Mega in the Middle
//Third UNO
Wire.beginTransmission(3); // transmit to device #3
Wire.write(x); // sends x
Wire.endTransmission(); // stop transmitting
//Fourth UNO
Wire.beginTransmission(4); // transmit to device #4
Wire.write(x); // sends x
Wire.endTransmission(); // stop transmitting
if ( (x == 3) || (x == 6)) {
digitalWrite(LED, HIGH);
}
else {
digitalWrite(LED, LOW);
}
if (x == 7) x = 0; // `reset x once it gets 6
delay(1000);
// I2C counter for LEDs
// I2C Scanner Code
byte error, address;
int nDevices;
Serial.println(“Scanning…”);
nDevices = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the 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++;
}
else if (error==4)
{
Serial.print(“Unknown error at address 0x”);
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
// I2C Scanner Code
}
As I add I2C devices to the bus, I need a way to identify the new device addresses to make sure no duplicate devices.
I2C Scanner
Scanning…
I2C device found at address 0x01 !
I2C device found at address 0x02 !
I2C device found at address 0x03 !
I2C device found at address 0x04 !
done