PiicoDev Air Quality Sensor ENS160 | Getting Started Guide

Updated 12 January 2023

The PiicoDev® Air Quality Sensor is based on an ENS160 multi-gas sensor that detects Volatile Organic Compounds (VOCs). These measurements are run through some algorithms to produce standardised outputs like Air Quality Index (AQI), Equivalent CO2 (eCO2), and Total Volatile Organic Compounds (TVOC). These readings are useful for logging indoor air quality, or controlling ventilation and alarms.

Hardware Connections

To follow along you'll need:

Plug your Pico into the expansion board. Make sure it is plugged in the correct orientation - Pin 0 on the expansion board should be to the left of the Pico's USB connector.

Connect your Air Quality Sensor to the expansion board with a PiicoDev Cable.

The PiicoDev Air Quality Sensor features an address switch labelled ASW. Ensure this switch is OFF before proceeding. This will use the default device address. For advanced users, this corresponds to OFF:0x53 and ON:0x52.

To follow along you'll need:

Mount the Adapter onto your Pi's GPIO. Make sure it is plugged in the correct orientation - An arrow on the Adapter will point to the Pi's Ethernet connector (on a Pi 3 the Ethernet connector and USB ports are swapped.)

Connect your Air Quality Sensor to the Adapter with a PiicoDev Cable.

To follow along you'll need:

Plug your micro:bit into the Adapter, making sure the buttons on the micro:bit are facing up.

Connect your Air Quality Sensor to the Adapter with a PiicoDev Cable.


Setup Thonny

Select your dev board from the tabs above to get ready for programming with Thonny for the first time. 

If you have already programmed with PiicoDev modules before, there's probably no need to follow these steps again.

Let's get set up with scripting in Thonny for the Raspberry Pi Pico.

We'll install Thonny, configure for Pico and write our first script. To follow along you'll need:

Contents

 

Install Thonny

If you're working on a Raspberry Pi 4, you're in luck - Thonny comes pre-installed. For those on another operating system, download Thonny here and run the installer.

Once the installer finishes, run Thonny.

Set up Thonny

Hold the BOOTSEL button on your Pico, and connect your Pico to your computer via USB.

Go to Run > Select interpreter and choose MicroPython (Raspberry Pi Pico).

It's also a good idea to install or update firmware. This will update your Pico with the latest version of MicroPython, or install MicroPython if it wasn't already.

Select Interpreter

REPL interface (Shell)

We can immediately start executing code in the REPL - Enter this code in the shell tab: print("Hello, World!")

The command will be sent to your Pico, which will execute the command and display back the message.

We can also take control of the on-board LED by executing the following code:

from machine import Pin
led = Pin(25, Pin.OUT)
led.toggle()

This code will toggle the LED. If you keep executing led.toggle() the LED will keep changing state.

REPL Interface

LED On

Writing a Script

Create a new script with File > New and paste in the following code:

from machine import Pin
from time import sleep
led = Pin(25, Pin.OUT)
n = 0;
while True:
    led.toggle()
    print("13 x {} = {}".format(n, 13*n)) # print the thirteen-times table
    n = n + 1
    sleep(0.5)

Save the script - you will be prompted to save to your computer OR the pico. Select save to Pico and name the file main.py

Return to the REPL and press Ctrl+D (or use the Stop/Restart button) to restart your Pico. The LED should flash at a steady rate and the shell should begin printing multiples of thirteen.

Script Output

Installing a Package

Packages are reusable pieces of code that another programmer has written for a common task, such as interfacing with a specific piece of hardware. Thonny has support for installing micropython packages from the Python Package Index - aka 'PyPI' directly onto the Raspberry Pi Pico.

To install a package, ensure the Pico is plugged in and go to Tools > Manage Packages, which will show the Manage Packages dialog.

Enter the name of the package you would like to install into the search bar and click 'Search on PyPI'.

In the search results list, click the title of the package you would like to install. A title that's in bold simply indicates a direct search match on the text you entered in the search bar.

The Manage Packages dialog will show an overview of the package you have clicked. Click Install to install the package on your Pico.

You can confirm the package has been installed by checking the 'Raspberry Pi Pico' section of the File View in Thonny. The view should show a new folder named 'lib', and inside this folder will be one or more folders containing the metadata and source code of the library you just installed.

 

An example of installing the micropython-servo libraryAn example of installing the micropython-servo library
An example of installing the micropython-servo library

Conclusion

We've installed Thonny and uploaded scripts to our Raspberry Pi Pico - if you have any questions feel free to start the conversation below, or open a topic in our forums - we're full time makers and happy to help!

Good news! Thonny comes pre-installed with Raspberry Pi OS. However, to work with PiicoDev we need to enable I2C communications as follows:

  • Power on your Raspberry Pi.
  • Open the Preferences > Raspberry Pi Configuration, select the Interfaces tab
  • Ensure I2C is Enabled

