2

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.

  • 1
    Here is my working code abyz.me.uk/rpi/pigpio/examples.html#Python_Si7021_py There is a difference between SMBus and I2C. SMBus has a richer command set but is strangely enough far less flexible. – joan Jul 10 at 19:54
  • @joan You can bet I already came across your site. I have not yet been able to run, though mostly because of my poor python skills. I'll come back with more results. – Marcel Jul 10 at 20:04
  • @joan Ok, that was all my faults. I did save it with another name than given, and modules seem to be picky about their names, even casings. Then I needed to start the demon as stated in the intro screen. Now it works. However, I wonder, do I need a demon to use I2C generally? – Marcel Jul 10 at 20:08
  • 1
    No you don't need a daemon to use I2C, that's just how my software works (it lets you run the script on another machine whilst talking to a Pi over the network). You need to see how that example code reads multiple bytes and see if you can adapt your code in a similar fashion. – joan Jul 10 at 20:16
  • @joan I could not figure out the necessary code to work without the daemon, I still got those weird error on write. Eventually, this is for a hobby project only, anyway. However, I have written up my odissey in a blog post at qrys.ch/…, if your interested. I give you credit there for your module. If you write up an answer here mentioning your module, I'll gladly accept it as an answer. – Marcel Jul 11 at 22:18
0

Credit for the solution goes to @joan. Key here seems to omit the I2C and SMBus python implementations and query the sensor via the pipgio library.

Using @joan's pigpio library and his Si7021.py script I finally got a solution that uses the full resolution.

I've written in detail in my blog post about how I used the library and script, but basically you can do it like this in the terminal:

# installing pigpio first
sudo apt-get install pigpio python-pigpio python3-pigpio

# get the Si7021.py script
wget http://abyz.me.uk/rpi/pigpio/code/Si7021_py.zip
unzip Si7021_py.zip

# start the pigpiod daemon; -l disables the remote socket interface, it's not needed to read a local sensor
sudo pigpiod -l 
# start the main function in the module
python3 Si7021.py

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.