1. Overview
2. What is I2C Communication Protocol
3. I2C Pins in Raspberry Pi Pico
- Features of Raspberry Pi Pico I2C Pins
4. How to use I2C Pins of Raspberry Pi Pico with I2C Sensors or Modules?
5. Raspberry Pi Pico I2C Scanner Code
OverviewIn this tutorial, we will learn how to use I2C Pins in Raspberry Pi Pico & go through the I2C Scanner Code. The Raspberry Pi Pico is built using an RP2040 microcontroller. The board exposes 26 multi-function GPIO pins from a total of 36 GPIO pins. The 10 GPIO Pins are not exposed, hence they can’t be used. Out of the 26 usable GPIO pins, there are 2 pairs of I2C Pins which will be discussed in this post.
We will see what is I2C Communication protocol and how does it work. We will also learn in detail about the I2C Pin of Raspberry Pi Pico. We will take I2C Scanner Code as an example and check how the I2C Address of a few I2C enabled sensors & modules. Before that, you can check our Raspberry Pi Pico Getting Started Tutorial to learn more about the module.
What is I2C Communication ProtocolI2C (Inter-Integrated Circuit) is a synchronous, multi-master, multi-slave, packet-switched, single-ended, serial communication bus. It is widely used for attaching lower-speed peripheral ICs to processors and microcontrollers in short-distance, intra-board communication.
Like UART communication, I2C only uses two wires to transmit data between devices.
The two wires are named Serial Clock Line (SCL) and Serial Data Line (SDA). The data to be transferred is sent through the SDA wire and is synchronized with the clock signal from SCL. All the devices/ICs on the I2C network are connected to the same SCL and SDA lines as shown above.
The devices connected to the I2C bus are either masters or slaves. At any instant of time, only a single master stays active on the I2C bus. It controls the SCL clock line and decides what operation is to be done on the SDA data line.
All the devices that respond to instructions from this master device are slaves. For differentiating between multiple slave devices connected to the same I2C bus, each slave device is physically assigned a permanent 7-bit address.
When a master device wants to transfer data to or from a slave device, it specifies this particular slave device address on the SDA line and then proceeds with the transfer. So Effective communication takes place between the master device and a particular slave device. All the other slave devices don’t respond unless their address is specified by the master device on the SDA line.
I2C Pins in Raspberry Pi PicoThe microcontroller RP2040 chip has two I2C controllers. You can access both I2C controllers through GPIO pins of Raspberry Pi Pico. The following table shows the connection of GPIO pins with both I2C controllers.
Each connection of the controller can be configured through multiple GPIO pins as shown in the figure. But before using an I2C controller, you should configure in software which GPIO pins you want to use with a specific I2C controller.
Features of Raspberry Pi Pico I2C PinsThe Raspberry Pi Pico has RP2040 Chip which supports the following features:1. Device can work in Master or Slave Mode with a default salve address = 0x055
2. I2C Pins have 3 speed modes: Standard (0 to 100 Kb/s), Fast(<= 400 Kb/s) & Fast Plus mode (<= 1000 Kb/s)
3. It can both transmit and Receive Buffers
4. It can also be used in interrupt and DMA mode
How to use I2C Pins of Raspberry Pi Pico with I2C Sensors or Modules?Now let us learn how we can use the I2C Pin of Raspberry Pi Pico with any other I2C Based Sensors or Modules. In this case, we can use Raspberry Pi Pico as a Mater Device & other external sensors or modules as a Slave Device.
Here is a circuit where we have attached 3 different I2C Devices to the Pico Board. The 3 devices are BME680 Sensor, MPU6050 Sensor & 0.96″ OLED Display.
In this example, we will connect the SDA & SCL pin of MPU6050, BME680 & OLED Display to the common I2C line of Raspberry Pi Pico. Since there are multiple I2C Pins, we will use only a single pair of I2C pins of Raspberry Pi Pico. We will use GPIO8 as SDA0 & GPIO9 as SCL0.
Now let us see the Raspberry Pi Pico I2C Scanner Code. The code is written in MicroPython. You can either use Thonny IDE or uPyCraft IDE to connect the Raspberry Pi Pico to your computer.
The following code will scan the I2C Address of all the Sensors connected to the I2C pin of Raspberry Pi Pico. Copy the code and then Download & Run.
import machine
sda=machine.Pin(8)
scl=machine.Pin(9)
i2c=machine.I2C(0,sda=sda, scl=scl, freq=400000)
print('Scan i2c bus...')
devices = i2c.scan()
if len(devices) == 0:
print("No i2c device !")
else:
print('i2c devices found:',len(devices))
for device in devices:
print("Decimal address: ",device," | Hexa address: ",hex(device))
Once you run the code, the Micropython will try scanning the I2C devices connected to Pico Board. You can connect up to 127 I2C Slave devices. The device will scan the address and will show it in the Shell Window.
- The I2C Address of OLED Display is 60 which in Hexadecimal is 0x3C2.
- The I2C Address of MPU6050 is 104 which in Hexadecimal is 0x683.
- The I2C Address of BME680 is 119 which in Hexadecimal is 0x77
OriginalPost: https://how2electronics.com/how-to-use-i2c-pins-in-raspberry-pi-pico-i2c-scanner/
Mr. Alam
Comments