Raspberry Pi Pico: I2C Scanner (Arduino IDE) – Finding the Address of I2C Devices

This is a quick guide that shows how to find the address of I2C devices with the Raspberry Pi Pico programmed using Arduino IDE.

Raspberry Pi Pico I2C Scanner Arduino IDE Finding the Address of I2C Devices

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 ControllerSDA GPIOsSCL GPIOs
I2C0GPIO0, GPIO4, GPIO8, GPIO12, GPIO16, GPIO20GPIO1, GPIO5, GPIO9, GPIO13, GPIO17, GPIO21
I2C1GPIO2, GPIO6, GPIO10, GPIO14, GPIO18, GPIO26GPIO3, 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);          
}

View raw code

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.

Raspberry Pi Pico I2C Scanner Serial Monitor

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:

Thanks for reading.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Enjoyed this project? Stay updated by subscribing our newsletter!

2 thoughts on “Raspberry Pi Pico: I2C Scanner (Arduino IDE) – Finding the Address of I2C Devices”

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.