This is a quick guide that shows how to find the address of I2C devices with the Raspberry Pi Pico programmed using Arduino IDE.
Already familiar with the Raspberry Pi Pico? Jump to the I2C scanner sketch.
Prerequisites
This tutorial uses Raspberry Pi Pico programmed with Arduino IDE. You need to install the Raspberry Pi Pico boards on Arduino IDE and you must know how to upload code to the board. Check out the following tutorial first if you haven’t already:
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 (I2C1 and I2C2) 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 Sketch – Arduino IDE
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 sketch provided.
Copy the following code to the Arduino IDE and upload it to the Raspberry Pi Pico.
/*********
Rui Santos
Complete project details at https://RandomNerdTutorials.com/raspberry-pi-pico-i2c-scanner-arduino/
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*********/
#include <Wire.h>
void setup() {
Wire.begin();
Serial.begin(115200);
Serial.println("\nI2C Scanner");
}
void loop() {
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; 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.println(address,HEX);
nDevices++;
}
else if (error==4) {
Serial.print("Unknow 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);
}
After uploading the code, 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. Replace:
Wire.begin();
With the following line, in which I2C_SDA corresponds to the GPIO number you’re using as SDA and I2C_SCL to the GPIO number you’re using as SCL.
Wire.begin(I2C_SDA, I2C_SCL);
Open the Serial Monitor at a baud rate of 115200.
Wait a couple of seconds, and the I2C address of your peripheral will be printed in the Serial Monitor. 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 upload the I2C scanner sketch provided.
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.
Thank you, works great!
You’re welcome:)