TheAir - Gas Sensor Project

Introduction: TheAir - Gas Sensor Project

Carbon monoxide and carbon dioxide, also known as CO and CO2. Gasses that are colorless, odorless, tasteless and frankly dangerous when in high concentrations in a closed room. If you're living say for example in a student room thats badly isolated, no good airflow and for some reason, the toaster makes a weird noise while making toast. Then you might come in contact with these gasses and when that happens, lets hope it only ends with a small headache, cause in high concentration it might incapacitate or even kill you (although very rarely).

So I decided to come up with this project. My idea is simple, use fans to make an airflow. Good air in and bad air out so to speak. For extra utility, I added an extra temperature sensor, button for manual activation fans and also a website for those that like to see statistics and/or activate fans from their computer.

As a student, parent, single person or living being. This is something you'd normally like to avoid when living in the comfort of your own house. This helps those that like to make their life a bit easier.


  • Raspberry Pi 3+
  • Mini-usb charger 5V/2.5A
  • Micro-sd card
  • Sensors
    • MQ-7 ( CO )
    • MQ-135 (CO2)
    • DS18B20 (temperature)
  • 2 x 12V DC fan
  • 2 x 2n2222 transistors
  • LCD 16*2 display
  • Pushbutton
  • MCP3008
  • Logi level converter
  • Ethernet cable (for setup reasons)

Step 1: Raspberry Pi Setup

Before working with the Rpi, we will need some software.

Before I begin I would like to mention that when making this tutorial, when I pick a program over the other, does NOT mean that I recommend it. For example I like to use etcher because more user friendly but Win32 has the option to make backups. Now that's out of my system, lets begin.

If you already have a Rpi that is connected to your wifi network, skip to step 3.

First we're going to use Etcher to put the Raspbian image on your sd card. Now before we pull out the sd card, we're going to change some "stuff" in the cmdline.txt file, which can be found in the image. Open the .txt file -> Add this line "ip=" (no quotation marks) at the end of the line (all on 1 line) -> Save file

Secondly make an empty folder called "ssh" in the boot-partition (without quotation marks).

After that you may safely eject the Microsd and put it in the Rpi.

The reason for the hard coded static IP is to make it easier for connecting to the Rpi. If for some reason the Rpi doesn't have an ip with DHCP then you easily use the static ip.

Step 2: Making a Connection and Connecting Rpi Wireless to Your Local Network

We're going to boot up the Rpi -> connect the ethernet cable between computer and Rpi.

  1. Start Putty and fill in this:
    • Host Name (or IP address) :
    • Port : 22
  2. A terminal pops up and you type in the default username & password:
    • Username: pi
    • Password: raspberry

Now that we are connected locally to the rpi, we want the Rpi to have a connection to your wifi.

  1. Extra: type in "sudo raspi-config"
  2. Here you will need to change password for pi user (safety reasons)
  3. Afterwards go to Localisation Options -> Change Time (pick the correct one) -> Then go to Wifi Country -> choose country.
  4. Close raspi-config and reboot.
  5. When logged in, temporary make yourself root user -> sudo -i
  6. Write this command to add your network to the Rpi (code below list)
    • password = "password" (with quotation marks)
    • Name network = "SSID"
    • Remember to use double >> !Important!
echo "password" | wpa_passphrase "SSID" >> /etc/wpa_supplicant/wpa_supplicant.conf
  • Now reboot again.

When reconnecting, check your ip by typing:


and check wlan0, next to inet.

Now that we have internet connection, lets do a "quick" update.

sudo apt update
sudo apt dist-upgrade -y

This might take some time.

Step 3: Sensor DS18B20 (temperature) -- 1-wire

With every project, there is always going to be that something special that has to be done or else it won't work moment.

This time we have it with the DS18B20 temperature sensor which requires 1-wire which I won't explain why but I'll explain how to make it work at least.

For this we have to go back to the raspi-config on the Rpi, the nice blue screen.

  1. Go to Interfacing options
  2. Choose 1-Wire and choose enable.


Just kidding.

Now we'll need to adjust /boot/config.txt

sudo nano /boot/config.txt

Add this line at the bottom.

# Enable onewire

Now sudo reboot that thing and now we're done.

To check whether it works, connect the sensor to the Rpi then go back to the terminal and type this code (See next step Hardware on how to connect temperature sensor).

cd /sys/bus/w1/devices/w1_bus_master1 

You should see something with numbers and letters in dark blue at top left, make sure to write this piece of information for later when we'll be working with the code from github.

If for some reason it does not work, check this link that goes deeper into it.

Step 4: MCP3008 - Analog Sensing

As we did a change for the temperature sensor, we also need to do some changes for the other sensors since they we need to read in analog data. This where the MCP3008 comes in handy, we also need to change SPI interface.

sudo raspi-config

Go to Interfacing Options -> Select SPI -> enable.

Then Finish.

Step 5: Hardware

We"re not entirely done with the Rpi but enough so that we can start building and putting the hardware together.

Some advice is to thoroughly check your connections when building to make sure you don't ... blow up the Rpi.

