/*
Voltaat learn (http://learn.voltaat.com)
Link for full tutorial:
Tutorial: Measure the CO2 concentration using the Arduino!
Adafruit_ILI9341 Library:
Adafruit_GFX Library:
Wire Library:
The purpose of this sketch is to display the concentration of CO2 gas in ppm obtained from the MQ-135 gas sensor on the TFT Display.
Connections from the Arduino to the breadboard:
•Arduino GND pin → Breadboard ground line
•Arduino 5V pin → Breadboard 5V line
Connections from the MQ-135 gas sensor:
•MQ-135 gas sensor VCC pin → Breadboard 5V line
•MQ-135 gas sensor GND pin → Breadboard ground line
•MQ-135 gas sensor A0 pin → Arduino pin A0
Connections from the passive buzzer :
•passive buzzer GND pin → Breadboard ground line
•passive buzzer VCC pin (+ pin) → Arduino pin 3
Connections from the TFT Display to the breadboard:
•TFT Display VCC pin → Breadboard 5V line
•TFT Display GND pin → Breadboard ground line
•TFT Display LED pin → Breadboard 5V line
Connections from the TFT Display to arduino :
•TFT Display CS pin → Arduino pin 10
•TFT Display RST pin → Arduino pin 8
•TFT Display DC pin → Arduino pin 9
•TFT Display MOSI pin → Arduino pin 11
•TFT Display SCK pin → Arduino pin 13
•TFT Display MISO pin → Arduino pin 12
*/
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "Wire.h"
#include "SPI.h"
//Define TFT Display pin numbers
#define TFT_DC 9
#define TFT_CS 10
#define TFT_RST 8
#define TFT_MISO 12
#define TFT_MOSI 11
#define TFT_CLK 13
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
#define anInput A0 //analog feed from MQ135
#define co2Zero 265 //Adjust this value in the calibration process
#define passive_buzzer 3 //define passive buzzer pin
char co2ppmChar[10];
void setup(){
Serial.begin(9600);
tft.begin();
tft.setRotation(0);
tft.fillScreen(ILI9341_BLACK);
pinMode(anInput,INPUT); //MQ135 analog feed set for input
pinMode(passive_buzzer,OUTPUT); //define passive buzzer pin as an output pin
Wire.begin();
printText("CO2 Level", ILI9341_GREEN,20,130,3);
}
void loop()
{
int co2now[10]; //int array for co2 readings
int co2raw = 0; //int for raw value of co2
int co2comp = 0; //int for compensated co2
float co2ppm = 0; //float for calculated ppm
int zzz = 0; //int for averaging
int grafX = 0; //int for x value of graph
for (int x = 0;x<10;x++){ //samplpe co2 10x over 2 seconds
co2now[x]=analogRead(A0);
delay(200);
}
for (int x = 0;x<10;x++){ //add samples together
zzz=zzz + co2now[x];
}
co2raw = zzz/10; //divide samples by 10
co2comp = co2raw - co2Zero; //get compensated value
co2ppm = map(co2comp,0,1023,400,5000); //map value for atmospheric levels
String co2ppmString = String(co2ppm,1); //Convert float to string
co2ppmString.toCharArray(co2ppmChar,10);
tft.fillRect(50,175,150,40,ILI9341_BLACK); //Draw a Rectangle
printText(co2ppmChar, ILI9341_WHITE,30,180,4); //printing the co2 gas concentration on the TFT Display
printText("ppm", ILI9341_WHITE,160,180,4);
grafX = map(co2ppm,0,1000,0,127); //map value to screen width
tft.fillRect(0, 235, grafX, 30, ILI9341_WHITE); //print graph 400min 1000max
if(co2ppm>999){ //if co2 ppm > 1000
digitalWrite(passive_buzzer,HIGH); //turn on the buzzer
}
else{ //if not
digitalWrite(passive_buzzer,LOW); //turn off the buzzer
}
delay(500);
}
//The function of writing words on the TFT Display
void printText(char *text, uint16_t color, int x, int y,int textSize)
{
tft.setCursor(x, y);
tft.setTextColor(color);
tft.setTextSize(textSize);
tft.setTextWrap(true);
tft.print(text);
}