This is a quick guide to show you how to find the address of I2C devices with the Raspberry Pi Pico programmed using MicroPython firmware.
Already familiar with the Raspberry Pi Pico? Jump to the I2C scanner sketch.
Prerequisites – MicroPython Firmware
To follow this tutorial you need MicroPython firmware installed in your Raspberry Pi Pico board. You also need an IDE to write and upload the code to your board.
The recommended MicroPython IDE for the Raspberry Pi Pico is Thonny IDE. Follow the next tutorial to learn how to install Thonny IDE, flash MicroPython firmware, and upload code to the board.
Raspberry Pi Pico – Default I2C Pins
In the Raspberry Pi Pico, there are two I2C peripherals available, named I2C1 and I2C0. You can use two different buses (I2C0 and I2C1) simultaneously, but you can’t use two I2C1 or two I2C2 at the same time. You can use I2C communication on any of the I2C pins available.
The default I2C pins are GPIO 4 (SDA) and GPIO 5 (SCL) – read our Raspberry Pi Pico Pinout.
The following table shows all the pins you can use for I2C communication.
I2C Controller | SDA GPIOs | SCL GPIOs |
I2C0 | GPIO0, GPIO4, GPIO8, GPIO12, GPIO16, GPIO20 | GPIO1, GPIO5, GPIO9, GPIO13, GPIO17, GPIO21 |
I2C1 | GPIO2, GPIO6, GPIO10, GPIO14, GPIO18, GPIO26 | GPIO3, GPIO7, GPIO11, GPIO15, GPIO19, GPIO27 |
I2C Scanner – MicroPython
If you want to find the I2C address of a specific sensor, display, or any other I2C peripheral, connect it to the Raspberry Pi Pico I2C pins and then run the I2C scanner code provided.
Open Thonny IDE, or the IDE of your choice, and copy the following code.
# I2C Scanner MicroPython
from machine import Pin, SoftI2C
# You can choose any other combination of I2C pins
i2c = SoftI2C(scl=Pin(5), sda=Pin(4))
print('I2C SCANNER')
devices = i2c.scan()
if len(devices) == 0:
print("No i2c device !")
else:
print('i2c devices found:', len(devices))
for device in devices:
print("I2C hexadecimal address: ", hex(device))
After copying the code:
- connect the Raspberry Pi Pico to your computer, if it isn’t already;
- make sure you have your I2C peripheral properly connected to your board on the right I2C pins (SCL=GPIO5 ; SDA =GPIO4). If you’re using different pins, make sure you adjust that on the code.
i2c = SoftI2C(scl=Pin(5), sda=Pin(4))
- run the previous code (I2C scanner sketch). If you’re using Thonny IDE, you just need to click on the green run icon.
The I2C address of your peripheral will be printed on the shell.
If you have more than one device connected to the same I2C bus, it will display the address of all devices.
Wrapping Up
In this guide, you learned how to quickly find the address of I2C devices. You just need to connect your I2C peripheral to the Raspberry Pi Pico and run the I2C scanner code.
We hope you’ve found this guide useful.
Learn more about the Raspberry Pi Pico with our resources:
We also have guides for other popular microcontroller boards:
- ESP32 Projects and Tutorials
- ESP8266 Projects and Tutorials
- MicroPython Projects with the ESP32 and ESP8266
- Raspberry Pi Projects and Guides
Thanks for reading.
May I ask you if you probably did you made a little mistake.
Please check
… two different buses (I2C1 and I2C2) simultaneously,
Did you probably mean I2C0 and I2C1?
Kind regards
Christian
Hi.
Yes. You’re right.
Thanks for pointing that out.
It’s already fixed.
Regards,
Sara