The code posted by joan works well for most devices however I was receiving an error when it attempted to read the device at 0x44, which is a SHT31-D from adafruit (
https://www.adafruit.com/product/2857). The code also showed a device at 0x28 which does not exist.
I found that changing bus.read_byte(device) to bus.write_byte(device, 0) worked all of the time.
Here is the output from i2cdetect on my Raspberry Pi zero.
Code: Select all
[email protected]:~ $ sudo i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- 19 -- -- -- -- 1e --
20: -- -- -- -- -- -- -- -- -- 29 -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: 40 -- -- -- 44 -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: 60 -- -- -- -- -- -- -- -- -- -- 6b -- -- -- --
70: -- -- -- -- -- -- -- 77
[email protected]:~ $
Here is the modified code.
Code: Select all
#!/usr/bin/env python3
import smbus
import errno
if __name__ == "__main__":
bus_number = 1 # 1 indicates /dev/i2c-1
bus = smbus.SMBus(bus_number)
device_count = 0
for device in range(3, 128):
try:
bus.write_byte(device, 0)
print("Found {0}".format(hex(device)))
device_count = device_count + 1
except IOError as e:
if e.errno != errno.EREMOTEIO:
print("Error: {0} on address {1}".format(e, hex(address)))
except Exception as e: # exception if read_byte fails
print("Error unk: {0} on address {1}".format(e, hex(address)))
bus.close()
bus = None
print("Found {0} device(s)".format(device_count))
The output after running the above code.
I know this thread is a few years old now but I'm posting this update so it may help others if they run across this thread.
BTW: The run with joan's code, modified with capturing the error on device 0x44, was the following.
After running into this error, i2cdetect showed that no devices were connected to the I2C bus. The only way to recover was to shut down the Raspberry Pi, turn the power off, then turn the power back on.
Code: Select all
[email protected]:~ $ sudo i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
[email protected]:~ $