I’m running the below code. It works for a brief moment then fails with the error “[Errno 5] Input/output error”
import time
import easygopigo3 as easy
gpg = easy.EasyGoPiGo3()
my_distance_sensor = gpg.init_distance_sensor()
while True:
# Directly print the values of the sensor.
print("Distance Sensor Reading (mm): " + str(my_distance_sensor.read_mm()))
.
When i try to rerun the code after the initial error i then get the error
" print("Distance Sensor Reading (mm): " + str(my_distance_sensor.read_mm()))
AttributeError: ‘NoneType’ object has no attribute ‘read_mm’
"
I tried detaching the cable from 12c and reattached it again which got it to work for a brief moment but then failed again. Any suggestions on how to solve this would be greatly appreciated.
The init_distance_sensor method will return None when an object of the distance sensor class cannot be created. That’s one thing.
The next thing is, getting that IO Error can be due to the Raspberry Pi’s buggy HW I2C implementation. When it fails, I think it also puts the distance sensor in a brownout state for a brief moment. Thing is, you must have the latest version of the GoPiGo3’s library installed in order for it to use a different I2C bus that’s more stable: the SW I2C.
Update it, reboot and check that again:
Or you can go the other route and use one of the 2 AD ports on the GoPiGo3. That makes all the transfers go through the GoPiGo3 and it’s more stable than the Raspberry Pi’s I2C. To use this, call the constructor wrapper like init_distance_sensor('AD1') or 'AD2' for the other port and change the sensor to the designated port.
Please let me know the state of your situation. Thanks!
I will test the GoPiGo3 + distance sensor on the SW I2C bus and see why it fails. It shouldn’t. But it certainly fails on the HW I2C (aka the RaspberryPi’s) after a few moments. I have a hunch that yours is still on the HW I2C.