Chain ENV
This library is the driver for Chain ENV.
Support the following products:
UiFlow2 Example
Environmental readings
Open the m5basic_chain_env_basic_example.m5f2 project in UiFlow2.
Connect Chain ENV as the first device on M5Stack Basic UART2 (TX=21, RX=22). The screen displays temperature (degrees Celsius), humidity (%), pressure (hPa), and altitude (m), refreshed every 500 ms. Set SEA_LEVEL_PRESSURE_PA to the current local sea-level pressure. The initial Shenzhen test reference is 101330 Pa (1013.3 hPa).
UiFlow2 Code Block:
MicroPython Example
Environmental readings
Connect Chain ENV as the first device on M5Stack Basic UART2 (TX=21, RX=22). The screen displays temperature (degrees Celsius), humidity (%), pressure (hPa), and altitude (m), refreshed every 500 ms. Set SEA_LEVEL_PRESSURE_PA to the current local sea-level pressure. The initial Shenzhen test reference is 101330 Pa (1013.3 hPa).
MicroPython Code Block:
1# SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD 2# 3# SPDX-License-Identifier: MIT 4 5import os, sys, io 6import M5 7from M5 import * 8import time 9from chain import ChainBus 10from chain import ENVChain 11 12 13title = None 14pressure_caption = None 15label_pressure = None 16pressure_unit = None 17temperature_caption = None 18label_temperature = None 19temperature_unit = None 20humidity_caption = None 21label_humidity = None 22humidity_unit = None 23altitude_caption = None 24label_altitude = None 25altitude_unit = None 26sea_level_caption = None 27bus2 = None 28chain_env_0 = None 29 30 31SEA_LEVEL_PRESSURE_PA = None 32UPDATE_INTERVAL_MS = None 33last_time = None 34 35 36def setup(): 37 global \ 38 title, \ 39 pressure_caption, \ 40 label_pressure, \ 41 pressure_unit, \ 42 temperature_caption, \ 43 label_temperature, \ 44 temperature_unit, \ 45 humidity_caption, \ 46 label_humidity, \ 47 humidity_unit, \ 48 altitude_caption, \ 49 label_altitude, \ 50 altitude_unit, \ 51 sea_level_caption, \ 52 bus2, \ 53 chain_env_0, \ 54 SEA_LEVEL_PRESSURE_PA, \ 55 UPDATE_INTERVAL_MS, \ 56 last_time 57 58 M5.begin() 59 Widgets.setRotation(1) 60 Widgets.fillScreen(0x10171D) 61 title = Widgets.Label("Chain ENV", 111, 8, 1.0, 0x4CD7D0, 0x10171D, Widgets.FONTS.Montserrat18) 62 pressure_caption = Widgets.Label( 63 "Pressure", 12, 44, 1.0, 0x8FA1AD, 0x10171D, Widgets.FONTS.Montserrat14 64 ) 65 label_pressure = Widgets.Label( 66 "----.-", 12, 62, 1.0, 0xF2F6F8, 0x10171D, Widgets.FONTS.Montserrat40 67 ) 68 pressure_unit = Widgets.Label( 69 "hPa", 170, 84, 1.0, 0x4CD7D0, 0x10171D, Widgets.FONTS.Montserrat18 70 ) 71 temperature_caption = Widgets.Label( 72 "Temperature", 12, 118, 1.0, 0x8FA1AD, 0x10171D, Widgets.FONTS.Montserrat14 73 ) 74 label_temperature = Widgets.Label( 75 "--.-", 12, 137, 1.0, 0xFFB454, 0x10171D, Widgets.FONTS.Montserrat24 76 ) 77 temperature_unit = Widgets.Label( 78 "C", 80, 144, 1.0, 0xFFB454, 0x10171D, Widgets.FONTS.Montserrat18 79 ) 80 humidity_caption = Widgets.Label( 81 "Humidity", 170, 118, 1.0, 0x8FA1AD, 0x10171D, Widgets.FONTS.Montserrat14 82 ) 83 label_humidity = Widgets.Label( 84 "--.-", 170, 137, 1.0, 0x63D08C, 0x10171D, Widgets.FONTS.Montserrat24 85 ) 86 humidity_unit = Widgets.Label( 87 "%", 225, 144, 1.0, 0x63D08C, 0x10171D, Widgets.FONTS.Montserrat18 88 ) 89 altitude_caption = Widgets.Label( 90 "Altitude", 12, 180, 1.0, 0x8FA1AD, 0x10171D, Widgets.FONTS.Montserrat14 91 ) 92 label_altitude = Widgets.Label( 93 "----.-", 100, 173, 1.0, 0x63A8FF, 0x10171D, Widgets.FONTS.Montserrat24 94 ) 95 altitude_unit = Widgets.Label( 96 "m", 200, 180, 1.0, 0x63A8FF, 0x10171D, Widgets.FONTS.Montserrat18 97 ) 98 sea_level_caption = Widgets.Label( 99 "Sea level / Shenzhen: -- hPa", 100 12, 101 215, 102 1.0, 103 0x8FA1AD, 104 0x10171D, 105 Widgets.FONTS.Montserrat14, 106 ) 107 108 title.setCursor( 109 x=(int((320 - (M5.Display.textWidth("Chain ENV", Widgets.FONTS.Montserrat18))) / 2)), y=8 110 ) 111 SEA_LEVEL_PRESSURE_PA = 101330 112 UPDATE_INTERVAL_MS = 500 113 bus2 = ChainBus(2, tx=21, rx=22) 114 chain_env_0 = ENVChain(bus2, 1) 115 chain_env_0.set_sea_level_pressure(SEA_LEVEL_PRESSURE_PA) 116 sea_level_caption.setText( 117 str( 118 ( 119 str((str("Sea level / Shenzhen: ") + str((SEA_LEVEL_PRESSURE_PA / 100)))) 120 + str(" hPa") 121 ) 122 ) 123 ) 124 label_pressure.setText(str(round((chain_env_0.read_pressure()) / 100, 1))) 125 label_temperature.setText( 126 str(round(chain_env_0.read_temperature(sensor=ENVChain.SENSOR_AHT20), 1)) 127 ) 128 label_humidity.setText(str(round(chain_env_0.read_humidity(), 1))) 129 label_altitude.setText(str(round(chain_env_0.read_altitude(), 1))) 130 last_time = time.ticks_ms() 131 132 133def loop(): 134 global \ 135 title, \ 136 pressure_caption, \ 137 label_pressure, \ 138 pressure_unit, \ 139 temperature_caption, \ 140 label_temperature, \ 141 temperature_unit, \ 142 humidity_caption, \ 143 label_humidity, \ 144 humidity_unit, \ 145 altitude_caption, \ 146 label_altitude, \ 147 altitude_unit, \ 148 sea_level_caption, \ 149 bus2, \ 150 chain_env_0, \ 151 SEA_LEVEL_PRESSURE_PA, \ 152 UPDATE_INTERVAL_MS, \ 153 last_time 154 M5.update() 155 if (time.ticks_diff((time.ticks_ms()), last_time)) >= UPDATE_INTERVAL_MS: 156 label_pressure.setText(str(round((chain_env_0.read_pressure()) / 100, 1))) 157 label_temperature.setText( 158 str(round(chain_env_0.read_temperature(sensor=ENVChain.SENSOR_AHT20), 1)) 159 ) 160 label_humidity.setText(str(round(chain_env_0.read_humidity(), 1))) 161 label_altitude.setText(str(round(chain_env_0.read_altitude(), 1))) 162 last_time = time.ticks_ms() 163 time.sleep_ms(20) 164 165 166if __name__ == "__main__": 167 try: 168 setup() 169 while True: 170 loop() 171 except (Exception, KeyboardInterrupt) as e: 172 try: 173 bus2.deinit() 174 from utility import print_error_msg 175 176 print_error_msg(e) 177 except ImportError: 178 print("please update to latest firmware")
API
ENVChain
- class chain.env.ENVChain(bus, device_id)
Bases:
KeyChainDriver for Chain ENV.
Create a ENVChain on an existing ChainBus. Registration performs no device configuration. The caller owns the bus and must close it after all devices are detached.
- Parameters:
- Returns:
A registered ENVChain object.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
device = ENVChain(bus, 1)
- ODR_1HZ = 1
- ODR_1_5625HZ = 1.5625
- ODR_2HZ = 2
- ODR_3_125HZ = 3.125
- ODR_4HZ = 4
- ODR_6_25HZ = 6.25
- ODR_8HZ = 8
- ODR_12_5HZ = 12.5
- ODR_16HZ = 16
- ODR_25HZ = 25
- ODR_32HZ = 32
- ODR_50HZ = 50
- ODR_64HZ = 64
- ODR_100HZ = 100
- ODR_128HZ = 128
- ODR_200HZ = 200
- ODR_VALUES = (1, 1.5625, 2, 3.125, 4, 6.25, 8, 12.5, 16, 25, 32, 50, 64, 100, 128, 200)
- SENSOR_AHT20 = 0
- SENSOR_SPA06 = 1
- ALARM_NONE = 0
- ALARM_TEMPERATURE = 1
- ALARM_HUMIDITY = 2
- ALARM_PRESSURE = 4
- ALARM_ALTITUDE = 8
- ALARM_ALL = 15
- EVENT_TEMPERATURE_HIGH_CLEAR = 16
- EVENT_TEMPERATURE_HIGH = 17
- EVENT_TEMPERATURE_LOW = 18
- EVENT_TEMPERATURE_LOW_CLEAR = 19
- EVENT_HUMIDITY_HIGH_CLEAR = 32
- EVENT_HUMIDITY_HIGH = 33
- EVENT_HUMIDITY_LOW = 34
- EVENT_HUMIDITY_LOW_CLEAR = 35
- EVENT_PRESSURE_HIGH_CLEAR = 48
- EVENT_PRESSURE_HIGH = 49
- EVENT_PRESSURE_LOW = 50
- EVENT_PRESSURE_LOW_CLEAR = 51
- EVENT_ALTITUDE_HIGH_CLEAR = 64
- EVENT_ALTITUDE_HIGH = 65
- EVENT_ALTITUDE_LOW = 66
- EVENT_ALTITUDE_LOW_CLEAR = 67
- read_temperature_humidity()
Read one AHT20 temperature and humidity sample.
- Returns:
Tuple (temperature, humidity), in degrees Celsius and percent.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
temperature, humidity = device.read_temperature_humidity()
- read_pressure_temperature_altitude()
Read one SPA06 sample. Altitude uses the configured sea-level pressure.
- Returns:
Tuple (temperature, pressure, altitude), in degrees Celsius, Pa and metres. Pressure resolution is 10 Pa; altitude resolution is 0.01 m.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
temperature, pressure, altitude = device.read_pressure_temperature_altitude()
- read_temperature(sensor=0)
Read temperature from the selected sensor.
- Parameters:
sensor (int) – Temperature source: SENSOR_AHT20 (0) or SENSOR_SPA06 (1). Default: SENSOR_AHT20 (0).
- Returns:
Temperature in degrees Celsius, resolution 0.01 degrees.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
temperature = device.read_temperature(ENVChain.SENSOR_AHT20)
- read_humidity()
Read humidity.
- Returns:
Relative humidity in percent, 0..100.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
value = device.read_humidity()
- read_pressure()
Read pressure.
- Returns:
Pressure in Pa, resolution 10 Pa.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
value = device.read_pressure()
- read_altitude()
Read altitude.
- Returns:
Altitude in metres, resolution 0.01 m.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
value = device.read_altitude()
- set_temperature_rate(value)
Set SPA06 temperature sampling rate.
- Parameters:
value (int or float) – An ODR_* constant in ODR_VALUES, in Hz; applies to SPA06 only.
- Returns:
No return value.
- Return type:
None
UiFlow2 Code Block:

