Let's get started measuring indoor air quality with the PiicoDev Air Quality Sensor (ENS160) and a Micro:Bit v2. 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, such as 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.

I'm going to show you how to get started with the PiicoDev Air Quality Sensor and a micro:bit V2. We'll connect these together and measure some air quality metrics, but first a little more about the sensor. It has two connectors for daisy-chaining connections with other PiicoDev Hardware. There's an address switch labelled ASW, which is used for uniquely addressing a second air quality sensor. Leave the switch in the off position for now. Across the bottom, there's also a breakout header for more experienced makers.

To follow along, you'll need a micro:bit V2 and the following PiicoDev Hardware: an Air Quality Sensor Adapter for micro:bit and a PiicoDev Cable to connect everything together. Plug your micro:bit into the adapter and make sure that the buttons are facing up. Connect one end of your cable into the adapter and the other to your air quality sensor, and again make sure this ASW switch is off. Then connect to your computer with USB.

We're going to program our micro:bit with MicroPython using Mu. If you've never done that before, check out the article for help getting started. To move along, find the download section in the article and select your dev board, micro:bit. Right-click each of the links and select ‘Save Link As’. I'll save PiicoDev Unified to a PiicoDev directory in my documents, and I'll do the same with PiicoDev ENs 160. Open Mu, navigate to where you saved the files, and drag them into Mu.

Once you have those two files connected to your micro bit, select both files, right click and upload to microbit. There they are! Once that's complete, head back to the article and scroll down to the first example reading air quality metrics. Copy all of this code with Ctrl C and paste into a new script and click the Green Run button. I'll save this to my MicroBit as main.py. All going well, you should see some air quality metrics showing up in the Shell.

Let's go through them in turn. I'm getting a Qi of three which is moderate, t-voc about 300 parts per billion, CO2 which is about 800 parts per million and the status which is currently in warm-up. AQI is an air quality index that ranges from one (excellent) to five (unhealthy) and we can see that there are recommendations for ventilation and exposure limits. While this is a German standard originally, it is recognized internationally and used in industry and since the numbers range from one to five, it's very easy to make logical statements from these numbers. For example, if greater than three then we need to ventilate.

Equivalent CO2 is another metric that's used in industry and it's basically using the multi-gas readings on the chip to create another single reading, the equivalent CO2. It's not measuring CO2 concentration directly, but it's inferring it from the concentration of other gases and again we have some brackets with a descriptive rating and some ventilation recommendations.

As for t-voc or total volatile organic compounds, this is basically what the chip is measuring. It's a multi-gas sensor and this is like the app output of that sensor. Now let's have a chat about this status. Initially when I powered on my device it was warming up and it's now operating okay. Your device may have been reporting that it's in the initial startup phase.

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.

Time for a closer look at the code. At the top of the script, we import the device driver for the onboard Ens 160 (that's the sensor). We also import a function to create a delay. We call the Sensor Initialization function which returns a Quality Sensor object and we assign that to the variable sensor. So anytime we see sensor in this code, we're referring to this physical device.

Then there's an infinite Loop and we submit some queries for the air quality properties. We read sensor.akui and store that in an aqi variable, sensor.tivoc and sensor.eco2, and then we print all the data. Aqi is a tuple of both a value (that's the numerical one to five) and a rating (that's the description that we get in these brackets). So here in the print statement, we have aqi.value and aqi.rating. That can be really useful because now we have a number to use with say switch logic, but we also have a description that we can use as a helpful print statement. Tivock is just a value, so we can just print t-voc and similarly with eco2, we have a dot value and dot rating component of that named tuple.

And that's how we can get the numeric value and the description. Let's actually put one of these values to use and make an air quality alarm using the micro bit. We can import the music module. This will allow us to play some pre-made sounds.

I've opened up the MicroBit documentation and what music shall we play? Let's go with music.dot.chase and see what that sounds like. Music.dot.play music.dot.chase and maybe we'll sleep for three seconds. Run the script.

Okay, the aqi is currently three. Now I have here a bottle of cleaning alcohol and if I open this up, this should register as a sudden spike in air quality or a sudden change in air quality. There it goes. Okay, so we transitioned to an aqi of four which is poor and we had an alarm. Now that I've moved it away from the bottle of alcohol, the aqi has improved and now if I slowly bring it closer to the bottle, we should see that happen again.

It's possible to have up to two air quality sensors on the same PiicoDev bus. This is how you do it. They are each going to need a unique ASW or address switch setting. For my first sensor, we need the address switch to be off and for the second sensor, we need the address switch to be on. I'll build that now. Here's my second sensor and the address switch is on.

Now I can copy this example code and paste it into funny. Now when I run this code, there are two numbers being printed A and B. These are the t-voc values as sampled from each sensor independently. Here in the print statement, we have t-voc a which is the first sensor and t-voc B which is the second sensor. And if I bring that bottle of alcohol back and bring some of the vapors down, we can see that we can read those two independently. There's a little bit of a difference in the readings and that's probably because one of my senses would have already been warmed up and the other is only in its initial startup phase.

We read two senses independently in code. There is a separate initialization statement for each sensor. For sensor A, we are passing in the argument ASW equals zero because the address switch is off. For sensor B, with the address switch on, we have ASW equal to one. Now we have two instances of air quality sensors in our code. Sensor A refers to this device and sensor B refers to the other device. We can sample sensor A.tvoc and sensor B.tvoc to get two independent values.

Getting started with the PiicoDev air quality sensor, we were able to read three separate air quality metrics and we were even able to remix the example code to create a fun air quality alarm which could be quite useful. Of course, we can read from two devices at the same time.

If you make something cool from the startup project or you just have some questions, let us know on our forums. We're full-time makers and 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.