You only need to do this step for your first PiicoDev project - from here on you probably won't have to repeat this step when using PiicoDev hardware.

Let's get set up with scripting in Thonny for the Micro:bit. We'll install Thonny, configure for Micro:bit, and write our first script.

All you'll need to follow along is a Micro:bit v2 GO kit

Contents

Install Thonny

Download Thonny here and run the installer.

Once the installer finishes, run Thonny.

Set up Thonny

Connect your Micro:bit V2 to your computer with the USB cable.

connect-microbit-to-computer-with-usb-cable

Open Thonny, and in the menu bar find Run > Select interpreter and choose MicroPython (BBC micro:bit)

It's also a good idea to install or update firmware. This will update your micro:bit with the latest version of MicroPython, or install MicroPython if it wasn't already.

select-microbit-interpreter-in-thonny

Make sure the Files pane and Plotter are visible by selecting them in View > Files, and View > Plotter.

REPL interface (Shell)

Click the red STOP button to restart the MicroPython on your micro:bit if necessary. The Shell tab should display a block of text that indicates MicroPython is running:

thonny-shell-restarted

We can immediately start executing code in the REPL - Enter this code in the shell tab: print("Hello, World!")

The command will be sent to your micro:bit, which will execute the command and display back the message - nice!

Next, we can also take control of the on-board speaker by executing the following code:

from microbit import *
import audio
audio.play(Sound.HAPPY)

This code will play a happy tone from the micro:bit's speaker! If you the LED. If you keep executing audio.play(Sound.HAPPY), the tone will repeat.

microbit-thonny-repl-play-audio

Writing a Script

The REPL is great for test-driving some new commands and performing short experiments - the real power is in scripting though.

Create a new script with File > New and paste in the following code:

from microbit import *
import audio

print("Hello!")

multiple = 1 # initialise the counter

while True:
    if button_a.was_pressed(): # get the next multiple of the thirteen
        result = multiple * 13 # Calculate the result
        print("13 times " + str(multiple) + " is " + str(result)) # print the multiplication
        multiple = multiple + 1 # increment the multiple
    
    if button_b.was_pressed(): # Say Hello
        display.show(Image.HAPPY)
        audio.play(Sound.HAPPY)
        display.clear()
        
    sleep(10) # a 10 millisecond delay

Save the script - you will be prompted to save to your computer OR the micro:bit. Select save to micro:bit and name the file main.py

Return to the REPL and press Ctrl+D to restart your micro:bit. If something went wrong, use the Stop/Restart button, then Ctrl+D. 

Now, when we press the A button, the Shell will print the next multiple of 13, or if we press the B-button, our micro:bit gives us a smile and a hello sound.

thonny-example-script-for-microbit

Notice the plot is also showing some lines, and they're colour-coded to numbers in the print statement! In my case:

  • The Blue line is the constant 13,
  • The Orange Line is the multiple variable that increases slowly, and
  • The Red line is the result variable, which increases really quickly.

Useful Tips

  • You can stop a running script by selecting the Shell window and pressing Ctrl+C. This is useful if we want to make changes to the file(s) stored on the micro:bit.
  • Reboot your micro:bit (or other MicroPython device) by selecting the Shell window and pressing Ctrl+D
  • If something goes wrong, you can always click the red STOP button in the menu bar

Uploading and Downloading Files

We've been working with a file stored directly on the micro:bit - if you'd like to keep a copy on your computer, right-click the main.py file and select the 'download' option. Similarly, you can always upload code to your micro:bit by right-clicking files on your computer and selecting the upload button.

download-script-from-microbit-to-computer

Conclusion

We've installed Thonny and uploaded our first script to our micro:bit. The script includes branches depending on which button is pressed and can generate audio tones and perform basic arithmetic. Now we can write scripts, move them between micro:bit and computer, or test code interactively using the REPL.

If you have any questions feel free to start the conversation below or open a topic in our forums - we're full-time makers and happy to help!


Download / Install PiicoDev Modules

To work with PiicoDev hardware, we need to download some drivers. The drivers provide all the functions to easily connect and communicate with PiicoDev hardware. Select your dev board from the options above.

We will need these files to easily drive the PiicoDev Air Quality Sensor:

  • Save the following files to your preferred coding directory - In this tutorial, we save to My Documents > PiicoDev.
  • Upload the files to your Pico. This process was covered in the Setup Thonny section.

The PiicoDev Unified Library is responsible for communicating with PiicoDev hardware, and the device module contains functions for driving specific PiicoDev devices.

We will now install/upgrade the PiicoDev python module for Thonny. This module contains all the drivers to work with all current PiicoDev hardware. Even if you have already installed the PiicoDev modules before, it's still worth upgrading to the latest version if one is available.

