Project tutorial
Air Meter Making #4: Complete

Air Meter Making #4: Complete © GPL3+

It can measure fine dust, humidity, temperature and CO2 concentration in the air and check it on the internet in real time.

  • 3,209 views
  • 2 comments
  • 9 respects

Components and supplies

About this project

Background

I have rhinitis so I wanted to know the air quality level of our school. So I decided to make a device that can measure air quality and I wanted to make this information available on the Internet in real time. So I tried to google how to do this and in the end I concluded that it would be best to use the sites called "Arduino" and "thingspeak".

After making these conclusions, I thought about what functions to include in the Air Meter I produce, and concluded that temperature, humidity, fine dust, and CO2 concentration, which have a large impact on air quality, must be made measurable. And I started by looking for information on the Internet. We could combine the examples collected from the Internet with each other, and eventually create the code that performs the desired action. Of course, it was not easy.

Issues

I spent a lot of time and effort trying to figure out how the middle code example works. Also the ESP8266 WiFi module was unstable and this bothered me. The accuracy of the fine dust sensor I bought at first (Air Meter v1.5) was very low, the fine dust sensor was also scratched and the CO2 sensor used too much electric power to supply additional external power. However, we were able to solve all of these problems and eventually create a successful Air Meter v2.0.

Used for this project

So I would like to help you with the schematic, code, and how to make Air Meter v2.0 that I made here. Now we will make an air meter in earnest. Please note that this post also uses modules that I did not describe.

Air Meter prototype ↓ (Function: Temperature and humidity measurement)

Air Meter v1.0 ↓ (Function: Temperature, humidity, fine dust measurement)

Air Meter v1.5 ↓ (Function: temperature, humidity, fine dust, co2 concentration measurement + fine dust sensor change)

Air Meter v2.0 ↓ (Same as Air Meter v1.5 with some minor fixes)

Code

Arduino Air Meter (iot) codeArduino
Arduino Air Meter (iot) code
(2017.10.29 Minor error correction)
(2017.10.30 Fixed fine dust code error)
(2017.10.31 Modify fine dust renewal interval)
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
#include <stdlib.h>
#include <DHT.h>
 
boolean state_led1 = 0;
const int duration2 = 16000;
 
#define         MG_PIN                       (0)     //define which analog input channel you are going to use
#define         BOOL_PIN                     (2)
#define         DC_GAIN                      (8.5)   //define the DC gain of amplifier
#define         READ_SAMPLE_INTERVAL         (50)    //define how many samples you are going to take in normal operation
#define         READ_SAMPLE_TIMES            (5)     //define the time interval(in milisecond) between each samples in 
                                                     //normal operation
#define         ZERO_POINT_VOLTAGE           (0.37) //define the output of the sensor in volts when the concentration of CO2 is 400PPM
#define         REACTION_VOLTGAE             (0.020) //define the voltage drop of the sensor when move the sensor from air into 1000ppm CO2
float           CO2Curve[3]  =  {2.602,ZERO_POINT_VOLTAGE,(REACTION_VOLTGAE/(2.602-3))};   
 
                                                     
#define DHTPIN 4        // Setting the SDA pin
#define DHTTYPE DHT11   // DHT22 (AM2302) Sensor type setting
int pin = 3; //Dust sensor
unsigned long duration;   //duration
unsigned long starttime;  //Start time
unsigned long sampletime_ms = 30000;   //Updated every 30 seconds of sample time
unsigned long lowpulseoccupancy = 0;   //Low Initializes the time the signal lasted
float ratio = 0;  //ratio
float concentration = 0;  //Initialized to zero particle concentration
float pcsPerCF = 0;  //Initialize CF to 0 per particle
float ugm3 = 0;  //The final value, in micrograms per cubic meter(㎍/㎥)
 
unsigned long pre_time = 0;
unsigned long cur_time = 0;
 
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);  
// Upload Notification LED Setting (Adunono On Board LED)
int ledPin = 13;
 
