0

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 OSErrors. 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?

2
  • 1
    Try tweaking I2C bus frequency. I have a dtparam=i2c_baudrate=400000 on my pi zero 2w, but you can go lower... Also, i use for same sensor a RPi.bme280 library. Nov 21, 2023 at 21:21
  • Setting 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.
    – MPA
    Nov 24, 2023 at 9:01

2 Answers 2

1

set

dtparam=i2c_baudrate=100000 (or lower)

in /boot/config.txt

0

You have a poor I2C connection. Check that all the pertinent wires are secure and make a good connection. I.e. check power, ground, SDA, and SCL.

6
  • I replaced the wires and redid the connections several times over, but the problem persists. Could it be a faulty sensor?
    – MPA
    Nov 18, 2023 at 18:23
  • Yes, however a wiring problem is far more likely. Perhaps add photos clearly showing the connections at the Pi and the sensor ends.
    – joan
    Nov 18, 2023 at 20:32
  • I find these sensors are often prone to 'crashing' and a power cycle is needed to restore communication
    – Bra1n
    Nov 19, 2023 at 7:30
  • @joan: I've uploaded some pictures to here.
    – MPA
    Nov 19, 2023 at 9:46
  • @Bra1n: how would one do a power cycle? (I am quite green in this sort of work).
    – MPA
    Nov 19, 2023 at 9:47

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .