Introduction: CPC Class Polution Checker

About: I'm a Computer Science Student @ Howest. I love programming, making, exploring and people!

Hi there, I'm a student from Belgium and this is my first big project for my bachelors degree! This Instructable is about how to make an airpolution meter for closed rooms, especially classrooms!

I hear you thinking why this project? Well, it all started when I went to high school. In the afternoon after a good lunch and a break, lessons start again. But there is a problem, the theacher forgot to open up the window while we had lunch so it is hot, sweaty and you can't concentrate because you are falling a sleep. That's because there is to much CO2 in the air.

My project will resolve this and make al students more concentrated while in their lessons.


1 xRaspberry Pi 4 (€55)

1 x steppenmotor with driver (€5)

2 x 12v 6800 mAh batteries (2x €20)

2 x stepdown module (2x €5)

1 x 16x2 LCD (€1.5)

Sensors: 1x MQ8, 1x MQ4, 1x MQ7, 1x MQ135, 1x HNT11, 1x TMP36 (1 x €23)

IC's: 1x MCP3008, 2x 74hc595AG (1x €2.30, 2x €0.40)

LED's: 2x green, 2x red, 3x yellow (found in some old hardware, normaly €0.01 each)

Connector for the batteries (2 x €0.35)

40 f-to-f connector cables (€1.80)

40 f-to-m connector cables (€1.80)

20 m-to-m connector cables (€1.80)

2 x PCB to solder on (2x €0.70)


Soldering iron (preferable 60 Watt)

Tin to solder

Aluminium sheet 50x 20

Casing (I used an old mini computer case)

This could easily be some MDF or own ideas.

Step 1: Setting Up Your RPi

So our heart our brain and our soul is in this product. Threat it well, because it can hurt you in some way or the other. I'm using an RPi 4B 4gb, other models should do just fine. You could expect some more lag on older models.

We got the OS from our school with some preinstalled software such as phpMyAdmin.

First of all make sure you can connect to your Raspberry Pi via ssh we'll need it plenty of time.

So first we should enable the SPI bus, GPIO-pins and disable other busses, we won't be needing them.

You can do this in the raspi-config menu. Navigate to Interfaces and enable GPIO and SPI normally it won't be necessary to While you are here, expand your storage by going to advanced and then hit enter on expand storage.

Now reboot. We will be needing some extensions to use VS Code on our pi, to run our server and database.

Installation process for the VS Code Extension can be found here.

Now we will install the extensions for our server and database. Use the terminal and type 'python install flask, flask-cors, mysql-python-connector, eventlet' wait until finised.

Now we can start with the making of the project.

Step 2: MCP3008 + TMP36

So we have 6 sensors: 4 gas, 1 humidity + temperature and 1 temperature sensor. It is a real task to get them al working. All sensors are analog sensors so we need to convert the analog signal to a digital signal. That's because the RPi (Rasberry Pi) can only "understand" digital signals. For more info click here.

To complete that task you'll need an MCP3008, this will do the job great!

It has 16 ports, counting from the top (the little bubble) left, down, other side and up. Pin1-8 are the inputs for the analog signal from our sensors. Pin 9 on the other side is a GND this needs to be connected to the GND of the complete circuit otherwise this would not work. Pin 10-13 need to be connected more carefully these will transmit data to and from the RPi. Pin 14 is another GND and pins 15 and 16 are the VCC's these must be connected to the positive side of the circuit.

This is the layout for thr wiring:

  • MCP3008 VDD to external 3.3V
    MCP3008 VREF to external 3.3V
  • MCP3008 AGND to external GND
  • MCP3008 DGND to externalGND
  • MCP3008 CLK to Raspberry Pi pin 18
  • MCP3008 DOUT to Raspberry Pi pin 23
  • MCP3008 DIN to Raspberry Pi pin 24
  • MCP3008 CS/SHDN to Raspberry Pi pin 25

This also a good time to connect the GND from the RPI to the external GND. This will make elctricity flow from the RPi.

Here's how to connect it the right way.

Make sure to connect the right way, otherwise you can short-circuit everything!

The first piece of code comes in to place here.

You can copy my code from my project github under models.Analog_Digital.

On the bottom of the page you'll find source code to make it work.

But we need our first sensor, so we can test.

We need to test our sensor if it works. Connect a 3.3V or 5V powersupply to the positive side of the TMP36. Don't forget to connect it to the GND as well, this is maybe something stupid to happen but believe me. That's reality ;). You can test with your multimeter the output of the sensor this is the middle pin. With this simple equation you can check the temperature in °C. ((milivolts*input voltage)-500)/10 and voila donne! Bye! Well no hahah we need the MCP3008. Connect the analog pin for your TMP36 to the first inputpin of the MCP3008. This is pin 0.

You can use the example code at the bottom for this MCP-class. Or something that you'll find online wi'll do the job fine.

Step 3: Database

