Skip to content

Instantly share code, notes, and snippets.

@projetsdiy
Last active May 3, 2024 19:45
Show Gist options
  • Save projetsdiy/f4330be62589ab9b3da1a4eacc6b6b1c to your computer and use it in GitHub Desktop.
Save projetsdiy/f4330be62589ab9b3da1a4eacc6b6b1c to your computer and use it in GitHub Desktop.
Micropython i2c scanner
# Scanner i2c en MicroPython | MicroPython i2c scanner
# Renvoi l'adresse en decimal et hexa de chaque device connecte sur le bus i2c
# Return decimal and hexa adress of each i2c device
# https://projetsdiy.fr - https://diyprojects.io (dec. 2017)
import machine
i2c = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4))
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))
@425629
Copy link

425629 commented May 11, 2020

Very valuable means to show the first steps in setting up and accessing i2C resources. Other "1st steps" tutorials assumed Pin objects and abstractions that I hadn't learned yet. This gave the starter everything needed.

@aofek
Copy link

aofek commented Jun 4, 2021

Nice, Thanks
Avi

@urish
Copy link

urish commented Apr 13, 2022

Wokwi simulation of this code (with two connected I2C devices):

https://wokwi.com/projects/328854546483774035

image

@PrometheusWannaB3
Copy link

no idea if it's a new version thing or because I'm using a pi pico instead of esp32, but I had to use an extra argument for the I2C

added the 0 after I2C(

i2c = machine.I2C(0, scl=machine.Pin(9), sda=machine.Pin(8))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment