Measuring Air Quality with ESP32 & Arduino

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today in the workshop we're working with air quality sensors you'll see how we can use these sensors to test the air for gases and particles using both an arduino and esp32 we'll even pollute the workshop in the interest of science there's something in the air today so welcome to the workshop [Music] [Applause] well hello and welcome to the workshop and today we are going to be working with a large bunch of sensors whose purpose is to sense the air and the environment around you these sensors can detect the presence of specific chemicals and can also measure the concentration of particulate matter at specific sizes many of these sensors are used in common industrial processes you can also use these sensors yourself for things like 3d printing and laser cutting but the most common usage for these sensors is to detect the quality of the air that you're breathing and that's a pretty important thing pollution is unfortunately a common thing many of us are not breathing high quality air and that problem isn't necessarily restricted just to those of us who live in urban areas so knowing what's in the air you breathe and knowing how to make the situation better is a very important thing so we're going to look at a lot of these sensors today we're going to look at them both with an esp32 and with an arduino but before we get started we need to learn something about how air quality is measured there's no denying that the quality of our air is continually deteriorating air pollution is responsible for over six and a half million deaths per year pollution can originate from industrial sources but could also be originating from natural sources such as forest fires we can divide air pollution into two categories outdoor pollution and indoor pollution pollution from items within our own homes often indoor pollution is a combination of both outdoor and indoor pollutants there are many different pollutants in our atmosphere ozone particulate matter noxious gases volatile organic chemicals polycyclic aeromic hydrocarbons and traffic related air pollution chemicals include nitrogen sulfur dioxide carbon monoxide ozone and nitrous oxide polluted air can aggravate or be responsible for many different medical conditions the air quality index is a scale that's been established to measure air pollution air quality can range from 0 to 500 and you want to have an air quality index of 100 or less in an ideal situation today we're going to explore a variety of sensors that can measure air quality each of these sensors measures a specific parameter there is no one sensor that measures everything many of the gas sensors use resistive elements these are elements to change resistance when exposed to specific gases our particulate matter sensor uses lasers to determine whether there are particles in the air we'll be using sensors that work with i squared c serial and analog interfaces so let's go measure the quality of our air now here are the sensors that we're going to use today and as you can see there are quite a few of them we'll start off with these sensors and these are the mq series gas sensors i've also got a few of them on the side here these sensors provide an analog output and at the moment these six sensors are connected to an arduino uno the rest of the sensors that i have are connected to this esp32 and we'll start off with this one this tiny little box is actually a sensor it's what's called a particulate matter sensor it senses little particles inside the air and this is a pm 2.5 particulate matter sensor which is a pretty standard device and it has a serial output so it's going to one of the uarts on the esp32 the rest of these are all i2c based sensors and i'll tell you what they are as follows this is the sgp40 which is an air quality sensor next to it we've got a bme 680 and a bme 680 is a temperature humidity and air pressure sensor right next to that we've got an sgp 30 and that's another air quality sensor it's also what's known as an mox gas sensor this is another temperature and humidity sensor it's an extremely accurate one it's the at8820 we have a cs 811 over here and it's another air quality sensor and finally we've got a bme280 another temperature humidity and pressure sensor and we'll be working with all of these sensors today now before we start working with all of these sensors i want to bring up the subject of calibration because it's a pretty important subject now the sensors we're working with today are all pretty accurate and they're all capable of being used in a measurement instrument however they need to be calibrated and the calibration process for every one of the sensors varies and can sometimes take a long time sometimes over 24 hours now because of that i'm not going to be able to perform calibration on the experiments that we're doing today but i do have information on the article that accompanies this video on the dronebotworkshop.com website and the information will tell you how you go about calibrating the sensor another handy thing to have is something like what i've got over here and this is a commercial air quality sensor and it can be used in order to calibrate the readings that we are getting from our other sensors but even if you don't have time to do the calibration and if you don't have access to a device like this these sensors can still be useful because sometimes we're not really concerned with the exact value of a measurement but just the fact that the measurement has gone up and exceeded a certain threshold and we can use that as an alarm or as a trigger in order to drive something and so really depending upon your application calibration may or may not be important but i wanted to mention that just so that you can know that the readings that you get from your sensor aren't necessarily accurate until you go through some sort of calibration process now the first sensors that we're going to be using today are the mq series of sensors these are very popular gas sensors you may have seen them before they're inexpensive and there is a whole line of these sensors and each one of them is sensitive to a number of specific gases now we're going to start off with an arduino uno because it's actually easier to use this sensor with a 5 volt logic device than it is with a 3.3 volt logic device however after we get it working with the uno i will show you how we can use it with an esp32 as well so let's learn something about the mq series of gas sensors the mq line is a popular line of resistive gas sensors there are different mq sensors for specific gases most of these sensors use a 5 volt heater and this heater can take a bit of current so you need to keep that in mind when designing your project the most popular way to buy mq gas sensors is on a module and the module provides dropping resistors as well as a comparator for the digital output the modules have an analog output whose voltage will vary with the concentration of the gas being measured the modules also provide a digital output that has a trigger point this can be used when you want to make a simple alarm with one of these sensors as you can see from this chart there are a wide variety of mq gas sensors each one dedicated to a specific gas or set of gases hooking up an mq gas sensor to an arduino or other microcontroller is quite simple the vcc on the mq sensor is connected to the 5 volt output of the arduino the sensor's ground is connected to the arduino's ground and the analog output can be sent to any of the analog inputs which on the arduino is a0 to a5 note that because of the current used in the heater of the sensor you can only power one of these from an arduino if you want to use multiple sensors you'll need to have an additional power supply for the heaters make certain that power supply shares a common ground with your arduino here's a schematic diagram of a typical mq gas sensor you'll notice the sensor off on the left side and its resistive elements are connected to pins one and three and six and four you'll also see a heater connected to pins two and five with a dropping resistor the output on pin 6 and 4 has a resistor that goes to ground and this is the analog output that we're going to be measuring now here's a chart of the resistance level of one of the mq sensors the mq-135 note how the resistance changes in the presence of different gases you can use these resistance characteristics to determine which gas you're measuring you should also note that the resistance will change due to humidity so a proper mq gas sensor reading will also factor in the current humidity so now let's start experimenting with some mq gas sensors okay well i've got an mq2 gas sensor wired up to an arduino uno for this experiment and it's a very simple wiring job i'm using five volts in the uno to power the gas sensor which you can do if you're only using one gas sensor any more than that and you'll want to provide a separate 5 volt power supply because the heaters in these actually consume quite a bit of current now the analog output of the gas sensor is going to one of the analog inputs on the arduino in this case analog input a0 is being used and so the easiest way to see if something is happening is this to monitor pin a0 and that's exactly what we're doing in this very simple sketch here we're just defining the pin we're defining a value for the sensor value we go into our setup set up our serial ports so that we can our serial monitor excuse me so that we can monitor what's going on then we do a bit of a delay over here that just allows the sensor to warm up for the heater to warm up and then we just go into the loop where we read the sensor value and we print it out to the serial monitor and give it a two second delay and do it over again so it's a very very simple sketch and of course you could use any of the pins other than a0 if you wish just change a sketch i've got it running right now i'm going to start it again by opening the serial monitor and that could restart my sketch there we go it's warming up and we're getting a sensor value now and that's just the analog to digital output so as you recall the arduino uno has a 10 bit a to d converter so that's 10 24 possible combinations over here now uh the sensor is supposed to be sensitive to a number of different gases including lpg which is liquefied petroleum gas propane hydrogen alcohol and to some degree methane and butane so i thought an easy way to test it would be with a butane lighter i'm not going to light the lighter i'm just going to press it down so it shows up some butane we'll see if that affects our value and it certainly does affect their value it just held it there and immediately went up to 500 and it dropped down so the higher amount of the gas concentration the higher the reading you're going to get the analog the digital converter of course this reading doesn't mean anything it isn't in parts per million we don't know how much lpg or how much hydrogen is in the air but there are ways that we can find out and that's by using a library there are a few different libraries that you could make use of for the mq gas sensor and the one that i'm using is called mq unified sensor and you can find that in the library manager just by typing in mqu up in the filter box once you find it just hit the install and we'll install that library okay it's installed and we can close and the library comes with a wealth of different examples so we'll just go into examples and we'll go down into the library examples and here's mq unified sensor and as you can see there are a lot of examples you can even get an alcohol meter up over here if you wanted to but you'll notice there are a number of different examples for the different mq sensor although it can be used with any mq sensor and there's one here for the mq2 so let's take a look at that one over here this makes use of the library now you need to go and take a look at some of the setup in here we are using the correct kind of board an arduino uno i'm using a0 so let's go and change that um it's an mq2 sensor that we've got here the resolution is 5 volts and the adc resolution is 10 bits on the arduino and this ratio over here is something you can actually determine through the calibration process there's another calibration sketch on here but if you don't know that value just use the 9.83 now if you go down over here you'll notice there's a couple of other values for mq2 set a and mq2 set b and they've got values in the brackets and the values you put in here will determine what gas it is that you're making it the most sensitive to and this one is configured for lpg so if you look down this table they've got here lpg is indeed 574.25 which they've got over here and a negative 2.222 which they have over here now i don't have any lpg but i do have a source of alcohol i've got isopropyl alcohol here so let's just go and change that let's just copy that value and set it over here and we'll copy this value as well which is always a negative value and we'll set it here and this way our readings will be biased toward alcohol okay so i've made some changes in it right now it basically is just going to initialize the sensor it's uh going to update its clean air ratio which was that number we saw above there so if you have a different number it'll do that and then it'll just go into a loop where it calls mq update and then reads the sensor and these are functions that are part of the library itself and so let's go and load that up to our arduino right now and we'll go to the serial monitor oh i believe the serial monitor here is at 9 600 yet i'm sorry my mistake and we start getting a ton of readings over here it's actually quite difficult to see on the screen because they go so far so i'm going to let it run and then just unplug it from the arduino but before i do that i'm going to add some alcohol to the picture and so this is isopropyl alcohol it's actually 99 and i'm just going to put it near the sensor and that could be getting us some readings but of course a lot of that's flying by on the screen it's very hard to see okay i'm going to move the isopropyl i'm going to pull the plug on this so we can go back and examine the data that we got and okay over here is the parts per million at the very end and you can see the readings we were getting at the very and then i added the little thing of alcohol in front of it it just skyrocketed and when i took it away it started to go down and so as you can see you get a lot of data out of these sensors and you can fine-tune everything to the actual gas that you want to sense so how about using the esp32 with the mq gas sensors after all the esp-32 is 18 analog to digital channels and these are 12-bit adcs so it sounds like it would be a pretty good choice the adc channels on the esp-32 are split into two groups labeled adc-1 and 2. if your project is going to be using the wi-fi or bluetooth capabilities of the esp-32 you'll want to restrict yourself to just using the adc-1 group as you can't use adc-2 along with wi-fi and bluetooth as they use the same resources however the esp-32 has a far bigger problem when it comes to mq gas sensors the esp32 is a 3.3 volt microcontroller whereas our gas sensor modules run on 5 volts now as we're using an analog output we can't use a simple logic level converter to solve this problem but there are three solutions that we could employ the first solution is simply to wire this up to the 3.3 volt power supply if you're only using one mq sensor you could power it directly from the esp-32 otherwise you could give it a separate supply with this solution everything will work but the readings will be inaccurate as the heater won't come up to the proper voltage however it's a good solution for most applications and it's certainly the easiest another solution is to use the 5 volt output of the esp32 to power the sensor this means the heater will run at the proper temperature the analog output of the sensor will be connected to a potentiometer in order to trim the level down this will be less accurate but with proper adjustment of the pot the accuracy can be reasonable a third and more complex solution involves modifying the module itself in order to do that let's take a look at the schematic for the module if we look at the module schematic we can see the actual mq gas sensor module here on the left the gas sensor has two components the first component is the heater and the heater is connected to vcc which in this case is 5 volts and goes to ground through a dropping resistor to limit the current the other connections are for the sensor itself and the sensor connections are on pins one and three on one side and six and four on the other side one side of our sensor is connected to vcc the other side drops through a resistor labeled rl over here and this produces a voltage divider whose output will change depending on the presence of gas as the gas sensor changes resistance that output is fed to the analog output now the problem here is that that output is going to go all the way up to 5 volts under certain conditions when the gas sensor resistance is low in order to resolve this we need to break the connection to bcc and connect the sensor itself to 3.3 volts this way the sensor will get the full heater voltage but the voltage divider will work with 3.3 volts and will produce an accurate output here's the hookup for our modified module we'll be using 5 volts to power the heater and we'll be using 3.3 volts to power the sensor now if you're using the mq-unified sensor library with your esp32 there are a number of changes you need to make to the code and these changes are identical regardless of which way you decided to hook them up the first change is pretty obvious the board type is going to be an esp32 after that we need to supply the port number we're using of course you could be using a different one but i'm using 34. the voltage resolution is now 3.3 volts and the analog to digital bit resolution is now 12 bits after making these changes the mqu unified sensor code should run correctly with the esp32 now the next sensor that we're going to check out is what's called a pm 2.5 sensor and pm is an abbreviation for particulate matter little particles that are inside the air that we breathe 2.5 refers to the 2.5 micron size of these particles and pm 2.5 is a standard for measuring air quality it basically measures a number of 2.5 micron particles within a given volume of air now the sensor we're using today is a very popular one and it's a fairly large sensor when you compare it to the other ones that we've used and that's because it actually has a small fan and a laser inside it to measure these particles now it's not only capable of measuring particles at 2.5 microns it can also give you outputs to let you know how many particles there are at 1 micron 5 microns and 10 microns as well so let's go and take a look at our pm 2.5 sensor the pm 2.5 sensor senses particulate matter which is a particularly common form of air pollution it can measure particles down to a size of 0.3 microns pm 2.5 and pm10 which are two and a half and 10 micron particles are standard units of measurement this sensor uses a laser for particle detection it provides a serial output at 9600 baud the sensor uses a 5 volt power supply however it is 3.3 volt logic compatible i'm going to be using the sensor with an esp32 and the hookup is pretty simple the ground of the sensor will be connected to the esp-32s ground the sensor's vcc will be connected to the 5-volt output on the esp-32 the txd or transmit data will be connected to pin gpio16 on the esp-32 the rxd or received data will be connected to pin gpio 17. if you wish you can use two different pins you'll just need to modify the sketch accordingly you can also use the pm 2.5 sensor with an arduino uno the connection is shown here i've shown the transmit and receive connected to uno's pins d2 and d3 but you can use any data pins as you'll require the software serial library in order to communicate with the sensor so now let's take a look at our pm 2.5 sensor now we're testing our pm 2.5 sensor which is this box over here and if you take a look at the box you can see a fan on it and a couple of ventilation holes there's a laser inside here that measures particulate matter now this is a pretty common sensor and it's made by a company called plant tower but it's distributed through a number of different distributors and i got mine from adafruit you can also get these from people like sparkfun and pimeroni and the only real difference in the distribution is the cable that they give you to connect to the box so that you can get it to serial connections as well as the power connections adafruit's cable comes with a little adapter that works very nicely on a solderless breadboard i've just got that wired up to 5 volts because although this is a 3.3 volt friendly device it requires 5 volts for the fan and the laser i've got the output and input of this connected to a esp32 using one of the uart ports because this is a serial device now there are a number of libraries that you could use in order to use this device however i had a problem with a few of them specifically the adafruit one in which it would run and then lock up after about half a day and at first i thought my sensor was bad but actually it turns out after googling i found it was a common problem and it's because many libraries are not looking for a specific byte that sometimes comes from the device and it kind of messes up their code i found the solution on a site called howtoelectronics.com and i'm quite grateful to them this is basically their code the only modification i've made is that their code was for an arduino uno so it used the software serial whereas i'm using the serial port on the esp32 and that's what i define right over here pins 16 and 17 are my receive and transmit data and you can define different pins if you want to just change these numbers over here now i'll go down into the code that they actually provided now they've provided the data structure over here so this is the data structure that we're looking for on the on the serial line and over here they've provided a function called read pms data and it's the special start byte over here that the other libraries are having problems with so this just takes a serial data and breaks it down and puts it into that data structure so in the setup all we do is we set up our serial monitor that we're going to use to monitor the output and we also set the serial port that we're using i'm using serial one and it's at 9600 baud we set the parameters for the port and the receive and transmit pins that we defined earlier then in the loop we just go and run the read pms data and take a look at the output and we print the output in a nice formatted fashion out to the serial monitor so i've got this running right now on this and i will bring up the serial monitor and you can see it gives a lot of different information it gives particles of all sorts of sizes down to 0.3 of a micrometer which is quite small and it does 0.5 1 2.5 5 and 10 and then it focuses those into the standard units because these measurements down here are for 0.1 of a liter of air whereas the standard unit is different and so you can take a look at the standard and environmental units to compare it and i've got my commercial meter over here now the commercial one is reading a fair bit higher i'm getting readings of like four or uh sorry about three or four yeah for the 2.5 and they're getting 7.4 in this meter and for the pm10 i'm reading a 5 from the sensor whereas i'm reading 11.9 so they're off by a fair bit now one thing is of course this hasn't been running for very long you literally need to run this for several hours before you can really trust the readings and that's also true of my commercial sensor over here so that may be a little off right now i've run this before and actually had it within one or two points of it i think the commercial sensor is accurate it is supposed to be calibrated but it didn't receive a calibration certificate nonetheless this does seem to be working and sensing particulate matter and by using this discrete code it's quite simple to use and if you've got an arduino uno just take a look at his original article and use the software serial instead now the next batch of sensors that we're going to look at have one thing in common and that's the interface to the microcontroller or microcomputer using the i2c or i squared c bus now these sensors measure a variety of different parameters and there is some overlap between certain sensors i also have a couple of sensors that specifically measure temperature and humidity and while you might not think the temperature and humidity have a lot to do with the quality of air those readings are essential in order to calibrate the other sensors so we need to get an accurate temperature and humidity measurement as well so let's go over to that big breadboard and take a look at half a dozen i squared c sensors in order to obtain the best performance from some of our other sensors will require accurate temperature and humidity readings so we're going to start off by looking at some temperature and humidity sensors the temperature and humidity sensors we'll be looking at today are the bme680 the bme280 and the aht20 the bme680 is a sensor that can measure temperature humidity and air pressure with reasonable accuracy it can operate from 3.3 to 5 volts dc and it has interfaces for both i squared c and spi we'll be using the i squared c interface in our projects today the bme280 has very similar specifications to the bme680 it also can operate on three to five volts dc and has interfaces for i squared c and spi note that the interfaces share the same pins and so you'll have to observe that during the wiring the aht20 is brought to you by the same folks who make the common dht sensors this is an extremely accurate temperature and humidity sensor with accuracy and temperature to .03 of a degree celsius it can operate from 2 to 5.5 volts dc and this sensor only has an i squared c interface we'll be using the i squared c connection for all of our sensors today and i'm using it with an esp32 the hookup is as follows the 3.3 volt output of the esp-32 is going to the vcc on the sensor on some of the sensors this is labeled v in the esp-32's ground is going to the sensor's ground we're connecting pin gpio 22 to the scl or sck line on the sensor this is the i squared c clock and pin gpio 21 is connected to the sda or sdi line on our sensor this is the data line for the i squared c data now note that although the esp32 is capable of using any pins for i squared c i would highly recommend you use the pins i've chosen here as these are the default pins and the libraries that we're going to be using with these sensors don't allow you to easily change the pins so keep things simple and use pins 22 and 21 for your i squared c connection so now let's start experimenting with our temperature and humidity sensors now here's my adafruit bme280 sensor right at the very top corner of my sawdust breadboard it's connected via the i2c bus and the easiest way to use this would be to install the adafruit library made specifically for the sensor so if we go into our library manager and type in bme280 it's the first thing on the list the adafruit bme280 library and we can just click install for that now if you've never installed any adafruit unified sensor libraries before you'll probably get prompted to install some other libraries as well just go ahead and do that because these libraries are dependent upon one another once you've done that you can go down into examples from custom libraries you'll see adafruit bme280 and bme280test is the one we're going to want to run and it's a very simple sketch it basically just shows you how you can use the bme280 library it shows you both the connections for the spi and the i2c which is why it's including the spi bus and why it has spi connections but over here they've not remarked the i2c and they've remarked out everything for the spi so if you want to use the spi connections on this you can modify it as well at any rate i've already uploaded it to my my unit so i've got it right now on the serial monitor and there we go this is basically what it's telling us the temperature the pressure the altitude and the humidity i agree with the temperature and the humidity i have no idea what the air pressure is currently in the workshop but i'm a little confused by the altitude figure because although the workshop is in the basement it's most certainly not 55 meters below sea level but otherwise it's a very easy sensor to use and if you need temperature and humidity it's a good choice so here's my df robot bme 680 sensor right at the edge of my solderless breadboard and in order to work with this we're going to install a df robot bme 680 library so i've gone into my library manager typed in bme680 and there are a number of different libraries for this sensor because it's quite a popular one i want the df robot one so i'm going to install that right now okay it's installed and i can close my library manager now let's go into the examples and run an example file now we'll go down over here here's bme 680 and i want the df robot bme 680 i2c sketch i'm going to open that up now i'll tell you you're going to need to make a couple of edits to this sketch the first one is they have an actual error in it over here where they call up the sensor they give it an i2c address of 77 hexadecimal it's actually supposed to be 7 6 so you're going to want to change that the other thing you're going to need to edit is that you need to put in your current altitude at least if you want to get all their altitude values correct it needs a reference one and they've put in 525 and that's in meters and that's for their location in china that's quite a bit above sea level compared to me i went to my city hall and i'm 36.03 and you probably have a source like a city hall website or something you can get this from now remember this needs to be in meters i'm in canada so mine was already in meters but if you're in the states you'll probably need to convert it from feet once you've done that you can upload it and i've actually already uploaded it to my esp32 so i'm just going to open the serial monitor and see what we get over here and we get temperature pressure and humidity readings along with altitude and calibrated altitude now again like all other sensors it takes a little while to settle down although the temperature and humidity readings should be correct right off of the box so if you need to get temperature humidity and perhaps a couple of other parameters this isn't a bad sensor to choose because it's pretty inexpensive and it's fairly easy to use now the final temperature and humidity sensor that we're going to test is the aht20 and i've got an adafruit board with it on it and if you take a look at this board which i have on my breadboard you might be able to see the sensor itself is almost a little island so to speak they've cut the circuit board around it and i believe that is for thermal stability now once again we're going to require a library and adafruit has a library for it the ahtx0 library which you can find just by typing aht20 into the filter in your library manager now this library will install some additional libraries as well you need to install all of the dependencies so go ahead and do that and once it's installed you can close and as usual we'll go in and take a look at an example for the library and here it is right here and the aat test is the example it's a very simple example it shows you how easy it is to use this we basically just include the library and define an object and then uh after words we just go to ahtgetevent we want the temperature and the humidity and we can go and print those out so it's a very simple sketch i've got it running right now of course and we'll open the serial monitor and it gives us back basically the temperature and the humidity and again this is supposed to be a very accurate sensor i have no reason to disbelieve the two readings that i'm getting right now that boat jives with what i figure it is in the workshop and so if you use if you need a temperature reference for another sensor this is an excellent choice for you the sensors that we're going to look at next build themselves as being air quality sensors and they measure various parameters to determine the quality of the air that we're breathing we're going to be looking at the ccs-811 the stp-30 and the sgp-4z the ccs811 is an older sensor that has been very popular in many projects before using it in a new project however you should note that the ccs811 sensor itself is no longer manufactured as the company that made it is no longer in business however there are many modules that are still existing that use this sensor it's capable of measuring co2 as well as the total amount of volatile organic compounds in the air it operates on 3.3 to 5 volts dc has an i squared c interface and a warm-up time of only 15 seconds the sgp-30 is another very popular sensor that can measure the total amount of volatile organic chemicals as well as co2 it operates on 3.3 to 5 volt dc and has an i squared c interface the disadvantage with this sensor is that it requires 24 hours to warm up before the readings are considered to be accurate the stp-40 is an improved version of the stp 3-0 its output is a voc index which is very similar to the air quality index that we discussed earlier it can also measure ethanol it operates on 3.3 to 5 volts dc and has an i squared c output one of the biggest advantages of the stp-40 over the stp 3-0 is its warm-up time is a mere 10 seconds so let's start experimenting with these air quality sensors the first air quality sensor that we're going to test is a df robot ccs811 and the ccs811 was a very common chip for measuring air quality however the manufacturer who originally created this is no longer making them and they've sold off all of their old stock and so i don't think this would be a very good choice for a brand new design we're going to mass produce but you can certainly find these sensors they're quite inexpensive and if you can get a hold of one there's certainly no reason not to use it in your project now as always we're going to require a library for it and i'm going to be using the df robot ccs811 library because i have a df robot implementation of this sensor so i'm going to install that and then we can close the library manager and go into our examples and here we are down here and read data is a good example the show and it's a very easy sensor to use oops i lost my there we go and basically you just include the library and you can specify uh the i2c address in there if you need to but it just uses the default one otherwise and it's very easy to use the sensor itself just if it checks the data ready is true you can print back a bunch of values but get co2 ppm and get tvoc ppmb so it gets you the carbon dioxide and parts per million and the total volatile organic chemicals in parts per billion now i've been running this for a little while right now and so let's bring it up on the serial monitor and these are the readings that i'm getting and we can also take a look at my commercial meter but unfortunately they seem to be in different units my tvoc over here is in parts per billion whereas this one here is in milligrams per meter squared and i'm not sure in the conversion but one thing i thought i'd try is i've got myself a little bit of paint over here i'm going to spray it into this little cup very carefully because i don't want to paint the workshop and we'll see if that registers in the meter so let's let me do this and bring it in here and you can see the meter at the bottom there is this filling with it interestingly enough my commercial one hasn't really gone up all that much and yet i can certainly smell the paint which means i'll probably have to evacuate the workshop for a little while but that's quite interesting the sensor over here most definitely did respond to it when was first in the air and now i'm holding it near the sensor and you can see the reading is this skyrocketed so it certainly is sensitive to that you can see my commercial one is gradually going up over here it's actually kind of surprises me i'm not getting more readings on that um and there's a histograph on this over here too and you can see it's rising a little bit at the bottom actually the ccs811 seems to be more responsive than my commercial meter so it is proof that it definitely is capable of detecting volatile organic chemicals and if you can get a hold of one it's a great choice for a new design but just not one that you're going to mass produce now the next air quality sensor that we're going to work with is the adafruit sdp-30 and the stp-30 was a common and still is a very common air quality sensor and a number of manufacturers make boards for it now once again we're going to be using a library and i've got sgp30 filtering in my library manager and we can see an adafruit sgp30 sensor so let's install that library and it's installed we'll close our library manager and we'll go and we'll run an example and there's only one example it's the sgp30 test so we'll bring that up and a couple of things to note inside this example this sensor can improve in its accuracy if you give it the actual temperature and humidity so that would be a good job for something like the ht20 to get that and if you can get that you can go and modify the sketch in order to take the absolute temperature and humidity they show you that down over here so you can unremark these values and plug in the values that you're getting from a proper sensor another thing is that this sketch establishes a baseline and it periodically updates that baseline if you have a method of storing that let's say eeprom or something you can store that and bring that up when it first runs because otherwise it's got to run for about 10 minutes or so before it can start producing any kind of readings now i've been running mine for a little while so let's go and take a look at it on the serial monitor i'm actually getting readings on it right now now the baseline reading for co2 by the way is 400 so that's indicating that i hardly have anything over the baseline and that may not be accurate the way you're supposed to get that again is to run it establish a baseline and do that in an area where you aren't expecting any uh any co2 and you should get a reading of 400 and then this reading will be more accurate you can also see that it gives some other readings in addition to the tvoc and co2 now once again the unit over here is parts per billion so i'm not sure how this compares to my commercial one which is reading the total volatile chemicals down over here using a different unit but it does seem like a very easy unit to work with and it brings you back a lot of parameters about the quality of your air now the stp-40 is an improved version of the stp 3-0 and it measures the quality of air the tvoc and it gives you a tv oc index as an output one of the features of this sensor is that it's self-calibrating and that it comes up to speed fairly quickly as opposed to other sensors and once again of course we're going to require a library for it so i've got the df robot stp 4-0 so i'm going to use their library and i'm in my library manager and i'll install that and we'll go run an example right now and the only example they have is get voc index and it does exactly what you think it would do and it's very simple code you literally include the library you create an object and you just query the object most of this code is acted as comments and it's going to initialize and print itself up on the serial monitor so once again i have this running already let's bring my monitor up and it gives you a voc index and it takes a little while to stabilize but a little while means just a couple of minutes rather than just a couple of hours and right now it's reading my voc indexes 97 again when you compare that to my commercial meter it's a completely different reading but the voc index actually has values 0 to 100 are considered to be excellent and over 100 is considered to be good so i'm sitting on the border line between excellent and good right now here in my enclosed workshop and that's probably about correct and so this is a very easy sensor to use and a great sensor to use if you want something that doesn't have to sit for a long time to require calibration this thing can just get up and go right away okay we've looked at a lot of sensors today and we've run sample code for each of them but now it's time to put everything together to get a good indication of the quality of the air that you're breathing you really need to take a number of different measurements and so what we're going to do is put all of our sensors together into one big sketch the grand poobah of sketches and we're going to run it on an esp32 and measure all of our sensors at once so let's go and take a look at that sketch now now here's the sketch that i'm using in order to monitor a number of the sensors at the same time using the esp32 and i'm not using every one of the sensors on the board after all i've got three temperature and humidity sensors on there but i'm using a number of different sensors i'm using the pm 2.5 sensor and of course that connects with the uart we're using the sgp 30 air quality sensor the bm 680 temperature and humidity and air pressure module as well as the aht20 now both of these measure temperature and humidity but the aht20 is much more accurate so we're going to use that for temperature and humidity and we'll just take the air pressure out of the bme 680 and we're also using the stp 40 air quality sensor and we're going to use the temperature and humidity readings from the aht20 to improve the performance of both the stp 30 and the stp 40. now for the pm 2.5 sensor we're again using those functions from howtoelectronics.com instead of a library for everything else you'll have to have the library loaded so we start off by including those libraries obviously the wire library for i squared c and then the libraries that we need for all of our various sensors the last library we're including is the preferences library the preferences library is included when you install the esp32 into your arduino ide so you don't need to download any other library from the library manager what preferences does is it works with the eeprom in the esp32 and this replaces the old ee prom library with preferences you can store data as a name uh value pair and so you store a number of these name value pairs and it's perfect for configuration data and we're going to use it for the baseline data for the stp 30 because the sdp 30 needs to calibrate for about a day and it adjust its baseline you'll notice when we ran the test it kept adjusting it if you save that and load it at the beginning you won't have to go through that calibration process again so that's what we're doing the rest of these are basically connections to the serial over here and some variables a number of variables and then the objects that are going to represent all our various sensors so we define all of those over here here's the local altitude again so you'll have to substitute your own inside here and that's in meters and here are the variables we're going to use for the co2 and tvoc base for the sg30 so those are the calibration ones now we come to a couple of functions and these have basically just been copied out of the samples that they gave us with the library so this is the one to get the absolute humidity and that came from the sg30 library and here's the pm 2.5 that we got from howtoelectronics.com the data structure the object and the function to read that data so that's all over here now we can jump into the setup and see how we set all this up we start off by beginning the serial monitoring giving it a little delay we print to the monitor then we open up a namespace for preferences and this is what's required when you're using the preferences library you need to give it a namespace false means that we can read and write if we had it true we could only read from it we're going to apply another delay we need to apply a number of delays to let the sensors get ready set up the uart connection for the pm 2.5 sensor and then we connect to the various sensors first we start with the bm680 and then we go down and start with the aht20 we do the stp 30 and when we do the stp 30 we go and check to see if we have any values already in eeprom and if these values are there we assign them to the variable tvoc base and then we write that into our sensor so we can tell the sensor to start off with preset calibration values so with preferences you just use a get command and this is get you int because these are unsigned integers and there's a get command for every type of variable so there'll be a get integer get boolean etc etc and we're just getting the name value pair that is named tvoc and we'll get its value back if there's nothing in there which there won't be when you first start we can apply a default value now over here i have a zero and if you have no other value to use use zero then it'll start to configure itself when it first starts and within 24 hours you'll have a good reading if however you've been running the demo program you probably already have a value for tvoc and eco2 and you'll have that in your serial monitor you could copy those values and put it here and then your sensor could just take advantage of all the training it's already had so you could change that if you need to then after that we start the stp 40 and we apply at the very end the five second delay once again just let all the sensors warm now in the loop we're going to get the data from the aht20 and that's the temperature and humidity data and we're going to apply it to our variable we're going to start the bme 680 now we compensate the stp 30 for the temperature and humidity that we just read and we get data from the stp 30. now here's the increment of the counter if you remember in the sample code they do this every 30 readings they go and take a baseline reading and see if they need to adjust it we're going to do the same thing however a lot of the times you're going to take this reading you're going to get the same values before and we don't want to write to the ee prom every time because you really shouldn't write to an ee prom that often if you can avoid it so we're going to check to see if it's a new value we do that over here if the value hasn't changed we just ignore it but if it's new then we go and we do a put you int and that's the opposite of the get we can just write to the name value pair tvoc with our value the same with eco2 so we update the eprom but only if the value is changed now here's the stp-40 we're compensating it for temperature and humidity and we're going to get the voc index from it and the rest of this is just printing everything we're going to print this out on the serial monitor i've got this divided by the data from the sensor so we start with the sdp 30 data the 40 the ht20 etc etc if you're wiring this up and not using all of the sensors you can eliminate this and you can of course eliminate the code associated with it in the above routine so you don't have to use all of the sensors i'm using but this will print everything out to our serial monitor and then of course this is the loop so we'll go and do it again so that's the sketch we're going to be using i've already got it loaded and it's been running for a while so let's take a look at the results on my serial monitor so i've got the serial monitor open and you can see the results of my multi-sensor array now the top two readings tvoc eco2 these readings should be relatively accurate as i've been running this for quite some time and i also have a baseline number generated which you can see at the very bottom now once again when you first run this it'll take about 10 minutes before you get any tvoc reading and eco2 will sit at 400 which is its baseline and it'll stay over there until you get a reading i've got the other readings displayed down there including all the readings from the pm 2.5 sensor and over here i've got my commercial sensor running and it's in pm 2.5 mode as well although of course the units are slightly different the top reading here is the pm 2.5 and the bottom is pm10 and if we look at the pm 2.5 and pm10 readings the standard environmental ones you'll see that my 10 is a little bit higher than the 2.5 and that's borne out by what's on my meter now what i'm going to do here is i'm going to intentionally foul the air now the sensors take a little while before they pick up on the foul there so i'm going to make it pretty polluted here in the workshop and i'm going to do that with a little bit of solder so we're just going to make a big mess and try to get a bit of smoke around here that the sensors will pick up and again they take a few moments before they start reading on that and i'll just foul it a bit more i normally have the doors open in the workshops with some ventilation but there isn't at the moment any ventilation so we should start to see results pretty soon over here and actually we're certainly seeing it on our sensor it's on the board right now if you take a look at the pm 2.5 and pm10 they've just skyrocketed from what they were before uh the voc index hasn't really gone up that much the commercial one hasn't moved yet but it actually takes almost a minute or so before it really registers anything toxic so let's just give it a moment and see what happens to it okay and as you can see right now the meter has actually risen up to the point where it is now considered to be hazardous to be here in the workshop again i've got the doors both closed here so there is no ventilation so i'm going to open those and clear this room out a little bit maybe put my fume extractor in here and get rid of all the solder smell but as you can see both the commercial sensor and the one that we have over here in this case the pm 2.5 sensor do agree that the air quality is certainly deteriorated here in the workshop you can still see the readings that i'm getting here on the screen they're not particularly healthy and it does prove out that these sensors are a very useful thing that you could use for a monitor for an alarm type of system in order to trigger something like a fan or an air cleaner or something and uh since i'm using an esp32 of course there's always the option of using wi-fi and bluetooth to make this into an iot type device so all in all these sensors are pretty easy to work with and they're very versatile and so that wraps up our look at air quality sensors i hope that you enjoyed it and i also hope that you found it educational and inspirational in the sense that it's inspiring you to either build an air quality sensor or perhaps some sort of an air quality alarm you can use these sensors to detect when a specific gas has exceeded a certain concentration and either raise an alarm or perform an action such as turning on a fan or opening up a vent there's a lot of possibilities when you interface these devices to microcontrollers now if you want a bit more information about these sensors or if you want to get some of the code that i use today you will find everything in the article that accompanies this video on the dronebotworkshop.com website and there is a link to that article right below the video and while you're on the website if you haven't signed up for my newsletter yet please do so it's not a sales letter by any means it's just something i send out occasionally to let you know what is going on here in the dronebot workshop if you want to discuss these sensors a great place to do that of course is on the dronebot workshop forums where you'll find the number of like-minded individuals who love to talk about electronics and i'm sure there's probably a few people out there who know a lot more about these sensors than i do so we're bound to have a few interesting conversations on the forum and the forum is free to join as well of course and finally if you have not signed up for the youtube channel please subscribe it's very easy to do all you need to do is hit that red subscribe button and then also click on the bell notification so that you will be notified every time i make a video that is as long as you have notifications enabled on your web browser and your youtube there's a lot of things for you to do up there at any rate please take care of yourself please stay safe please use these sensors to keep yourself safer and i will see you again very soon here in the dronebot workshop goodbye for now [Music] do [Music] you
Info
Channel: DroneBot Workshop
Views: 72,539
Rating: undefined out of 5
Keywords: air quality monitor, air quality sensor arduino, air quality sensor esp32, particulate matter, mq gas sensor, sgp30 sensor, sgp40 sensor, bme280, bme680, ccs811 air quality sensor, aht20, pm2.5 sensor
Id: GF27dp4RA0g
Channel Id: undefined
Length: 61min 13sec (3673 seconds)
Published: Fri Sep 23 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.