Environmental

Air Quality

The ENS160 Air Quality Sensor is capable of sensing Volatile Organic Compounds (VOCs) and Equivalent CO2 (eCO2). Because of this, it is also capable of generating the AQI (Air Quality Index) through these readings.

The device requires a 3-minute ‘warm-up’ time before it can return reliable results. It will still return readings when polled, but this initial warm-up state is needed for it to establish baseline readings in its environment. Therefore, this sensor isn’t ideal for retrieving extremely rapidly-changing air quality readings.

The units used in class have already been calibrated so there is no need to let the sensors ‘burn in’ for 24 hours before use.

We are using Core Electronics’ version of the ENS160 sensor for this recipe (follow the link to learn more details)

Piicodev Air Quality Sensor ENS160.

Piicodev Air Quality Sensor ENS160.

I2C connection standards?

An increasing number and variety of sensors now use the I2C communication bus. Simply put, I2C sensors provide an easy way to connect it to a microcontroller.

The PiicoDev wiring is similar to a growing class of sensor interconnect standards such as Adafruit’s STEMMA, the identical pin order of Sparkfun’s QWIIC, or the slightly larger SeeedStudio’s Grove. Most of them are pin-compatible, but require specialty connector ends to fit. The healthy competition keeps innovation alive, but it can be slightly annoying finding the right cables to fit.

For PiicoDev sensors, you can connect the cable into either side of the sensor board. However the cable plug itself only goes into the socket one way, so exercise caution when connecting them.


Wiring & Code

Select a microcontroller platform in the tabs below to view the wiring and code meant for the platform:

  • Breadboard diagram

    Air Quality ENS160 sensing circuit

    Air Quality ENS160 sensing circuit

    Download Air Quality Fritzing file

    Libraries

    Install the following libraries via your Arduino IDE’s Library Manager, if you haven’t already:

    • SparkFun_ENS160

    Code

    For a refresher on how to use the code in this recipe, click here.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    
    /*
      Air Quality Sensor (ENS160)
    
      We are using the Core Electronics version of the ENS160, but the code library is referencing a Sparkfun Library.
      It's a good point to know – that sensors can be assembled by various manufacturers, but so long as the sensor
      model number is the same, you are likely to get compatibility with code libraries written for the same sensor.
    
      This sensor returns air quality readings of the following:
        Simplified Air Quality Index (AQI) – 1 = BAD, 5 = EXCELLENT
        Total Volatile Organic Compounds (ppb)
        CO2 concentration (ppm)
    
      In this sketch and wiring we are keeping things even more simple as a sensing recipe.
      As there are three metrics being provided by the ENS160 sensor, the sketch simply prints them
      out via the Serial port. Check the output in the Serial Monitor.
    */
    #include <Wire.h>
    #include "SparkFun_ENS160.h"
    
    SparkFun_ENS160 myENS;
    
    int ensStatus;
    
    void setup() {
      Wire.begin();
      Serial.begin(115200);
    
      if (!myENS.begin()) {
        Serial.println("Could not communicate with the ENS160, check wiring.");
        while (1)
          ;
      }
    
      // Reset the indoor air quality sensor's settings.
      if (myENS.setOperatingMode(SFE_ENS160_RESET))
        Serial.println("Ready.");
    
      delay(100);
    
      myENS.setOperatingMode(SFE_ENS160_STANDARD);
      ensStatus = myENS.getFlags();
      Serial.print("Gas Sensor Status Flag (0 - Standard, 1 - Warm up, 2 - Initial Start Up): ");
      Serial.println(ensStatus);
    }
    
    void loop() {
      if (myENS.checkDataStatus()) {
        Serial.print("Air Quality Index (1-5) : ");
        Serial.println(myENS.getAQI());
    
        Serial.print("Total Volatile Organic Compounds: ");
        Serial.print(myENS.getTVOC());
        Serial.println("ppb");
    
        Serial.print("CO2 concentration: ");
        Serial.print(myENS.getECO2());
        Serial.println("ppm");
    
        Serial.print("Gas Sensor Status Flag (0 - Standard, 1 - Warm up, 2 - Initial Start Up): ");
        Serial.println(myENS.getFlags());
    
        Serial.println();
      }
    
      delay(200);
    }
    
    /*
        Please note that the code provided here is licensed under the MIT license.
    
        The MIT License (MIT)
        Copyright © 2024 Chuan Khoo
    
        Permission is hereby granted, free of charge, to any person obtaining
        a copy of this software and associated documentation files (the
        "Software"), to deal in the Software without restriction, including
        without limitation the rights to use, copy, modify, merge, publish,
        distribute, sublicense, and/or sell copies of the Software, and to
        permit persons to whom the Software is furnished to do so, subject to
        the following conditions:
    
        The above copyright notice and this permission notice shall be included
        in all copies or substantial portions of the Software.
    
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
        OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
        THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
        OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
        ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
        OTHER DEALINGS IN THE SOFTWARE.
    */
    
  • COMING SOON…

    More code samples for this component for other platforms will be added here over time.


This page was last updated: 23 Sep 2024