Also, in the Schematic you will notice some components are on it only once even though we'll be working with more than 1 of the same component. It just means that you have to repeat the same process of building that 1 component. There is 1 small exception, the mq-x sensors don't need an extra level convertor or MCP3008. Just add an extra green cable (in pdf) to the level convertor and MCP3008.

Extra edit: The fans need to use a transistor as a switch. I use a 2n2222A transistor for 1 fan, because 2 fans might be a to heavy load.

If you have a transistor that can handle a bigger current then good, skip the last part of this step.

If you don't have one like me, then you'll need to do it like this, 1 fan = 1 transistor, 2 fans = 2 transistors, and so on (every fan it's own transistor + diode as in the pdf).

You will also need to add some code to in backend_project later on in Step 7: Git code....

Step 6: Creating a Mariadb Datbase

As the title implies, we're going to create a database so that we have place to store our sensor data.

First things first, download Mariadb on the Rpi.

sudo apt-get install mariadb-server

After installation, let's use it.

mysql -u root

Password is blank, so nothing to type. Press enter.

Lets create a user now.

CREATE USER 'user'@'%' IDENTIFIED BY 'userdb';

Press Ctrl + C to exit and do a quick restart of the service:

sudo service mysql restart

Login with username: user & password: userdb:

mysql -u user -p

Time to create the database now.

USE project_db

Create a table "historiek" (means history).

  `sensorID` VARCHAR(5) NOT NULL,
  `waarde` FLOAT(4) NULL DEFAULT 0,
  PRIMARY KEY (`id`))

And voila, the database is made.

Step 7: Github Code and Testing

We're getting close to the end of our project.

Before we get the code, we will need to import some modules into the Rpi:

pip3 install Flask_MySQL
pip3 install flask-socketio
pip3 install -U flask-cors
pip3 install spidev

Now we need the code to make it work, type in the terminal:

git clone

Check if the folder is there with:


Now you will need 2 terminals so it is handy to right-click on the terminal and click Duplicate sessions:

Go to backend_project and temperature using the cd command.

Now before we start up the programs for testing purposes. Do you still remember Step 3 with the 1-wire sensor where you need to write down some numbers? No worries if you have, just take a quick peek at step 3 again.

We're going to add these numbers to the code cause it will need to know the correct sensor when using it.

The terminal with the temperature folder, you will find We're going to open it.

sudo nano

Look for the function that is called "def temperatuur():", there you will have to replace the "**" with the numbers you wrote up. In my case I would get it this line of code (each number is unique).

sensor_file_name = '/sys/devices/w1_bus_master1/<strong>28-0316a4be59ff</strong>/w1_slave

Testing time. Both terminals in both backend_project and temperature folder, type:


Now remember step 5: hardware where you need to add code if you're using multiple fans and transistors?

Good, if not go back to step 5.

Now we need to add code as I mentioned to in backend_project. To make it easier, I made an example of this in the code. Every line of comment code that has "fan1" in it, uncomment those lines and voila, now you can use 2 fans.

If you want to use more then just 2 fans, copy & paste the same code under it but with a different number. Disadvantage of this is more personal work for you and less gpio.pins available. There are no advantages to this that I know of.

Step 8: Run Code on Boot Up

We want these 2 python scripts to run the moment our Rpi boots up and in the case a script would crash, it should restart on its own. To do this we are going to make 2 services.

To do this, type:

sudo nano /etc/systemd/system/temperature.service

Copy and paste this for a temperature.service:

Description=Temperature Service
[Service] Type=simple
ExecStart=/usr/bin/python3 /home/pi/Documents/nmct-s2-project-1-TheryBrian/temperature/

Close and do again but then for a backend_project.service:

First open text:

sudo nano /etc/systemd/system/backend_project.service

Then again copy and paste:

Description=backend_project Service
ExecStart=/usr/bin/python3 /home/pi/Documents/nmct-s2-project-1-TheryBrian/backend_project/

Save and close.

Last part is typing this:

sudo systemctl daemon-reload
sudo systemctl enable temperature.service
sudo reboot

Now our 2 python scripts should run automatically on boot.

Step 9: Setup Website

When you downloaded the repository, you should've also gotten a folder called front. This is where the contents are for the website.

First we need apache before we can use the folder. Follow the guide on this link for apache.

When you're ready. Go to where the front folder is located:

cd /Documents/nmct-s2-project-1-TheryBrian

Then type:

sudo mv front /var/www/html

When that is done, go to the html folder, prepare for some tedious work (my fault).

cd /var/www/html/

then go into the front folder and start moving everything to the html folder.


sudo mv css /var/www/html

Then delete the front folder.

And we are done with everything.

Good luck :) .

Step 10: Optional - Miniature Prototype

For testing reasons I made a prototype of just a box with the all the hardware inside so I can see if everythings works to order.

Normally this project would be done on a bigger scale. For example: a room, a house, a factory, a store and so on...

But obviously before we start making holes in the walls (nice rhyme). We first want to see if it just works. You don't actually need to make a box for testing, but it's always fun to do some crafting.

Here is my example.

Be the First to Share


    • Make It Bridge

      Make It Bridge
    • Big and Small Contest

      Big and Small Contest
    • For the Home Contest

      For the Home Contest



    3 years ago

    First comment...
    I've noticed there is a small problem with CORS policy. This will updated later on.
    Pardon my failure on this.