How to Use I2C on Raspberry Pi Pico: Simple Guide
To use
I2C on Raspberry Pi Pico, import the machine module and create an I2C object specifying the pins and frequency. Then use methods like scan(), readfrom(), and writeto() to communicate with I2C devices.Syntax
To use I2C on Raspberry Pi Pico with MicroPython, you first import the machine module. Then create an I2C object with parameters for the bus ID, pins for SDA and SCL, and the communication frequency.
- id: I2C bus number (usually 0 or 1)
- sda: Pin object for data line
- scl: Pin object for clock line
- freq: Communication speed in Hertz (e.g., 100000 for 100kHz)
Use methods like scan() to find devices, readfrom(addr, nbytes) to read data, and writeto(addr, buf) to send data.
python
from machine import Pin, I2C i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=100000) devices = i2c.scan() # Returns list of device addresses # Read 2 bytes from device at address 0x40 # data = i2c.readfrom(0x40, 2) # Write bytes to device at address 0x40 # i2c.writeto(0x40, b'\x01\x02')
Example
This example shows how to initialize I2C on pins GP0 (SDA) and GP1 (SCL), scan for connected devices, and print their addresses in hexadecimal format.
python
from machine import Pin, I2C import time i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=100000) while True: devices = i2c.scan() if devices: print('I2C devices found:') for device in devices: print(' - Address: 0x{:02X}'.format(device)) else: print('No I2C devices found') time.sleep(5)
Output
No I2C devices found
# or if devices are connected:
I2C devices found:
- Address: 0x40
- Address: 0x68
Common Pitfalls
- Wrong pins: Make sure to use the correct GPIO pins for SDA and SCL (e.g., GP0 and GP1 for I2C0).
- Pull-up resistors: I2C lines need pull-up resistors (usually 4.7kΩ). Some breakout boards have them built-in; if not, add external resistors.
- Incorrect frequency: Use standard frequencies like 100kHz or 400kHz; too high may cause communication errors.
- Device address confusion: Use
scan()to find device addresses before reading or writing.
python
from machine import Pin, I2C i2c = I2C(0, sda=Pin(2), scl=Pin(3), freq=100000) # Wrong pins for I2C0 # Correct way: i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=100000)
Quick Reference
Here is a quick summary of key I2C methods on Raspberry Pi Pico using MicroPython:
| Method | Description |
|---|---|
| I2C(id, sda, scl, freq) | Create I2C object with bus ID, pins, and frequency |
| scan() | Return list of detected device addresses |
| readfrom(addr, nbytes) | Read bytes from device at address |
| writeto(addr, buf) | Write bytes to device at address |
| readfrom_mem(addr, memaddr, nbytes) | Read from device memory/register |
| writeto_mem(addr, memaddr, buf) | Write to device memory/register |
Key Takeaways
Use the machine.I2C class with correct pins and frequency to start I2C on Raspberry Pi Pico.
Always scan for device addresses before reading or writing to avoid errors.
Ensure SDA and SCL lines have pull-up resistors for reliable communication.
Use standard I2C frequencies like 100kHz for compatibility.
Double-check pin assignments to match the I2C bus you want to use.