I recently bought a Raspberry Pi 4B along with a BME280 T/P/humidity sensor. I use the bme280
and smbus2
Python libraries to interface with the sensor. For a while it worked pretty well, but yesterday I started getting frequent OSError
s. Here is a bare minimum example that raises the error:
from smbus2 import SMBus
with SMBus(1) as bus:
bus.pec = 1
b = bus.read_i2c_block_data(0x76, 0xF7, 8)
print(b)
Stack trace:
Traceback (most recent call last):
File "/home/****/Tsensor/local/test.py", line 5, in <module>
b = bus.read_i2c_block_data(0x76, 0xF7, 8)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/****/miniforge3/envs/sensor/lib/python3.11/site-packages/smbus2/smbus2.py", line 617, in read_i2c_block_data
ioctl(self.fd, I2C_SMBUS, msg)
OSError: [Errno 5] Input/output error
The output of 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: -- -- -- -- -- -- 76 --
Note that the OSError
is not always triggered. Sometimes I do get a valid output from this minimal example.
Any idea what could cause this behaviour?
i2c_baudrate=100000
(or lower) in/boot/config.txt
(and rebooting) fixed my issue (mostly). If you repost your comment as a reply, I can mark it as the correct answer for future generations of tinkerers.