For Carbon Dioxide detection in air

This code communicates with the MQ135 air quality sensor with the help of the MQ135.h library. The sensor is supposed to preheat for 24 hours before taking readings. Once the code runs, it prints out the concentration of detected gases in ppm on a serial monitor, and the results are displayed on an LCD screen. An alarm system (LED light) is also set to glow if the CO2 values cross a threshold value of 1000ppm.

Detailed explanation is given in the video tutorial

CO2Sensor.ino
#include "MQ135.h" //Main library that contains functions to get the ppm values
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>  //Header file for LCD
 
LiquidCrystal_I2C lcd(0x27,16,2);//set the LCD address to x27 for a 16 chars and 2 line display
 
#define led          9                  //led on pin 9
const int gas_pin = A0;                 //analog feed from MQ135
MQ135 gasSensor = MQ135(gas_pin);       //Define the gas pin in the function mq135 gas sensor as per the mq135 library 
 
void setup(){
 
  lcd.init();                      // initialize the lcd 
  lcd.begin(16,2);                 // consider 16 chars + 2 lines lcd
  lcd.backlight();                 // illuminate to produce visible reading
  lcd.clear();                     // clear lcd
  lcd.setCursor(4,0);              //set cursor of lcd to 1st row and 5th column
  lcd.print("Group L");            // print as a sentence on lcd
 
  pinMode(gas_pin,INPUT);     //MQ135 analog feed set for input
  pinMode(led,OUTPUT);        //led set for output
 
  Serial.begin(9600);        //serial comms for debugging
}
 
void loop(){
 float ppm = gasSensor.getPPM(); //function to get ppm value based on the MQ135.h library
 Serial.println(ppm);            // print ppm on serial monitor
 delay(1000);                    
   lcd.clear();                  // clear lcd
   lcd.setCursor(0,0);           // set cursor of lcd to 1st row and 1st column
   lcd.print("Air Quality: ");   // print as a sentence on lcd
   lcd.print(ppm);               // print value of MQ135
 if(ppm>999){                    //if co2 ppm > 1000
    digitalWrite(led,HIGH);      //turn on led
    lcd.setCursor(2,1);          // set cursor of lcd to 2nd row and 3rd column
    lcd.print("AQ Level BAD");   //print as a sentence on lcd
   }
 else{              
    digitalWrite(led,LOW);       //turn off led
    lcd.setCursor(1,1);          // set cursor of lcd to 2nd row and 2nd column
    lcd.print ("AQ Level Good"); // print as a sentence on lcd
  }
 
}

Back to report