Hackster is hosting Hackster Holidays, Finale: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Tuesday!Stream Hackster Holidays, Finale on Tuesday!
AZ-Delivery
Published

Room-Climate measuring with MQ-135 and DHT11

Monitoring of CO2, temperature and humidity with MQ-135, DHT11 and Nano V3.0 CH340 with ATmega328p

IntermediateFull instructions provided2 hours2,539
Room-Climate measuring with MQ-135 and DHT11

Things used in this project

Hardware components

Nano V3 ATmega328p Microcontroler
×1
MQ-135 Air Quality Sensor
×1
DHT-11 temperature and humidity sensor
×1
MT3608 DC adapter
×1
RED LED
×1
YELLOW LED
×1
GREEN LED
×1
OLED Display
×1
1K Ohm Resistor
×2
220 Ohm Resistor
×1
Buzzer
×1

Software apps and online services

Arduino IDE
Arduino IDE
MQ135-Library
DHT-11-Library
Adafruit GFX and Adafruit SSD1306 Library
Wire Library
CO2 monitoring sketch
MQ135 Calibration Sketch

Story

Read more

Schematics

Fritzing schematic

Diagramm for the connection on a breadboard

Code

mq135_calibration.ino

Arduino
Calibration of the MQ135
#include "MQ135.h"
#define RZERO 1

MQ135 gasSensor = MQ135(A0);
int val; 
int sensorPin = A0; 
int sensorValue = 0;

void setup() { 
  Serial.begin(115200);
  pinMode(sensorPin, INPUT); 
} 
 
void loop() { 
  val = analogRead(A0); 
  Serial.print ("raw = "); 
  Serial.println (val); 
  float zero = gasSensor.getRZero(); 
  Serial.print ("rzero: "); 
  Serial.println (zero); 
  float ppm = gasSensor.getPPM(); 
  Serial.print ("ppm: "); 
  Serial.println (ppm); 
  delay(2000); 
}

CO2_monitor.ino

Arduino
This is the final sketch
/*************   Necessary libraries for the project    **************************/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
#include "pitches.h"

/*************   Screen parameters    ********************************************/
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

/******    Humidity and temperature sensor DHT-11 paramters and variables    *****/
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
float t, h;

/******************    MQ-135 sensor parameteres and variables    ****************/
#define RZERO 1 
#include "MQ135.h" 
MQ135 gasSensor = MQ135(A0);
int val; 
int sensorPin = A0; 
int sensorValue = 0;
float ppm, zero;

/*****************    LEDs    ****************************************************/
#define led_verde 3
#define led_amarillo 4
#define led_rojo 5

unsigned long tiempo_anterior;					  // Time counter 

/***********************   SETUP BLOCK    ***************************************/
void setup( )
{
  Serial.begin(115200);
  dht.begin();							              // DHT-11 initialization
  
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {		// Checking display present
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  delay(2000);
  display.clearDisplay();					        // Initialization screen and
  display.setTextColor(WHITE);					  // config text color
  
  pinMode(sensorPin, INPUT);					    // MQ-135 pin data

  pinMode(led_verde, OUTPUT);
  pinMode(led_amarillo, OUTPUT);
  pinMode(led_rojo, OUTPUT);
  digitalWrite(led_verde, LOW);
  digitalWrite(led_amarillo, LOW);
  digitalWrite(led_rojo, LOW);

}

/***********************    LOOP BLOCK    *************************************/
void loop( ) {

  t = dht.readTemperature();					    // Read temperature
  h = dht.readHumidity();					        // Read Humidity

  if (isnan(h) || isnan(t)) {					    // If there aren't temperature and
    Serial.println("Fallo de lectura del sensor DHT !!!");	// humidity read, show message to inform
  }

  val = analogRead(A0);						        // CO2 read
  zero = gasSensor.getRZero();					  // Calibration value for the MQ-135 sensor
  ppm = gasSensor.getPPM();					      // Formula in the library for obtan the ppm of CO2
 
  chequeo_led ();						              // Go and run to LEDs check method
  
  Serial.print( "T = " );					        // Show values by the Serial Port
  Serial.print(t);
  Serial.print(" C, H = ");
  Serial.print(h);
  Serial.print( "%, " );
  Serial.print ("raw = "); 
  Serial.print (val);
  Serial.print (", rzero: "); 
  Serial.print (zero);
  Serial.print (", ppm: "); 
  Serial.println (ppm);
  delay (1000);

  if ( millis()-tiempo_anterior >= 60000) {			// If the time past is most or equal than 60 sg
        encendido_pantalla();					    // Execute the turn on screen method.
        tiempo_anterior = millis();				// Save the actual time.
  }
}

void chequeo_led() {
  if (ppm >= 800) { 				              // If the CO2 concentration is equal o most than 800
    digitalWrite(led_rojo, HIGH);	        // Turn on de red LED
    tone(8, NOTE_C5, 5000);		            // Buzzer sound
    display.clearDisplay();		            // Set the screen parameters to show the PPM value
    display.setTextSize(2);
    display.setCursor(7,0);
    display.print("CO2 (PPM):");
    display.setTextSize(3);
    display.setCursor(40,35);
    display.print(ppm,0);
    display.display();
    delay(10000);
  } else {
    digitalWrite(led_rojo, LOW);
    display.clearDisplay();
    display.display(); }
  
  if (ppm >= 551 && ppm <= 799) {
    digitalWrite(led_amarillo, HIGH); 		// The yellow LED turno on if the CO2 concentration
  } else {
    digitalWrite(led_amarillo, LOW); }    // is between 551 and 799
  
  if (ppm <= 550)  {
    digitalWrite(led_verde, HIGH); 				// The green LED turn on only when the CO2
  } else { 
    digitalWrite(led_verde, LOW); }	      // concentration is most low than 551
}

void encendido_pantalla() {   					  // Turn on screen method
  display.clearDisplay();		              // Set the screen parameters and show the PPM value
  display.setTextSize(2);
  display.setCursor(7,0);
  display.print("CO2 (PPM):");
  display.setTextSize(3);
  display.setCursor(40,35);
  display.print(ppm,0);
  display.display();
  delay(3000);
   
  display.clearDisplay();		              // Set the display parameters and show the Humidity value
  display.setTextSize(2);
  display.setCursor(20,0);
  display.print("HUMEDAD:");
  display.setTextSize(3);
  display.setCursor(20,35);
  display.print(h,1);
  display.setCursor(90,35);
  display.print("%");
  display.display();
  delay(3000);
  
  display.clearDisplay();		              // Set the display parameters and show the Temperature value
  display.setTextSize(2);
  display.setCursor(40,0);
  display.print("TEMP:");
  display.setTextSize(3);
  display.setCursor(5,35);
  display.print(t,1);
  display.setCursor(85,35);
  display.cp437(true);
  display.write(167);
  display.setCursor(107,35);
  display.print("C");
  display.display();
  delay(3000);
  
  display.clearDisplay();
  display.display();
}             
  

Credits

AZ-Delivery
4 projects • 1 follower
We are "AZ-Delivery - Your Expert for Microelectronics". Visit our shop with regularly new Blogs and free E-Books.

Comments