SI7021 I2C sensor
-
I'm trying to get the SI7021 sensor working, which is connected on the I2C bus. Connectivity basically works and I can also write and read bytes. But I strugle with converting the example Arduino code to MicroPython. I've also found another example using the smbus Python library, but I also couldn't find out how to convert this.
Here is what I've done so far:
from machine import I2C import time ADDR = 0x40 SI7021_MEASTEMP_NOHOLD_CMD = 0xF3 SI7021_MEASRH_NOHOLD_CMD = 0xF5 # configure the I2C bus i2c = I2C(0, I2C.MASTER, baudrate=100000) # Humidity i2c.writeto(ADDR, bytes([SI7021_MEASRH_NOHOLD_CMD])) time.sleep(0.3) hum0 = i2c.readfrom(ADDR, 1) hum1 = i2c.readfrom(ADDR, 1) print(hum0) print(hum1) #humidity = ((hum0 * 256 + hum1) * 125 / 65536.0) - 6 #print(humidity) # Temperature i2c.writeto(ADDR, bytes([SI7021_MEASTEMP_NOHOLD_CMD])) time.sleep(0.3) temp0 = i2c.readfrom(ADDR, 1) temp1 = i2c.readfrom(ADDR, 1) print(temp0) print(temp1) #cTemp = ((temp0 * 256 + temp1) * 175.72 / 65536.0) - 46.85 #fTemp = cTemp * 1.8 + 32 #print(cTemp)Output:
b'F' b'F' b'h' b'h'Currently I don't understand how I can read and convert the data correctly. Sending the commands works and I also get some sort of data back when calling the
i2c.readfromfunction, but then I'm lost. F.e. I don't know how many bytes I must read and then how to convert it to an actual useable value.Any help is much appreciated!
Cheers,
Tobias -
Try This;
from time import sleep_ms from machine import Pin, I2C # Default Address SI7021_I2C_DEFAULT_ADDR = 0x40 # Commands CMD_MEASURE_RELATIVE_HUMIDITY_HOLD_MASTER_MODE = 0xE5 CMD_MEASURE_RELATIVE_HUMIDITY = 0xF5 CMD_MEASURE_TEMPERATURE_HOLD_MASTER_MODE = 0xE3 CMD_MEASURE_TEMPERATURE = 0xF3 CMD_READ_TEMPERATURE_VALUE_FROM_PREVIOUS_RH_MEASUREMENT = 0xE0 CMD_RESET = 0xFE CMD_WRITE_RH_T_USER_REGISTER_1 = 0xE6 CMD_READ_RH_T_USER_REGISTER_1 = 0xE7 CMD_WRITE_HEATER_CONTROL_REGISTER = 0x51 CMD_READ_HEATER_CONTROL_REGISTER = 0x11 class SI7021(object): def __init__(self, i2c=None): self.i2c = i2c self.addr = SI7021_I2C_DEFAULT_ADDR self.cbuffer = bytearray(2) self.cbuffer[1] = 0x00 def write_command(self, command_byte): self.cbuffer[0] = command_byte self.i2c.writeto(self.addr, self.cbuffer) def readTemp(self): self.write_command(CMD_MEASURE_TEMPERATURE) sleep_ms(25) temp = self.i2c.readfrom(self.addr,3) temp2 = temp[0] << 8 temp2 = temp2 | temp[1] return (175.72 * temp2 / 65536) - 46.85 def readRH(self): self.write_command(CMD_MEASURE_RELATIVE_HUMIDITY) sleep_ms(25) rh = self.i2c.readfrom(self.addr, 3) rh2 = rh[0] << 8 rh2 = rh2 | rh[1] return (125 * rh2 / 65536) - 6 -
@crankshaft said in SI7021 I2C sensor:
Try This;
WOW! You're great, works like a charm... Thank you very very much!
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login