Skip to content

Raspberry Pi Pico I2C

The Pico has two I2C hardware controllers. Each controller can talk to multiple IIC devices as long as all the devices communicating on each controller have distinct addresses.

  1. I2C0 SDA are on GPIOs 0, 4, 8, 12, 16 and 20
  2. I2C0 SCL are on GPIOs 1, 5, 9, 13, 17 and 21
  3. I2C1 SDA are on GPIOs 2, 6, 10, 14, 18 and 26
  4. I2C1 SCL are on GPIOs 3, 7, 11, 15, 19 and 27

I2C Scanner for I2C 0

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import machine

I2C_SDA_PIN = 0
I2C_SCL_PIN = 1
i2c=machine.I2C(0,sda=machine.Pin(I2C_SDA_PIN), scl=machine.Pin(I2C_SCL_PIN), freq=400000)

print('Scanning I2C bus.')
devices = i2c.scan() # this returns a list of devices

device_count = len(devices)

if device_count == 0:
    print('No i2c device found.')
else:
    print(device_count, 'devices found.')

for device in devices:
    print('Decimal address:', device, ", Hex address: ", hex(device))

I2C Scanner for Both I2C 0 and 1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import machine

I2C0_SDA_PIN = 0
I2C0_SCL_PIN = 1
I2C1_SDA_PIN = 2
I2C1_SCL_PIN = 3
i2c0=machine.I2C(0,sda=machine.Pin(I2C0_SDA_PIN), scl=machine.Pin(I2C0_SCL_PIN), freq=400000)
i2c1=machine.I2C(1,sda=machine.Pin(I2C1_SDA_PIN), scl=machine.Pin(I2C1_SCL_PIN), freq=400000)

print('Scanning I2C bus 0.')
devices = i2c0.scan() # this returns a list of devices

device_count = len(devices)

if device_count == 0:
    print('No i2c device found on bus 0.')
else:
    print(device_count, 'devices found.')

for device in devices:
    print('Decimal address:', device, ", Hex address: ", hex(device))

print('Scanning I2C bus 1.')
devices = i2c1.scan() # this returns a list of devices

device_count = len(devices)

if device_count == 0:
    print('No i2c device found on bus 1.')
else:
    print(device_count, 'devices found.')

for device in devices:
    print('Decimal address:', device, ", Hex address: ", hex(device))

Results for both a OLED display on I2C 0 and a time-of-flight sensor on I2C 1

1
2
3
4
5
6
Scanning I2C bus 0.
1 devices found.
Decimal address: 60 , Hex address:  0x3c
Scanning I2C bus 1.
1 devices found.
Decimal address: 41 , Hex address:  0x29

References

  1. Article on Hackster.io