So now we that we can read in our first sensor we need to log it to a database. This is the memory of our brain. I designed this database to be expandable and easily modifiable for future changes.

So first we need to think what we will get as inputs and if we need to log specific things such as statuses of certain objects.

My answer would be: input from 6 sensors so we need to make a sensor table, with these sensors we will make values. What is linked to a value? For me it is the status of the window, is it open or is it closed while the sensor measured the value. But the location is also a factor for my value so we will add that to. The time and date for a value are also important so I'll add that to.

For future expansion I added a user table.

So what is my idea as for tables: table values, table address (linked to room), table room (linked to value), table window (linked to value), table sensor (linked to value) and a table in the wild for users.

As for linking the tables together. Each value need one sensor, one window, a value for the sensor, an id so we can make the value unique, a timestamp for when the value was made and as last we don't need a room so that is optional but can be added.

So this is how it looks now. This is what I'm using for the rest of my project.

Step 4: HNT11, for the Real Boys

So as we were not allowed to use any kind of libraries. We must program everything ourself.

The HNT11 is a one-wire system so this means that you have a GND and VCC such as any other electronic device but the 3the pin is an in- and output pin. So it is kind of strange but I learned a lot from it.

Connect the VCC to the external 3.3V and the GND to the external GND.

The datasheet of the DHT11 contains everything to use this sensors.

We can determine that a high bit contains of a low and high bit. But the duration of the high part determines the bit for real. If the high part is emitted longer than 100µs (normaly 127µs) the bit is high. Is the bit shorter than 100µs (normaly around 78µs) the bit is low.

When the HNT11 is activated, it will start to emit signals. This is always 41 bits. It starts with a start bit this doesn't mean anything so we can skip this one. The first 16 bits/ 2bytes are the integer and float part for the humidity. It is the same for the last 2 bytes but now it is for the temperature.

So we only need to calculate the duration of each bit and then we are done.

In the source code under DHT11 you'll find my method in solving this problem.

Step 5: Gas Sensors (legends Only)

So I thought at the start of the project that it would be a great idea to use many sensors. Think before you act and buy locally this will save you many sleeping hours! Because you can start earlier and this will you make more willingly to go for it.

So I have 4 gas sensors. MQ135, MQ8, MQ4 and a MQ7 all these sensors have specific gasses that they measure best. But these are all different in their configuration.

So first I used the datasheet, this didn't make me any wisher. Then I searched for code examples. What I found was one library from Adafruit. I tried to replicate it as good as possible. It worked with one of the four sensors.

I let it rest for some while and came back to it.

What I did to make it work for that one sensor is:

- I used the datasheet to mark points of the gas that I wanted to measure. So 1 ro/rs to 400ppm, 1.2 to 600ppm ...

- Then I put all those point in excell and extracted the formula for the curve. I saved this in my database.

- From the datasheet I also read the normal resistance and the clean air resistance. These values where also saved in the database.

I poured this all in to some code, you can find this as the last three functions in the MCP3008 class. But this isn't finished yet, sadly I didn't have enough time.

Step 6: The Shiftregister, 74HC595AG

So this a IC. And it does something special, with this device is it possible to use less GPIO outputs for the same output signal. I used this for the LCD (Liquid Crystal Display) and my leds. I will show the ip address on the LCD so everybody can surf to the site.

The LED's are choses wisely 2 red, 3 yellow and 2 green. This will show at any time the air quality in the room.

The shiftregister is a parrallel output device so it isn't possible to output different signals in a time period. This would be possible if programmed to from the outside but isn't natively backed in.

How to use the IC? Well you have 5 input's and 9 output's. 8 logical outputs for the 8 pins and then 9th pin to send the left over data to another shiftregister.

So we connect pin 16 to the external VCC, the next pin is the first output so for the LCD we will be needing it.. Pin 14 is the data line, here we'll be sending data to. The 13th pin is the on of switch, a low signal is enable the IC a high signal is needed to shut it down. Pin 12 is the pin where we can determine when a bit was send, when you pull this pin down so from high to low it reads the signal status of pin 13 and store it in its 8bit memory. Pin 11 is similar when this pin is set high then low it outputs the 8 bits to its port. And the last pin, pin 10 is the master reset, this pin must remain high or it will not work. The last connection is the GND pin 8 we need to connect this one to the external GND.

So now connect the pins how you'll like to the raspberry pi. The way I did it was connect them as close as possible to each other to make sure I knew where they were.

When you get a right output. You can solder this to a PCB with the LED's. and 220 Ohm resistors. Solder the output of the IC to the corresponding led. You should now have something like this.

You can find my test code here under Shiftregister. When you work with a 74HC595N you'll not be needing the MR so you can leave it not connected .

The LCD is pretty much the same. It is fairly easy to use it with the shiftregister because the input for the LCD is exactly the input for the shiftregister.

