Skip to content

Instantly share code, notes, and snippets.

@vurdalakov
Last active August 14, 2025 06:14
Show Gist options
  • Save vurdalakov/7e2aca81c067b879b906513838cf9af9 to your computer and use it in GitHub Desktop.
Save vurdalakov/7e2aca81c067b879b906513838cf9af9 to your computer and use it in GitHub Desktop.
A MicroPython program that scans all available I2C bus configurations on the Raspberry Pi Pico (RP2040 and RP2350), cycling through high, standard, and low frequencies to detect and list connected I2C devices.
# A MicroPython program that scans all available I2C bus configurations on the Raspberry Pi Pico (RP2040 and RP2350),
# cycling through high, standard, and low frequencies to detect and list connected I2C devices.
# https://gist.github.com/vurdalakov/7e2aca81c067b879b906513838cf9af9
from machine import Pin, I2C
count = 0
# RP2040 and RP2350A have 30 GPIOs; RP2350B has 48 GPIOs
for i in range(0, 48, 2):
if i in (22, 24, 28):
# GPIOs 23, 24, 25, and 29 are utilized internally on the Pico board
continue
i2c_id = int(i % 4 / 2)
sda_pin = i
scl_pin = i + 1
for i2c_freq in (400000, 100000, 10000, 1000):
print(f"--- I2C {i2c_id}, SDA {sda_pin}, SCL {scl_pin}, {i2c_freq:6d} Hz", end="\r")
try:
i2c = I2C(id=i2c_id, sda=Pin(sda_pin), scl=Pin(scl_pin), freq=i2c_freq)
except ValueError:
continue
device_addresses = i2c.scan()
if len(device_addresses) > 0:
print()
for device_address in device_addresses:
print(f"({count}) hex: 0x{device_address:02X}, dec: {device_address:3d}")
count = count + 1
break
print(' '*40, end="\r")
print(f"=== I2C devices found: {count}")
--- I2C 1, SDA 6, SCL 7, 400000 Hz
(0) hex: 0x57, dec: 87
(1) hex: 0x68, dec: 104
=== I2C devices found: 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment