CO2 Concentration, CO2 PPM, or CO2 Levels using MQ135 Sensor & Arduino

(Last Updated On: September 9, 2021)

CO2 Concentration using MQ135 Sensor and Arduino:

CO2 Concentration with MQ135 Sensor and Arduino– In this article, we will find the concentration of Carbon Dioxide using the MQ135 sensor and display it on the OLED I2C display module. So basically we will be making the Carbon Dioxide Parts per Million Meter.

* Atmospheric CO2 Level…………..400ppm

* Average indoor co2………….350-450ppm

* Maximum acceptable co2………..1000ppm

* Dangerous co2 levels………….>2000ppm

Before, we find the concentration of the Carbon Dioxide or the CO2 level first; let’s learn how to interface the OLED display module with the Arduino. If you already know how to interface the SDD1306 I2C Oled display module with the Arduino then you can jump to the end. Without any further delay let’s get started!!!



Amazon Links:

12v Adaptor:

Arduino Uno

Arduino Nano

SSD1306 128×64 Oled i2c display Module

MQ135 Gas Sensor:

Other Tools and Components:

Top Arduino Sensors:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!




Interfacing I2C OLED Display with Arduino:

Now we will learn how to connect and code an I2C OLED display with an Arduino microcontroller board and we will also learn how to write a simple code to display a message on your OLED screen. Let’s begin with wiring the display since the OLED display works on I2C communication we have to connect only four wires to the Arduino. The OLED consists of four pins which are:

  • SCK
  • SDA
  • VCC
  • GND

The SCK is the clock pin, SDA is the data pin, VCC is the power pin and GND is the ground pin.  The Arduino board has SDA at pin number A4 and SCK at pin number A5.

  • Connect the SDA pin of the OLED with the pin number A4
  • Connect the SCK pin of the OLED with the pin number A5
  • Connect the VCC pin of the OLED with the 5V of the Arduino
  • Connect the ground pin of the OLED with the ground of the Arduino

co2 concentration

Libraries needed for the Oled Display Module:

Before you start the programming, first of all, make sure you download the necessary libraries; otherwise, you won’t be able to display anything on your Oled display module. The Oled display module I am using is SSD1306 which i have been using for quite a long time and seriously this is my favorite one. For this Oled display module you will need to install two libraries which are:

Now to install these libraries go to the sketch click on the include library and then click on the manage library

co2 concentration

Now just write the library name and install it

co2 concentration

We can also install the library manually from zip file. Go to sketch click on the include library and click on the zip file and insert the zip file.

co2 concentration



Now click on files and in files click on examples and in examples select Adafruit SDD1306 in which click on ssd1306_128X32_i2c

co2 concentration

I2C Oled Arduino Code:

Upload the code to Arduino and the following text will be displayed on the OLED display module. If your display behaves in an abnormal way and if you can’t see anything then you should read my article on how to fix the Oled display Module issues.

co2 concentration

If your display is working then congrats you are done with the hard part. Before you jump into the project first play with the code a little, to understand how you can display different texts at different positions.



Interfacing MQ135 sensor with Arduino:

The connection of the I2C Oled display module with the Arduino remains exactly the same and we don’t need to change any wires, we can now add our MQ135 Sensor with the Arduino Uno or Arduino Nano to find the CO2 Concentration. But, before I am going to explain the wiring first let’s take a look at the MQ135 Sensor.

MQ135 Specifications:

  • Working voltage: DC 5V
  • Working Current: 150mA
  • DOUT: TTL output
  • AOUT: Analog output
  • Preheat time: Over 20s
  • Dimension: 32mm x 22m x 27mm(HIGH 27mm)

co2 concentration

Now to connect the MQ135 with Arduino:

  • Connect the VCC of the MQ135 Sensor with the 5V of the Arduino
  • Connect the gnd of the MQ135 with the ground of the Arduino
  • Connect the analog pin of the MQ135 with the A0 of the Arduino
  • Connect the anode of the led with the 220-ohm resistor at digital pin number 9
  • Connect the cathode of the Arduino with the ground of the Arduino

co2 concentration


CO2 Concentration Arduino Code:



Code Explanation of carbon dioxide CO2 concentration:

* Carbon Dioxide Parts Per Million Meter

* CO2PPM

