I bought an SI7021 adafruit sensor which I have been struggling to get to work since it arrived!
I've wired it up using the 3.3v line for power so to match the logic for the Pi. I have carried out the i2cdetect command to check the bus and I can see it on address 40 as you should!
I've imported the smbus library which I'm trying to use to read the bytes from the sensor with following the datasheet https://cdn-learn.adafruit.com/assets/a ... 21-A20.pdf
The humidity read i2c transaction is:
S, Slave add, W, [A], Measure command, [A], Sr, Slave add, r, [NA*], Sr, Slave address, r, [A], [MS Byte], a, [LS Byte], NA, P
*Note: Device will NACK the slave address byte until conversion is complete.
[ ] = slave
I've attempted a number of different smbus commands to try and get the sensor to give back a relative humidity value. Somewhere on the internet it said to:
Code: Select all
import smbus
import time
bus = smbus.SMBus(1) #set the bus up e.g./dev/i2c-1
address = 0x40
bus.write_byte(address,0xF5)
data0 = bus.read_byte(address) #MSB
data1 = bus.read_byte(address) #LSB the sensor sends the reading back in a 16bit word.
I've also tried to use the i2c_smbus_write_byte_data() and i2c_smbus_read_word_data() commands which return some errors which I can't remember at the time of writing this. I'll add them on later.
I have however successfully read the user1 register. The output I get seems to match up to what it should be against the data sheet with the RSVD showing 1's in the correct places! This was using the code:
Code: Select all
import smbus
import time
bus = smbus.SMBus(1) #set the bus up e.g./dev/i2c-1
address = 0x40
read_user_register = bus.read_byte_data(address,0xE7) & 0xFF
time.sleep(0.1)
print("{:08b}".format(read_user_register))
It would be greatly appreciated if someone could shed some light on this as I've been tearing my hear out trying to sort it and scouring the internet! I'm thinking that maybethe read word smbus command doesnt put the bits in the correct place as per what the datasheet shows the device is expecting.
Thanks all, Sam.