Posted on Leave a comment

Find out an I2C address with the Raspberry Pi Pico

Find I2C address with Raspberry Pi Pico

Quite some electronic parts use the I2C communication protocol to exchange information. To do this, we need to know the I2C address from these parts or peripherals . In this tutorial, we’ll see how to use a Raspberry Pi Pico to find out the I2C address of our connected peripheral.

  1. Prepare the hardware

    – First you need a computer to run Thonny. In this tutorial we’ll use a Raspberry Pi 4 as our computer. And Thonny is a user-friendly Python IDE to interact with the Raspberry Pi Pico board. If you never used Thonny to program the Raspberry Pi Pico, before continuing, you better have a look at our tutorial “How to start programming the Raspberry Pi Pico“.

    – Next you need an USB cable with micro-USB plug.

    – You also need a Raspberry Pi Pico of course.  And as we’ll connect another device to the Pico, we need pin headers soldered to the GPIO-pins of our board.

    And finally you’ll need some extra components :
    – a breadboard (we are using a 400 points breadboard)
    – an I2C bus compatible device (in this tutorial we are using a 16×2 LCD display)
    – Dupont jumper wires



    Raspberry Pi Pico breadboard I2C
    Visit our shop if you miss any components.

  2. Get to know the I2C basics

    I2C or I²C (Inter-Integrated Circuit) is a serial communication bus to allow data transfer between electronic components. Wtih this standardized type of communication we can drastically reduce the amount of wires between the 2 parts.

    In fact, besides the wires to power the device, the communication happens with 2 wires :
    – the clock wire, labeled with SCL
    – the data wire, labeled with SDA
    I2C bus logoAs multiple peripherals can be connected onto the same bus, each peripheral needs to have its own I2C address. We’ll use the device’s I2C address to exchange data between the Raspberry Pi Pico and the device. This I2C address is often hard-wired into the device. If you don’t have the device’s documentation, you can scan for it. And that’s exactly what we’ll do in this tutorial.

    If you need more information, it’s worth to spend some time reading the Wikipedia I²C webpage.

    Be careful ! Before starting to connect components to the GPIO pins of your Raspberry Pi Pico, make sure it is not connected to your computer.

  3. Connect your I2C device

    The Raspberry Pi Pico comes with 2 I2C buses. We can use several pins on each I2C bus. Nevertheless, be carefull. Depending of your script or library you use, it might be necessarry to use specific pins. In our case, we use following pins :
    – GPIO 0 for the SDA wire
    – GPIO 1 for the SDC wire.
    Raspberry Pi Pico pinout I2C pins
    Connect the SDA and SDC wires to the peripheral.

    And besides this, we have to power our device. Check if the voltage of your peripheral is 3.3V or 5V. Connect now the Raspberry Pi Pico pins : 3V3(OUT) or VSYS and GND to your peripheral.
    I2C pins

  4. Write the code

    The aim is to write a MicroPython script that will allow us to scan the I2C bus to have an overview of the connected devices with an I2C address. In our case, to find out a specific I2C address for a device, it’s good to have only one device connected of course.

    And because we often need the hexadecimal number, we’ll convert the decimal I2C address to a hexadecimal number too.

    Now, open Thonny and write or paste following code in the IDE:

    import machine
     
    sda=machine.Pin(0)
    scl=machine.Pin(1)
     
    i2c=machine.I2C(0,sda=sda, scl=scl, freq=400000)
     
    print('I2C address:')
    print(i2c.scan(),' (decimal)')
    print(hex(i2c.scan()[0]), ' (hex)')



    MicroPython Pico I2C

  5. Run the code

    Now, it’s time to save your script. You can either save it on your computer or on your Pico board.

    Then, click on the Run button of the Thonny IDE. The I2C address of your device will appear on the bottom of the screen.

    Thonny I2C-address

    If you get an error message instead of the I2C address, you better check the connections between your Raspberry Pi Pico and your device.

How useful was this post?

Click on a star to rate it!

Average rating 4.7 / 5. Vote count: 21

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

Your email address will not be published. Required fields are marked *

How useful was this post?

Click on a star to rate it!

Average rating 4.7 / 5. Vote count: 21

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

find out more products

Be the first to be informed about our latest tutorials and products by subscribing to our Newsletter

freva.com respects your privacy. Read our privacy policy on how we handle your personal information.