MicroPython SI7021 Library

micropython_si7021

MicroPython Driver for the SI7021 Temperature and Humidity sensor

  • Author(s): Jose D. Montoya

Implementation Notes

Software and Dependencies:

This library depends on Micropython

class micropython_si7021.si7021.RegisterStructRW(form: str, cmd_read: int = None, cmd_write: int = None)[source]

Register Struct

class micropython_si7021.si7021.SI7021(i2c, address=0x40)[source]

Main class for the Sensor

Parameters:
i2c : I2C

The I2C bus the SI7021 is connected to.

address : int

The I2C device address. Defaults to 0x40

Raises:

RuntimeError – if the sensor is not found

Quickstart: Importing and using the device

Here is an example of using the micropython_si7021.SI7021 class. First you will need to import the libraries to use the sensor

from machine import Pin, I2C
import micropython_si7021 as si7021

Once this is done you can define your machine.I2C object and define your sensor object

i2c = I2C(sda=Pin(8), scl=Pin(9))
si = si7021.SI7021(i2c)

Now you have access to the temperature and humidity attributes

temp = si.temperature
hum = si.humidity
property resolution

Meassurement resolution. The resolution of the measures. This will return the temperature and humidity resolution. These values are linked so change the resolution in temperature will affect humidity’s resolution

Value

RH

Temp

00

12 bit

14 bit

01

8 bit

12 bit

10

10 bit

13 bit

11

11 bit

11 bit

When selecting the values use the following variables:

Mode

Value

si7021.TEMP_14_RH_12

0b00000000

si7021.TEMP_12_RH_8

0b00000001

si7021.TEMP_13_RH_10

0b10000000

si7021.TEMP_11_RH_11

0b10000001

Example

from machine import Pin, I2C
import micropython_si7021 as si7021

i2c = I2C(sda=Pin(8), scl=Pin(9))  # Correct I2C pins for UM FeatherS2
si = si7021.SI7021(i2c)

si.resolution = si7021.TEMP_13_RH_10
property temperature

Returns the temperature in Celsius. Temperature resolution can be adjusted with the temperature attribute

Example

from machine import Pin, I2C
import micropython_si7021 as si7021

i2c = I2C(sda=Pin(8), scl=Pin(9))  # Correct I2C pins for UM FeatherS2
si = si7021.SI7021(i2c)

print("Temperature: ", si.temperature)
property humidity
Returns the humidity in %. Temperature resolution can be adjusted with the

humidity attribute

Example

from machine import Pin, I2C
import micropython_si7021 as si7021

i2c = I2C(sda=Pin(8), scl=Pin(9))  # Correct I2C pins for UM FeatherS2
si = si7021.SI7021(i2c)

print("Relative Humidity: ", si.humidity)
property heater

Sensor Heater Status.

Example

from machine import Pin, I2C
import micropython_si7021 as si7021

i2c = I2C(sda=Pin(8), scl=Pin(9))  # Correct I2C pins for UM FeatherS2
si = si7021.SI7021(i2c)

# Turning ON the Heater
print("Status of the Sensor Heater: ", si.heater)
si.heater = si7021.HEATER_ON
print("Status of the Sensor Heater: ", si.heater)