MicroPython Code Block:
device.set_temperature_rate(ENVChain.ODR_16HZ)
- get_temperature_rate()
Get SPA06 temperature sampling rate.
UiFlow2 Code Block:

MicroPython Code Block:
value = device.get_temperature_rate()
- set_pressure_rate(value)
Set SPA06 pressure sampling rate.
- Parameters:
value (int or float) – An ODR_* constant in ODR_VALUES, in Hz; applies to SPA06 only.
- Returns:
No return value.
- Return type:
None
UiFlow2 Code Block:

MicroPython Code Block:
device.set_pressure_rate(ENVChain.ODR_16HZ)
- get_pressure_rate()
Get SPA06 pressure sampling rate.
UiFlow2 Code Block:

MicroPython Code Block:
value = device.get_pressure_rate()
- set_sea_level_pressure(pressure)
Set the reference pressure for altitude calculation.
- Parameters:
pressure (int or float) – Sea-level pressure in Pa, 70000..120000, in steps of 10 Pa.
- Returns:
No return value.
- Return type:
None
UiFlow2 Code Block:

MicroPython Code Block:
device.set_sea_level_pressure(101330)
- get_sea_level_pressure()
Get the reference pressure for altitude calculation.
- Returns:
Sea-level pressure in Pa, resolution 10 Pa.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
pressure = device.get_sea_level_pressure()
- set_alarm_mode(mode)
Select the enabled alarm sources. Zero disables all alarms. Bits: 1 temperature, 2 humidity, 4 pressure, 8 altitude.
- Parameters:
mode (int) – Alarm bitmask, 0..15; combine ALARM_* bits with
|, use ALARM_NONE to disable or ALARM_ALL to enable all sources.- Returns:
No return value.
- Return type:
None
UiFlow2 Code Block:

MicroPython Code Block:
device.set_alarm_mode(ENVChain.ALARM_ALL)
- get_alarm_mode()
Get the enabled alarm sources.
- Returns:
Alarm bitmask, 0..15; combine ALARM_* bits with
|, use ALARM_NONE to disable or ALARM_ALL to enable all sources.- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
mode = device.get_alarm_mode()
- set_temperature_alarm_thresh(low, high, sensor=0)
Set the temperature alarm window and source.
- Parameters:
- Returns:
No return value.
- Return type:
None
UiFlow2 Code Block:

MicroPython Code Block:
device.set_temperature_alarm_thresh(10, 40)
- get_temperature_alarm_thresh()
Get the temperature alarm window and source.
- Returns:
Tuple (low, high, sensor); temperatures in degrees Celsius.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
low, high, sensor = device.get_temperature_alarm_thresh()
- set_humidity_alarm_thresh(low, high)
Set the humidity alarm window.
- Parameters:
- Returns:
No return value.
- Return type:
None
UiFlow2 Code Block:

MicroPython Code Block:
device.set_humidity_alarm_thresh(20, 80)
- get_humidity_alarm_thresh()
Get the humidity alarm window.
- Returns:
Tuple (low, high). Relative humidity in percent.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
low, high = device.get_humidity_alarm_thresh()
- set_pressure_alarm_thresh(low, high)
Set the pressure alarm window.
- Parameters:
- Returns:
No return value.
- Return type:
None
UiFlow2 Code Block:

MicroPython Code Block:
device.set_pressure_alarm_thresh(95000, 105000)
- get_pressure_alarm_thresh()
Get the pressure alarm window.
- Returns:
Tuple (low, high). Pressure in Pa, resolution 10 Pa.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
low, high = device.get_pressure_alarm_thresh()
- set_altitude_alarm_thresh(low, high)
Set the altitude alarm window.
- Parameters:
- Returns:
No return value.
- Return type:
None
UiFlow2 Code Block:

MicroPython Code Block:
device.set_altitude_alarm_thresh(0, 1000)
- get_altitude_alarm_thresh()
Get the altitude alarm window.
- Returns:
Tuple (low, high). Altitude in metres, resolution 0.01 m.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
low, high = device.get_altitude_alarm_thresh()
- deinit()
Detach the device and release its callbacks; the shared bus stays open.
- Returns:
None. Repeated calls are safe.
- Return type:
None
UiFlow2 Code Block:

- set_event_callback(event=None, callback=None)
Register a high, low, high-clear or low-clear alarm callback.
Use EVENT_<SOURCE>_HIGH, LOW, HIGH_CLEAR or LOW_CLEAR. Each handler receives its selected event constant. Register before enabling alarms. Registration does not change hardware settings. Perform I/O in the loop. HIGH_CLEAR requires only that source enabled via set/get_alarm_mode(); its source cannot be identified with multiple alarms enabled. Legacy source selectors and the single-callback form receive raw status.
- Parameters:
event (int or callable or None) – An EVENT_* constant, legacy callback or None. Default None. None clears all handlers.
callback (callable or None) – Handler receiving the selected event constant. Default None removes the selected source’s handler.
- Returns:
None. Each source’s handler can be replaced independently.
- Return type:
None
UiFlow2 Code Block:

MicroPython Code Block:
device.set_event_callback(ENVChain.EVENT_HUMIDITY_HIGH, on_humidity)