First, run Thonny by clicking the:

  1. Pi Start Menu
  2. Programming
  3. Thonny IDE

We need to set up a virtual environment to install the PiicoDev module into. This only needs to be done once as Thonny will remember an environment you have already made. If you are unsure if you have already set one up, you can always create a new one by following these steps.

To set up a virtual environment, click on run  > configure interpreter, to open up the interpreter tab.

In this window, we can create a new virtual environment in the bottom right. A notification window will first pop up, just click OK.

In this window, we are going to create a new empty folder in our Home Directory which will be the location of our virtual environment. To do so follow these steps:

  1. Select the Home tab on the left-hand side of the window.
  2. Click the Create Folder button in the top right.
  3. Enter the name of the new folder. You can call it what you want, but we will call ours "myenv". Once you have written the name, click Create next to it.
  4. Click OK in the bottom left.

Thonny will then set up your virtual environment and when it has finished, it will return to the Interpreter tab. Click OK, and you will have successfully set up the environment.

Note: the Python Executable path now points to the environment we just created.

Remember, you only need to do this once as the next time you open Thonny it will use this environment.

Next, open the package manager. From within Thonny click Tools > Manage Packages

Enter "piicodev" and click "Search on PyPI"

Finally, Install PiicoDev. If you already have PiicoDev installed, there may be an Upgrade button instead, to upgrade to a newer version.

 

With the PiicoDev module installed we are now ready to begin programming with PiicoDev hardware.

We will need these files to easily drive the PiicoDev Air Quality Sensor:

  • Save the following files to your preferred coding directory - In this tutorial, we save to My Documents > PiicoDev.
  • Upload the files to your micro:bit. This process was covered in the Setup Thonny section.

The PiicoDev Unified Library is responsible for communicating with PiicoDev hardware, and the device module contains functions for driving specific PiicoDev devices.


Reading Air Quality Metrics

This example samples the Air Quality sensor for three air quality metrics: AQI, TVOC and eCO2. Run the following code to begin taking measurements.

 

# Read air quality metrics from the PiicoDev Air Quality Sensor ENS160
# Shows three metrics: AQI, TVOC and eCO2

from PiicoDev_ENS160 import PiicoDev_ENS160 # import the device driver
from PiicoDev_Unified import sleep_ms       # a cross-platform sleep function

sensor = PiicoDev_ENS160()   # Initialise the ENS160 module

while True:
    # Read from the sensor
    aqi = sensor.aqi
    tvoc = sensor.tvoc
    eco2 = sensor.eco2
    
    # Print air quality metrics
    print('    AQI: ' + str(aqi.value) + ' [' + str(aqi.rating) +']')
    print('   TVOC: ' + str(tvoc) + ' ppb')
    print('   eCO2: ' + str(eco2.value) + ' ppm [' + str(eco2.rating) +']')
    print(' Status: ' + sensor.operation)
    print('--------------------------------')
    
    sleep_ms(1000)

All going well, you ought to see some data formatted as follows: Three metrics for air quality and the operation status.

Referring to the example code, we can see that the aqi property has both a value (a number, in this case 2) and a corresponding rating (a description, in this case, "good"). Each can be accessed individually with .aqi.value and .aqi.rating respectively. Similarly, the .eco2 property has a value and rating that can be accessed with .eco2.value and .eco2.rating respectively. The .tvoc property returns a value only.

You may notice that the data drifts a fair bit during the first few minutes of operation - this is normal and expected. The ENS160 sensor requires an initial start-up and warm-up phase. The current phase is indicated by the .operation property. Read more about this in the Initial Start-Up and Warm-Up section.

Air Quality Index - AQI

The Air Quality Index (AQI) provided by the sensor is derived from a guideline by the German Federal Environmental Agency and is often referred to more formally as AQI-UBA. The index is based on a TVOC sum signal. Although local to Germany, this guideline is referenced and adopted by many countries and organisations. The index ranges from 1 (Excellent) to 5 (Unhealthy) and each index is accompanied by recommendations and exposure limits. The following figure is reproduced from the ENS160 datasheet.

piicodev-air-quality-sensor-ens160-aqi-uba-table-rating

Equivalent CO2 - eCO2

Equivalent CO2 has historically served as an air quality indicator since there is a well established proportionality between VOCs and CO2 generated by humans. This inference is historically motivated by the unavailability of VOC measurement technology in the 19th Century - and so an equivalent CO2 measurement was adopted as a surrogate for inhabitant-generated air-pollution. Many HVAC industry standards still refer to eCO2 for demand-controlled ventilation. An example ventilation application might select "Low", "Medium", and "High" fan speeds for "Fair", "Poor" and "Bad" ratings, respectively. These ratings are shown in the table below, reproduced from the ENS160 datasheet.

piicodev-air-quality-sensor-tvoc-rating-table

 