The threshold values of the C02 are as follow:

* Atmospheric CO2 Level…………..400ppm

* Average indoor co2………….350-450ppm

* Maximum acceptable co2………..1000ppm

* Dangerous co2 levels………….>2000ppm

*/

We will require three libraries for interfacing OLED with Arduino which we have explained above in detail

//—————————————————————————————————————

//                                                  LIBRARIES

//—————————————————————————————————————

#include <Wire.h>                             //I2C for OLED

#include <Adafruit_GFX.h>                     //grafix library for OLED

#include <Adafruit_SSD1306.h>                 //OLED Driver

The data from the MQ135 Sensor is taken from the analog pin of the MQ135 which is connected with A0 of the Arduino and is stored in variable anInput

#define anInput     A0                        //analog feed from MQ135

#define digTrigger   2                        //digital feed from MQ135

Now this is the atmospheric value of carbon dioxide

#define co2Zero     55                        //calibrated CO2 0 level

#define led          9                        //led on pin 9

#define OLED_RESET   4                        //OLED reset on lin 4

//—————————————————————————————————————

//                                                LIBRARY CALL

//—————————————————————————————————————

Adafruit_SSD1306 display(OLED_RESET);         //create instance of OLED called display

//—————————————————————————————————————

//                                                  SETUP

//—————————————————————————————————————

void setup() {

pinMode(anInput,INPUT);                     //MQ135 analog feed set for input

pinMode(digTrigger,INPUT);                  //MQ135 digital feed set for input

pinMode(led,OUTPUT);                        //led set for output

Serial.begin(9600);                         //serial comms for debuging

display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  //begin display @ hex addy 0x3C

display.display();                          //show buffer

display.clearDisplay();                     //clear buffer

}

//—————————————————————————————————————

//                                               MAIN LOOP

//—————————————————————————————————————

void loop() {

This integer will save the concentration of the of the carbon dioxide

int co2now[10];                               //int array for co2 readings

int co2raw = 0;                               //int for raw value of co2

The compensated value of the carbon dioxide will be saved in co2comp

int co2comp = 0;                              //int for compensated co2

int co2ppm = 0;                               //int for calculated ppm

intzzz = 0;                                  //int for averaging

intgrafX = 0;                                //int for x value of graph

This command will clear the display

display.clearDisplay();                     //clear display @ beginning of each loop

Now through this loop we will collect the samples of the carbon dioxide in 2 seconds

for (int x = 0;x<10;x++){                   //sample co2 10x over 2 seconds

co2now[x]=analogRead(A0);

delay(200);

}

After filling up of the array then we are going to add the sample together we will create another for loop

for (int x = 0;x<10;x++){                     //add samples together

zzz=zzz + co2now[x];

}

Now in the zzz variable, we have added all the samples of the carbon dioxide we will divide it by 10 to obtain the average value

co2raw = zzz/10;                            //divide samples by 10



Then we calculate the compensated value

co2comp = co2raw – co2Zero;                 //get compensated value

As we know that analog input create value are from 0 to 1023 we will used the map function the minimum value will be 400 and the maximum value will be 5000 for the ppm value

co2ppm = map(co2comp,0,1023,400,5000);      //map value for atmospheric levels

display.setTextSize(2);                     //set text size

display.setTextColor(WHITE);                //set text colo

display.setCursor(0,0);                     //set cursor

display.println(“CO2 Level”);               //print title

display.println(” “);                       //skip a line

display.print(co2ppm);                      //print co2 ppm

display.print(” PPM”);                      //print units

grafX = map(co2ppm,0,1000,0,127);           //map value to screen width

display.fillRect(0, 52, grafX, 10, WHITE);  //print graph 400min 1000max

display.display();                          //show the buffer

if(co2ppm>999){                             //if co2 ppm > 1000

digitalWrite(led,HIGH);                   //turn on led

}

else{                                       //if not

digitalWrite(led,LOW);                    //turn off led

}

}

After uploading the code to the Arduino we will get the concentration of the carbon dioxide. You will see the CO2 Level on the OLED display module in the PPM value.

co2 concentration


Related article:

IoT Smoke Detector using Nodemcu ESP8266

Arduino Gas leakage Detection with SMS Alert

Recommended For You

About the Author: Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

%d bloggers like this: