Every device with an integrated i2c protocol has an address associated with it. Many devices such as OLED i2C display screen usually utilize the default address of 0x27 where 0x shows the hex format of the numbers. But the address can be different in some cases. This address depends on the position of pins correlated with A0, A1, and A2 on the I2C controller on this device.
The Raspberry Pi PICO i2C
The Raspberry Pi integrates i2C controller this controller is accessible through GPIO pins of RPI-PICO. See the below schematics for the pin location. However, this connection of the master controller can be configured through multiple GPIO pins set by your code, and each i2c device that attaches to the GPIO pins should have a unique communication address, using the code below we can search and look which i2c address communicates to our i2c master controller
Scanning the i2C address with MicoPython
You just have to copy the code below and upload it to your favorite development board that supports MicroPython make sure that your i2c-supported devices and modules are already hooked to your development board such Raspberry Pi PICO. The code will scan all any i2c supported devices connected to your development board and it will determine the number of devices utilizing an i2C address on the console.
Source Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import machine sdaPIN=machine.Pin(0) sclPIN=machine.Pin(1) i2c=machine.I2C(0,sda=sdaPIN, scl=sclPIN, freq=400000) devices = i2c.scan() if len(devices) == 0: print('14CORE - i2c Finder / Scanner ') print("Error: No i2c device found, check properly the wiring!") else: print('14CORE - i2c Finder / Scanner ') print('i2c devices found:',len(devices)) for device in devices: print("i2C Address: ",hex(device)) |
Downloads
- View Raspberry Pi – PICO – Pin-out Diagram
- View Raspberry Pi PICO – Schematics
- View Raspberry Pi PICO – Datasheet
I’ve tried the code and the result is wrong:
Scan i2c bus…
14CORE – i2c Finder / Scanner
i2c devices found: 112
i2C Address: 0x8
i2C Address: 0x9
i2C Address: 0xa
i2C Address: 0xb
……
and so on.
I’ve only one device connected, how is it possible?
Which device you attach to your host? some devices/ sensor has multiple address to talk.
I think it’s an hardware problem, with a different Pico it works.
Getestet und funktioniert, mit PICO
Sie müssen nur den folgenden Code kopieren und auf Ihr bevorzugtes Entwicklungsboard hochladen, das MicroPython unterstützt. Stellen Sie sicher, dass Ihre i2c-unterstützten Geräte und Module bereits an Ihr Entwicklungsboard wie Raspberry Pi PICO angeschlossen sind.
Plötzlicher Verbindungsabbruch, Fehler beim Verbindungsaufbau, evtl. Verkabelungsfehler