// Enter Write API key for your own thingspeak channel
String apiKey = "Your API Key";
 
SoftwareSerial ser(6,5 ); //RX / TX configuration, serial object creation
DHT dht(DHTPIN, DHTTYPE);
 
void setup() {
    pre_time = millis();
    pinMode(3,INPUT);
   pinMode(4,INPUT);
   pinMode(BOOL_PIN, INPUT);                        //set pin to input
    digitalWrite(BOOL_PIN, HIGH);                    //turn on pullup resistors
    lcd.begin(20, 4); 
  dht.begin();
  //Notification LED Output Settings
  pinMode(ledPin, OUTPUT);
  //Serial communication speed 9600 baud rate setting  
  Serial.begin(9600);
  //Start the software serial
  ser.begin(9600);
  //ESP8266 Reset
  ser.println("AT+RST");
}
 
void loop() {
 delay(1800);
    cur_time = millis();//Assign millis to cur_time (for parallel operations)
    int percentage;
    float volts;
  Serial.println( pre_time );
Serial.println( cur_time);
 
 //co2 Measurement Codes--------------------------------------------------------
    volts = MGRead(MG_PIN);
    Serial.print( "SEN-00007:" );
    Serial.print(volts); 
    Serial.print( "V           " );
    Serial.println(volts/DC_GAIN);
 
 
    percentage = MGGetPercentage(volts,CO2Curve);
 
    Serial.print("CO2:");
    if (percentage == -1) {
        Serial.print( "<400" );
    } else {
        Serial.print(percentage);
    }
 
    Serial.print( "ppm" );  
    Serial.print("\n");
 
    if (digitalRead(BOOL_PIN) ){
        Serial.print( "=====BOOL is HIGH======" );
    } else {
        Serial.print( "=====BOOL is LOW======" );
    }
 
    Serial.print("\n");
   //---------------------------------------------------------------------------
   
   //Fine dust measuring section code-------------------------------------------
  
  duration = pulseIn(pin, LOW); 
  lowpulseoccupancy = lowpulseoccupancy+duration;
  
  if ((millis()-starttime) >= sampletime_ms)  {   //If the sample time is 5 seconds (the sample time specified above)
    ratio = lowpulseoccupancy/(sampletime_ms*10.0);  // Integer percent
    concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // Fine dust sensor specification Sheet curve use
    pcsPerCF = concentration * 100;  // When the particle concentration is multiplied by 100, the CF value per particle
    ugm3 = pcsPerCF / 13000;  //When CF per particle is divided by 13000, the fine dust measurement of micrograms per cubic meter
    Serial.print(ugm3);
    lcd.setCursor(0, 2);
    lcd.print("                    ");
    lcd.setCursor(0, 2);
    lcd.print("Dust:");
    lcd.setCursor(6, 2);
    lcd.print(ugm3);
    lcd.print("ug/m3");
    lowpulseoccupancy = 0;
    starttime = millis();
    }
 
  // blink LED on board
  digitalWrite(ledPin, HIGH);   
  delay(200);               
  digitalWrite(ledPin, LOW);
 
  //Read DHT11 value
  float temp = dht.readTemperature();
  float humi = dht.readHumidity();
 
  // String Conversion
  char buf[16];
  String strTemp = dtostrf(temp, 4, 1, buf);
  String strHumi = dtostrf(humi, 4, 1, buf);
  
  Serial.println(strTemp);
  Serial.println(strHumi);
  Serial.print("Dust density(ug/m3) = ");
  Serial.println(ugm3);
  //Measured value lcd display section------------------------------------------
 
 
 lcd.setCursor(0, 3);
 lcd.print("             "); 
       lcd.setCursor(0, 3);
    lcd.print("Co2:");
    if (percentage == -1) {
         lcd.print( "<400" );
    } else {
        lcd.print(percentage);
         lcd.print("ppm");
    }
 
 
    
 lcd.setCursor(0, 0);
 lcd.print("                    ");
 lcd.setCursor(0, 1);
 lcd.print("                    ");
 lcd.setCursor(0, 0);
 lcd.print("Temperature:");
 lcd.setCursor(13, 0);
 lcd.print(strTemp);
 lcd.setCursor(0, 1);
 lcd.print("Humidity:");
 lcd.setCursor(10, 1);
 lcd.print(strHumi);
 
 //-----------------------------------------------------------------------------
 
 
   if(cur_time - pre_time >= duration2){//Sensor measurement and measurement lcd Independent from the display section Transfer measurements to the thingspeak server once every 16 seconds
 
 pre_time = cur_time;
 
  // TCP connection
  String cmd = "AT+CIPSTART=\"TCP\",\"";
  cmd += "184.106.153.149"; // api.thingspeak.com Access IP
  cmd += "\",80";           // api.thingspeak.com access port, 80
  ser.println(cmd);
   
  if(ser.find("Error")){
    Serial.println("AT+CIPSTART error");
     lcd.setCursor(13, 3);
      lcd.print("Wifioff");
    return;
  }
  
  // Set String, Data to send by GET method
  String getStr = "GET /update?api_key=";
  getStr += apiKey;
  getStr +="&field1=";
  getStr += String(strTemp);
  getStr +="&field2=";
  getStr += String(strHumi);
  getStr +="&field3=";
  getStr += String(ugm3);
  getStr +="&field4=";
  getStr += String(percentage);
  getStr += "\r\n\r\n";
 
  // Send Data
  cmd = "AT+CIPSEND=";
  cmd += String(getStr.length());
  ser.println(cmd);
 
  if(ser.find(">")){
    ser.print(getStr);
     lcd.setCursor(14, 3);
      lcd.print("Wifion");
  
  }
  else{
    ser.println("AT+CIPCLOSE");
    // alert user
    Serial.println("AT+CIPCLOSE");
     lcd.setCursor(13, 3);
      lcd.print("Wifioff");
 
  }
   }
}
//co2 measurement sensor--------------------------------------------------------
float MGRead(int mg_pin)
{
    int i;
    float v=0;
 
    for (i=0;i<READ_SAMPLE_TIMES;i++) {
        v += analogRead(mg_pin);
        delay(READ_SAMPLE_INTERVAL);
    }
    v = (v/READ_SAMPLE_TIMES) *5/1024 ;
    return v;  
}
 