For the LCD there is some other code to make it work but it is fairly the same as just the shifregister. You can find the test code here under LCD.

Step 7: Frontend, a Valuable Lesson

So here I will wander off, this a section of how you should do it. This is something very very valuable that learned.

Make the frontend before the backend!!!!

I did it the other way around. I made useless calls for my database, I spend to much time on this.

On the landing page I needed the current temperature and humidity and the values of all the gas sensors in a nice chart. I also need to show the ip address of the RPi.

On the page of the sensors I need the selection of one sensor and the selection time. I chose for the selection of one day and then the period from that day. This made it a lot easier for me because, I could control it more.

On the final page, the settings page is it possible to manage certain values such as healthy dangerous or hazardous gas and temperature levels. You could also make the RPi reboot if you feel the need to do so.

So first I made a design so I could easily start working on the coding part. I gradually made progress one thing at a time. The assignment was mobile first so i'll be focus on that first. Then I'll make my way to the bigger screens.

You can find my pages, css and js in my Github.

Step 8: The Backend

This part is the part that I mixed with the frontend. When I made something for the frontend I made it work on the backend. So it wouldn't need revision later. This was something I didn't do in the first place and because of this I lost certainly 2 weeks of time. Stupid me! But a lesson I happely take to other projects.

So when you make backend make something that you'll use. But make it future proof by making it reusable and not hard coded. So when I need the last 50 values of my DHT11, I'll check are there values being made? Yes, how do I put them in the database. How do I get them out of the database. How do I show it? Chart, graph, or just the plain data? Then I make a new route with the different parameters and properties such as dates, specific sensornames or what I'll be calling. I mean do I call all the values from the MQ sensors or do I call all the sensors with MQ in it's name. Then I put some errorhandling in. When the request from the call is the right method only then it can proceed otherwise it gets a nice error.

Also here are the threads located, these are pieces of software allow you to run parallel code. You could run the websites calls, the value creation function and the led+shiftregister. These functions run completely independent of each other.

So for the led's. I made a bottom/ healthy value for the CO2. This value came came from multiple gouvernemental sources. The healthy value for classrooms is under 600 ppm CO2 per cubic meter. The unhealthy value is everything above 2000 ppm. So the LED's make the bridge. If the value of the MQ4 sensor is 1400 it will automaticaly calculated in which level of danger it is. 2000 - 600 = 1400 so total range is 1400 /7 = 200. So when the value reaches 550 it show a green led. 750 shows 2 green led's, 950 1 yellow 2 green led's. And so on.

When the value goes above the middle the window opens. I used a steppenmotor because of the high torque and precision. And when the value goes over the 2000 a small alarm goes off. This is to alarm the people inside the room.

We can also detect smoke gasses so when there is a fire. It registers this also. When it goes above a certain value the alarm goes on and the LED's flicker.

The LCD is mainly there to show the IP address so you could surf to the site.

You can find everything + code inside my Githubin the

Step 9: Making the Case

I found a small computer case for all my components .

I cut an aluminium sheet to size. And drilled some holes where the sheet would rest. This corresponds with the motherboard holes.

Then I looked on how everything would fit inside the case. I laid out everything and started moving.

When I was content with how it would work I started to mark the holes I needed for the sensors, RPi, PCB's, powermodules and steppenmotor module. The holes are for the PCB standoff, this will make some room so the metal parts don't come in contact with the aluminium sheet. It also gives it a nice look.

I grabbed from each IC or other device the cabels and tied them togheter. THis is because i could see what cables are for what. I placed each everything nicely on some standoffs and used some nuts and screws to hold everything nicely in place.

To power this all I used 2 batteries. These provide a lot of power but these are still batteries so these will deplete in time. I mounted these with some velcro. I used velcro because then I could easily replace or get rid of the batteries.

The steppenmotor, LCD and LED's will come out of the top of the case. So I carefully layed the case cover on top and marked the holes and drilled them with a drill. So we can easily see everything.

As the case is finished we need to wire everything, here you can find the wiring scheme.

Step 10: Evaluation and Conclusion

So this is/was my first project.

It looks okay I guess.

I did learn a lot of new things, learned the great and the bad side of project managment. It really really was a valuable lesson. I leaned that you can't wait you really need to keep on giving. You need to document every move (almost very move), and that you need to do it when you just did it.

Focus on 1 thing at a time. You want the temperature on your screen? Do this, this and that. Don't wait or try letting it pass. It won't help. And it will lose you so so precious time.

Also 4 weeks seem a lot of time. But less is true. This just isn't right. You have only 4 weeks. First 2 weeks not really so much pressure. 3 week finishing and the 4the week sleepless night. This is how you shouldn't do it.

I was maybe a little ambitious: I super small case, no easy to use sensors, batteries ... Make it a lot simpler and then make it gradually harder and harder, only then you'll get a good prototype/ product.

First Time Author Contest

Participated in the
First Time Author Contest