Skip to content

Instantly share code, notes, and snippets.

@jmparatte
Last active July 14, 2022 00:44
Show Gist options
  • Save jmparatte/834f7d6d002292333f44e030b285518d to your computer and use it in GitHub Desktop.
Save jmparatte/834f7d6d002292333f44e030b285518d to your computer and use it in GitHub Desktop.
i2cdetect.py
#!/usr/bin/env python3
"""
pi@raspberrypi:~ $ ./i2cdetect.py
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- 27 -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
"""
import smbus2
import errno
def i2cdetect():
bus_number = 1 # 1 indicates /dev/i2c-1
bus = smbus2.SMBus(bus_number)
device_count = 0
for device in range(0x00, 128):
if device==0:
print(" ", " 0 1 2 3 4 5 6 7 8 9 a b c d e f", end="")
if device%16==0:
print()
print("{0:02x}:".format(device), end="")
if device<0x08 or device>0x77:
print("", " ", end="")
else:
try:
msg = smbus2.i2c_msg.write(device, [])
bus.i2c_rdwr(msg)
print("", "{0:2x}".format(device), end="")
device_count = device_count + 1
except IOError as e:
if e.errno == errno.EREMOTEIO:
print("", "--", end="")
else:
print("", "!!", end="")
except Exception as e:
print("", "##", end="")
print()
bus.close()
bus = None
print("Found {0} device(s)".format(device_count))
if __name__ == "__main__":
i2cdetect()