int  MGGetPercentage(float volts, float *pcurve)
{
   if ((volts/DC_GAIN )>=ZERO_POINT_VOLTAGE) {
      return -1;
   } else { 
      return pow(10, ((volts/DC_GAIN)-pcurve[1])/pcurve[2]+pcurve[0]);
    }
}
 
//--------------------------------------------

Custom parts and enclosures

Schematics

Arduino Air Meter (iot) Schematic
Arduino Air Meter (iot) Schematic
 schem    gypitku7bm

Comments

Similar projects you might like

Air Meter Making #1: Use The Arduino Wi-Fi Module ESP-01

by 4 developers

  • 4,205 views
  • 1 comment
  • 11 respects

Air Meter Making #3: Using Thingspeak

by 4 developers

  • 669 views
  • 0 comments
  • 1 respect

Air Meter Making #2: Use the Arduino DHT 11 Module

by 4 developers

  • 3,043 views
  • 4 comments
  • 10 respects

Temperature and humidity meter (iot)

Project showcase by gledel and 윤원호

  • 6,602 views
  • 1 comment
  • 15 respects

Hot Air Balloon or Weather Satellite Navigation System

Project tutorial by Patel Darshil

  • 1,960 views
  • 0 comments
  • 14 respects

UV METER (Music Visualizer) #1 With Arduino

Project tutorial by Team kj electronics

  • 2,657 views
  • 0 comments
  • 8 respects
Add projectSign up / Login