Let's get started measuring indoor air quality with the PiicoDev Air Quality Sensor (ENS160) and a Raspberry Pi. We'll measure air quality and take a look at the different metrics available.

Transcript

The PiicoDev Air Quality Sensor is a multi-gas sensor that detects volatile organic compounds, things like ethanol, oxidizing gases, and even byproducts of human respiration and metabolism. The sensor is capable of sampling these gases and producing outputs for total volatile organic compounds equivalent CO2 and air quality index. These metrics are commonly used to monitor indoor air quality and control ventilation systems.

Today, I'm going to show you how to get started with your PiicoDev Air Quality Sensor and a Raspberry Pi single board computer. We'll connect these two together and get some example code running to read different air quality metrics.

First, a closer look at the sensor. The PiicoDev Air Quality Sensor features two PiicoDev connectors for daisy-chaining connections with other PiicoDev hardware. There's an address switch labelled ASW; this is used for uniquely addressing a second air quality sensor. Leave the switch in the off position for now.

To follow along, you'll of course need a Raspberry Pi singleboard computer set up to run like a desktop computer. Check the article if you need help getting started. There's a PiicoDev Air Quality Sensor, of course, a PiicoDev Adapter for Raspberry Pi, and a PiicoDev cable to connect everything together. Connect the adapter to your Pi and, on a Raspberry Pi 4, there will be an arrow pointing towards the Ethernet connector. Connect one end of your PiicoDev cable to any of the sockets on the adapter and connect the other end to your air quality sensor. Make sure this ASW switch is off.

Once again, I'll connect my keyboard and mouse, a network lead, display, and finally power. With your Pi booted up, first make sure that the I2C interface is enabled. Open the Pi menu, preferences, and go to Raspberry Pi configuration. Just make sure, under the interfaces tab, that I2C is enabled. represented in parts per billion

I2C is set to enable, so next open Thonny from the programming menu and make sure you're running the latest version of Pico Dev. Go to Tools > Manage Packages and search for PiicoDev (that's with two eyes). Here it is and when you click through make sure that you have installed or upgraded as necessary. In the article for this tutorial, find the example reading air quality metrics and highlight all of that code and paste it into a new script in Thonny. We're ready to run, press the Green Run button and if prompted to save, I'll just save this as air_quality.py and I'm saving that to a PiicoDev directory in my home directory. Click OK and here we have some air quality data streaming through the shell. If I wave some error over this, we ought to see that data change. I'll stop the script so it stops scrolling and let's have a look at this data. We have something called AQI which has a value of 2 which is good. We have TVOC which is 135 parts per billion, ECO2 which is 605 parts per million and that's good, and we have status warm-up.

Let's have a look at each of these metrics in turn. Air Quality Index (AQI) is an index that was developed in Germany but is recognized internationally. It's a scale of one to five to represent the air quality. We can see one correlates to a rating of excellent and 5 to a rating of unhealthy and there's also a few exposure and ventilation recommendations. Similarly for equivalent CO2, this sensor doesn't directly measure CO2 but infers the concentration based off other volatile organic compounds. ECO2 is also used in industry and has similarly a rating and a ventilation recommendation range. As for TVOC or total volatile organic compounds, this is really what the sensor is measuring. It's measuring the concentrations of different gases and this is represented in parts per billion. get the air quality index

The PICAdev air quality sensor has three states: initial startup, warm-up, and operating. When the sensor first powers on, it will be in the initial startup state which lasts up to an hour. You can still use your sensor right away in this state, but the accuracy will be improved once the sensor has warmed up. If the sensor is allowed to run continuously for at least 24 hours, it will never need this long initial startup process again. Instead, it will perform a much faster warm-up which only lasts a few minutes.

Let's do an experiment. I have some sanitizing alcohol here which will produce a fume, a volatile organic compound. If I uncap the lid and waft some of those vapors down, look at that - immediately we have an AQI of five or unhealthy. If I remove the alcohol from the equation and clear the air around the sensor, we should go back to a much more suitable air quality index. You can see that data correlate in the other two ratings as well.

Let's look at how the code works. On the first line, we import the device module, the driver for this sensor, the ENS160. We'll also import a function to create a short delay. Next up, we call the initialization function which returns a sensor object that we assign to the variable sensor. So anytime in the script where you see sensor, that is referring to this physical air quality sensor. Then there's an infinite loop and we query sensor.aqi to get the air quality index. chain

We can get the Air Quality Index (AQI) property from the sensor.tivoc and sensor.eco2 and store them in equivalent variables. Now, these return more than just a number. You can see from our prints that AQI is returning a value which is in this case 2 and a rating or description which is good. So when it comes to our print statement, we can print AQI and then we can print the value of the AQI, the numeric value, but we can also print AQI.rate which is that English word description and that's really neat because it means we don't need to do any logic, we can just extract the number or in words what the rating is which can be useful for feedback or for data logging.

Tivock does return just a number so here we just print the value of t-voc but similarly for AQI, eco2 is present with a value and a rating. And so since air quality is often used to drive say ventilation or to create some kind of alarm, let's do a Code remix. Let's make it such that the Raspberry Pi will remind us to turn on the fan if the AQI goes above say three.

I will remove all of this code and keep the AQI line and we'll just add the following logic: if AQI.value is greater than three, print "turn on the fan". We can rerun the script and hey, we have an AQI that is one excellent. But if we introduce our volatile organic compound sample and I'll just waft some of those vapors over the sensor, immediately we've shot from one up to five and we should turn on the fan.

It's possible to read up to two air quality sensors on the same PiicoDev bus. The only requirement is that they each need a unique address switch setting. This is my second air quality sensor and I've set the address switch to on. So now I have my original sensor which has its address switch off and my new sensor with this address switch on and I can add that to the end of my daisy chain.

We can find the code example for connecting to multiple sensors and we can see the diagram here where we have two sensors each with a different address switch setting. I'll copy the example code and paste it into a new script. I'll save that as multiple_sensors.py and we'll run this script now in the Shell. We have two values being printed, A and B. A is the value coming from our first sensor and B is the value coming from our second sensor. Here we're reading the total volatile organic compound and the numbers are quite different because this sensor has already been through its warm-up and this is a brand new sensor that's probably still in the initial startup phase.

With similar Imports at the top of the script, we now have two sensors and so we're going to call the initialization function twice, one for each sensor. Sensor A, our first sensor, is being initialized with the address switch off and sensor B, the second sensor, is being initialized with the address switch on. Here the zero and the one refer to the state of that switch.

Now we have two sensor objects in our code and so we can read sensorA.tvoc and sensorB.tvoc. We can access the unique properties of each unique sensor independently and finally we just have a print that prints the value as shown from each sensor.

And so there you have it. We were able to connect a PICAdev air quality sensor to a Raspberry Pi and read three different air quality metrics. We even did a Code remix where we could change the state of something based on the air quality. In this case, we were able to print a helpful message when the air quality became too poor. And of course, we can read two sensors independently.

If you make something cool from this startup project or you just have some questions, let us know on our forums. We're full-time makers.

Happy to help until next time. Happy making!

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.