Remix ideas: Human Byproducts and Cooking Odours

In general, VOCs are more abundant indoors and come from a few main sources: humans (respiration, transpiration, and metabolism), and furniture or household supplies). All else being equal, we ought to be able to infer the presence of a human (or other large animals!) from the change in data. Give it a try! Here's a few experiments you might like to try: 

Kitchens are an abundant source of air contamination. Without adequate ventilation, smoke from oils and food can readily degrade the air quality. What's more, TVOCs are often odourous, and so you may even be able to track the concentration of 'cooking smells' by observing sensor data during cooking.

Bedrooms and bathrooms are great candidates for observing byproducts of human metabolism - they're generally contained volumes with people in them. Provided you're not violating anybody else's privacy, try tracking data over a day in these spaces.

The ENS160 datasheet supplies a fascinating figure (below) showing eCO2 values logged in a bedroom when windows are opened and closed, "cooking smells" in a kitchen, and body odours / bio-effluents in a bathroom.

 


Initial Start-Up and Warm-Up

The first ever time the PiicoDev Air Quality Sensor is powered on it will need some time before it can be expected to deliver good air quality readings. This is called the Initial Start-Up and lasts for 1 hour at most. If power is removed within 24 hours, the sensor will proceed with the Initial Start-Up again the next time it is powered up. Once the Initial Start-Up is complete and the sensor has run for at least 24 hours, the PiicoDev Air Quality Sensor needs only 3 minutes each time it is powered on to Warm-Up.

You can still read data from the sensor during the Initial Start-Up and Warm-Up times, though the output may not be as reliable.

You can check the current state of the sensor by reading the .operation property which will return one of the following descriptions:

  • operating ok
  • warm-up
  • initial start-up
  • no valid output

An example of showing the .operation property is below:

from PiicoDev_ENS160 import PiicoDev_ENS160
from PiicoDev_Unified import sleep_ms
 
sensor = PiicoDev_ENS160()   # Initialise the ENS160 module

while True:
    print('Operational Mode: ' + str(sensor.operation))
    sleep_ms(1000)

Connecting to multiple sensors

The following example reads data from two PiicoDev Air Quality Sensors independently. To do this, they each require a unique Address Switch (ASW) setting. Leave one with ASW:OFF and set the second sensor ASW:ON. Referring to the example code, we can see two instances of PiicoDev_ENS160(). Each sensor's instance is initialised with an asw argument which encodes the positions of the ASW switch (1=ON, 0=OFF).

 

 

# Read from two sensors independently
from PiicoDev_ENS160 import PiicoDev_ENS160
from PiicoDev_Unified import sleep_ms

sensor_A = PiicoDev_ENS160(asw=0)  # Initialise the first sensor with ASW:OFF
sensor_B = PiicoDev_ENS160(asw=1)  # Initialise the second sensor with ASW:ON

while True:
    # Read the sensors
    tvoc_A = sensor_A.tvoc
    tvoc_B = sensor_B.tvoc
    
    # print total organic volatile compounds
    print('A: ' + str(tvoc_A) + '    B: ' + str(tvoc_B))
    
    sleep_ms(1000)


Advanced Usage - Setting Temperature and Humidity parameters

The algorithms implemented by the ENS160 include terms for air temperature and humidity, which are used for compensating output data. In general, these can be left as defaults. If you have access to temperature and humidity data, you can provide this information to the ENS160 by setting the .temperature and .humidity properties. The following example sets the temperature to 25 degrees Celsius and humidity to 50%.  We use constant values in the example, but this data could be provided by temperature and humidity sensors.

#Set the temperature and humidity properties for compensated data.
from PiicoDev_ENS160 import PiicoDev_ENS160
from PiicoDev_Unified import sleep_ms

sensor = PiicoDev_ENS160() # Initialise the ENS160 module
sensor.temperature = 25.0  # [degC]
sensor.humidity = 50.0     # [%RH]

Conclusion

We're now well equipped to measure Indoor Air Quality (IAQ) with the PiicoDev Air Quality Sensor. We've performed basic readings of IAQ, and remixed code to perform some action depending on Air Quality Index. We're capable of running two sensors simultaneously and have observed how fumes may be detected. 

If you build on this starter-project - or just have some quesitons - continue the discussion below! We're full-time makers and happy to help.

Happy Making!

Resources

Documentation and Repos

Air Quality signal characteristics

  • TVOC: 0 - 65,000 ppb
  • eCO2: 400 - 65,000 ppm CO2 equiv.
  • AQI-UBA: 1 to 5

Have a question? Ask the Author of this guide today!

Please enter minimum 20 characters

Your comment will be posted (automatically) on our Support Forum which is publicly accessible. Don't enter private information, such as your phone number.

Expect a quick reply during business hours, many of us check-in over the weekend as well.

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.