Sometimes you need to know the addresses of your I2C devices. The picture above shows an Arduino UNO R4 Wifi mounted on a look-like Plug-and-Make base board together with three I2C modules. Using the UNO R4 WiFi, you have two buses: bus-0 (pins A4 & A5) and bus-1 (the Qwiic connector). This program scans both buses and displays its results using the built-in LED matrix. To run this code, you also need the library that displays HEX numbers on the LEDs (see attachments).
/*
Originally written by Nick Gammon, 20.04.2011
this one for Arduino UNO R4 Wifi
Scans the I2C-addresses of bus 0 and bus 1
and shows the hits on the LED-matrix .
see also
https://docs.arduino.cc/tutorials/uno-r4-wifi/cheat-sheet/
Inside void setup()
you need to initialize the library,
and initialize the I2C port you want to use.
Wire.begin() //SDA & SDL
Wire1.begin(); //SDA1 & SDL1
Wire2.begin(); //SDA2 & SDL2
The Qwiic connector on the UNO R4 WiFi is
connected to the secondary I2C bus (IIC0),
which uses the Wire1 object rather than the
Wire object.
Please note that the Qwiic connector is 3.3 V only.
Mind you:
After scanning the bus three times
only zeroes will be reported
(reason unknown).
Wire.end() does not help
only pressing RESET.
*/
#include "Arduino_LED_Matrix.h"
ArduinoLEDMatrix matrix;
#include <WiFiMatrix3dig.h>
#include <Wire.h>
boolean dev[2][128];
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println(__FILE__);
scanBus(Wire, 0);
scanBus(Wire1, 1);
matrix.begin();
Serial.println("press RESET to rescan");
}
void loop() {
for (int i = 0; i < 2; i++)
for (int j = 1; j < 128; j++)
if (dev[i][j]) {
show3digHex(matrix, 0x100 * i + j);
delay(2000);
}
}
void scanBus(TwoWire wire, int nr) {
Serial.print("I2C scanner. Scanning bus ");
Serial.println(nr);
Serial.print(" ");
byte count = 0;
wire.begin();
for (byte i = 1; i < 128; i++) {
if (i % 16 == 0) Serial.println();
wire.beginTransmission(i);
boolean b = (wire.endTransmission() == 0);
dev[nr][i] = b;
if (b) {
if (i < 16) Serial.print(0);
Serial.print(i, HEX);
Serial.print(" ");
count++;
}
else
Serial.print(".. ");
}
Serial.println("\ndone.");
if (count > 0) {
Serial.print("Found ");
Serial.print(count);
Serial.println(" device(s).");
}
else
Serial.println("did you swap CLK and DIO?");
}
This is device 0x51 on bus-0:
This is device 0x6A on bus-1:
The scan is run only once. If you add or remove any devices you have to press RESET to start a new scan.
Comments
Please log in or sign up to comment.