Simple test
Ensure your device works with this simple test.
examples/ens160_simpletest.py
1# SPDX-FileCopyrightText: Copyright (c) 2022 ladyada for Adafruit Industries
2#
3# SPDX-License-Identifier: Unlicense
4
5import time
6
7import board
8
9import adafruit_ens160
10
11i2c = board.I2C() # uses board.SCL and board.SDA
12# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
13
14ens = adafruit_ens160.ENS160(i2c)
15
16# Set the temperature compensation variable to the ambient temp
17# for best sensor calibration
18ens.temperature_compensation = 25
19# Same for ambient relative humidity
20ens.humidity_compensation = 50
21
22
23while True:
24 print("AQI (1-5):", ens.AQI)
25 print("TVOC (ppb):", ens.TVOC)
26 print("eCO2 (ppm):", ens.eCO2)
27 print()
28
29 # new data shows up every second or so
30 time.sleep(1)
Advanced test
Example showing more of the sensor capabilities.
examples/ens160_advancedtest.py
1# SPDX-FileCopyrightText: Copyright (c) 2022 ladyada for Adafruit Industries
2#
3# SPDX-License-Identifier: Unlicense
4
5import time
6
7import board
8
9import adafruit_ens160
10
11i2c = board.I2C() # uses board.SCL and board.SDA
12# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
13
14ens = adafruit_ens160.ENS160(i2c)
15print("Firmware Vers: ", ens.firmware_version)
16
17ens.mode = adafruit_ens160.MODE_STANDARD
18curr_mode = ens.mode
19print("Current mode: ", end="")
20if curr_mode == adafruit_ens160.MODE_SLEEP:
21 print("Sleeping")
22if curr_mode == adafruit_ens160.MODE_IDLE:
23 print("Idle")
24if curr_mode == adafruit_ens160.MODE_STANDARD:
25 print("Standard sensing")
26
27# Set the temperature compensation variable to the ambient temp
28# for best sensor calibration
29ens.temperature_compensation = 25
30print(f"Current temperature compensation = {ens.temperature_compensation:.1f} *C")
31# Same for ambient relative humidity
32ens.humidity_compensation = 50
33print(f"Current rel humidity compensation = {ens.humidity_compensation:.1f} %")
34print()
35
36# We can have the INT pin tell us when new data is available
37ens.interrupt_pushpull = True # use pushpull 3V, not open-drain
38ens.interrupt_on_data = True # Tell us when there's new calculated data
39ens.interrupt_polarity = False # Active 'low' (false)
40ens.interrupt_enable = True # enable pin
41
42while True:
43 # if no data, loop over
44 if not ens.new_data_available:
45 time.sleep(0.1)
46 continue
47
48 # Check status
49 status = ens.data_validity
50 if status == adafruit_ens160.NORMAL_OP:
51 print("Normal operation")
52 if status == adafruit_ens160.WARM_UP:
53 print("Warming up")
54 if status == adafruit_ens160.START_UP:
55 print("Initial startup")
56 if status == adafruit_ens160.INVALID_OUT:
57 print("Invalid output")
58
59 # read all sensors at once and print out the structure
60 data = ens.read_all_sensors()
61 print("AQI (1-5):", data["AQI"])
62 print("TVOC (ppb):", data["TVOC"])
63 print("eCO2 (ppm):", data["eCO2"])
64 print("Sensor resistances (ohms):", data["Resistances"])
65 print()
DisplayIO Simpletest
This is a simple test for boards with built-in display.
examples/ens160_displayio_simpletest.py
1# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries
2# SPDX-FileCopyrightText: 2024 Jose D. Montoya
3#
4# SPDX-License-Identifier: MIT
5
6# Simple demo of the ENS160 air quality sensor using a built-in display.
7import time
8
9import board
10from adafruit_display_text.bitmap_label import Label
11from displayio import Group
12from terminalio import FONT
13
14import adafruit_ens160
15
16# create a main_group to hold anything we want to show on the display.
17main_group = Group()
18# Initialize I2C bus and sensor.
19i2c = board.I2C() # uses board.SCL and board.SDA
20ens = adafruit_ens160.ENS160(i2c)
21
22# Set the temperature compensation variable to the ambient temp
23# for best sensor calibration
24ens.temperature_compensation = 25
25# Same for ambient relative humidity
26ens.humidity_compensation = 50
27
28# Create a Label to show the readings. If you have a very small
29# display you may need to change to scale=1.
30display_output_label = Label(FONT, text="", scale=2)
31
32# place the label in the middle of the screen with anchored positioning
33display_output_label.anchor_point = (0, 0)
34display_output_label.anchored_position = (4, board.DISPLAY.height // 2)
35
36# add the label to the main_group
37main_group.append(display_output_label)
38
39# set the main_group as the root_group of the built-in DISPLAY
40board.DISPLAY.root_group = main_group
41
42# begin main loop
43while True:
44 # Update the label.text property to change the text on the display
45 display_output_label.text = f"AQI:{ens.AQI} (1-5), TVOC:{ens.TVOC} ppb, eCO2:{ens.eCO2} ppm"
46 # wait for a bit
47 time.sleep(1.0)