I am using the Si7021-A20 sensor on a Raspberry Pi Zero WH (headless, controlled via ssh and VNC). I want to get readings using Python (version 3). However, I have a very hard time to actually get more than a single byte of data.
Here's what I have now (SI7021-test2-doublebyteread-nohold.py):
from smbus import SMBus import time # Get I2C bus bus = SMBus(1) #Measure Relative Humidity, No Hold Master Mode 0xF5 bus.write_byte(0x40, 0xF5) # SI7021 address, 0x40 Read 2 bytes, Humidity time.sleep(0.1) data0 = bus.read_byte(0x40) time.sleep(0.1) data1 = bus.read_byte(0x40) print(data0) print(data1) # Convert the data humidity = ((data0 * 256 + data1) * 125 / 65536.0) - 6 print(humidity)
this gives in Thonny (and similar from the command line)
========================= RESTART ========================= >>> %Run SI7021-test2-doublebyteread-nohold.py 104 104 44.9796142578125
44.9 is actually a realistic value, matching another device's reading.
Note, that the two bytes are having the same value. This is always the case. If the humidity increases, both value increase to the same value.Now I fail to get that second byte out of the sensor.
I tried the "Hold" and "No hold" modes, played with the timings, used smbus2, and also with the I2C blocks like:
rh = bus.read_i2c_block_data(0x40, 0xE5, 2)
But this gives
>>> %Run SI7021-test1-i2c-block.py Traceback (most recent call last): File "/home/pi/SI7021-test1-i2c-block.py", line 10, in <module> rh = bus.read_i2c_block_data(0x40, 0xE5, 2) OSError: [Errno 5] Input/output error
How to get that second byte as valid reading?
Note: There are various blog posts, forum threads and other resources, but no I found helped me beyond what I describe here.