Translate

Membuat Alat Monitor Gas Polutan Udara dan Debu Serta Suhu - Kelembaban Menggunakan DHT22 sensor MQ135 MQ7 dan data logger SDcard dilengkapi RTC ds3231

Membuat Alat Monitor Gas Polutan Udara dan Debu Serta Suhu - Kelembaban Menggunakan DHT22 sensor MQ135 MQ7 dan data logger SDcard dilengkapi RTC ds3231


         Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang dapat memonitoring gas polutan menggunakan sensor MQ135 dan MQ7 kemudian debu dan suhu - kelembaban, alat ini dilengkapi dengan data logger berupa sd card dan juga ada tampilan waktu, untuk tampilan waktu menggunakan modul RTC ds3231 dan juga waktu dan tanggal disimpan ke dalam sd card. untuk mengetahui program dan skemanya yaitu seperti berikut.  



a. Arduino Uno




b. Sensor Gas Mq135 dan MQ7





c. Sensor Debu




d. Sensor suhu dan kelembaban DHT22




e. Data Logger SD Card




f. Modul RTC ds3231






g. Program Arduino IDE

//https://github.com/R2D2-2017/R2D2-2017/wiki/MQ-7-gas-sensor

#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <LiquidCrystal_I2C.h>  //i2C LCD Library
LiquidCrystal_I2C lcd(0x27, 20, 4); //library i2c lcd
#include "DHT.h"
#define DS3231_I2C_ADDRESS 0x68
#include "MQ135.h"
#define ANALOGPIN A1    //  Define Analog PIN on Arduino Board
#define RZERO 206.85    //  Define RZERO Calibration Value
MQ135 gasSensor = MQ135(ANALOGPIN);

#define DHTPIN 7    
#define DHTTYPE DHT22   
DHT dht(DHTPIN, DHTTYPE);

  float RS_air; //  Get the value of RS via in a clear air
  float R0 = 0.25;  // Get the value of R0 via in H2
  float sensorValue;
  float sensor_volt;
  float RS_gas;
  float ratio;


//mosi - 11
//miso - 12
//clk - 13
//cs - 4

int cacah = 0;
File myFile;
int ledx = 2;
int suhu;
int hum;

float Dustval;

float ppm;
float ppm2;

byte second;
byte minute;
byte hour;
byte dayOfWeek; 
byte dayOfMonth; 
byte month; 
byte year;

byte decToBcd(byte val)
{
  return( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return( (val/16*10) + (val%16) );
}

void setup(){
  Wire.begin();
  lcd.begin(); //set lcd i2c
  lcd.noCursor(); //biar gak ada cursor di lcd
  lcd.clear(); //clear lcd
  dht.begin();
  Serial.begin(9600);
 pinMode(ledx,OUTPUT);

   // set the initial time here:
  // DS3231 seconds, minutes, hours, day, date, month, year
  // setDS3231time(0,45,22,4,9,5,18);

  float rzero = gasSensor.getRZero();
  delay(3000);
  Serial.print("MQ135 RZERO Calibration Value : ");
  Serial.println(rzero);
    
}



void setDS3231time()
{
  // sets time and date data to DS3231
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set next input to start at the seconds register
  Wire.write(decToBcd(second)); // set seconds
  Wire.write(decToBcd(minute)); // set minutes
  Wire.write(decToBcd(hour)); // set hours
  Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
  Wire.write(decToBcd(month)); // set month
  Wire.write(decToBcd(year)); // set year (0 to 99)
  Wire.endTransmission();
}
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}
void displayTime()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
  &year);
  // send it to the serial monitor
  lcd.setCursor(10,0);
  lcd.print(hour, DEC);
  // convert the byte variable to a decimal number when displayed
  lcd.print(":");
  if (minute<10)
  {
    lcd.print("0");
  }
  lcd.print(minute, DEC);
  lcd.print(":");
  if (second<10)
  {
    lcd.print("0");
  }
  lcd.print(second, DEC);
  lcd.print(" ");
  
  lcd.setCursor(10,1);
  lcd.print(dayOfMonth, DEC);
  lcd.print("/");
  lcd.print(month, DEC);
  lcd.print("/");
  lcd.print(year, DEC);
   /*
  lcd.print(" Day of week: ");
  switch(dayOfWeek){
   
  case 1:
    lcd.println("Sunday");
    break;
  case 2:
    lcd.println("Monday");
    break;
  case 3:
    lcd.println("Tuesday");
    break;
  case 4:
    lcd.println("Wednesday");
    break;
  case 5:
    lcd.println("Thursday");
    break;
  case 6:
    lcd.println("Friday");
    break;
  case 7:
    lcd.println("Saturday");
    break;
  
  }
    */
}



void loop(){
displayTime();
gasmq135();
gasmq7();
debu();

 float h = dht.readHumidity();
 float t = dht.readTemperature();
 float f = dht.readTemperature(true);

  hum = h;
  suhu = t;

  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  float hif = dht.computeHeatIndex(f, h);
  float hic = dht.computeHeatIndex(t, h, false);
  
lcd.setCursor(0,0); 
lcd.print("Gas1=");
lcd.print(ppm);
lcd.print("   ");
lcd.setCursor(0,1); 
lcd.print("Gas2=");
lcd.print(ppm2);
lcd.print("   ");
lcd.setCursor(0,2); 
lcd.print("debu=");
lcd.print(Dustval);
lcd.print("   ");
lcd.setCursor(0,3); 
lcd.print("T/H=");
lcd.print(suhu);
lcd.print("/");
lcd.print(hum);
lcd.print("    ");
lcd.setCursor(10,2); 
lcd.print(cacah);
delay(200);

cacah++;

if(cacah > 10){
cacah = 0;
simpan();
}

}


void gasmq135(){

  ppm = gasSensor.getPPM();
  delay(1000);
  //Serial.print("CO2 ppm value : ");
  //Serial.println(ppm);
    
}


void gasmq7(){

analogWrite(A3, 1023);
//Heather 1.4 V 90 s
    analogWrite(A3, (1023/5)*1.4 );
    for(int i = 0; i<90; i++){
        sensorValue = analogRead(A2);
        sensor_volt = sensorValue/1024*5.0;
        RS_gas = (5.0-sensor_volt)/sensor_volt;
        ratio = RS_gas/R0; //Replace R0 with the value found using the calibration code
        float kal = 83.39 / ratio;
        ppm2 = pow(kal , 0.63);      
        delay(100);
    }

}


void debu(){
//aktif low
//GP2Y1010AU0F_SAMPLEDELAY should be 280us to perform the correct reading
//this delay should consider that ADC conversion takes 13 ADC clock cycles
//ADCtime(s) = (1/ADCclock)*13 = (1/FCPU/ADCprescaler)*13
// es (1/(8000000/64))*13 = 0.000104s = 104us
//so to perform reading at correct time
//280 - (1/FCPU/ADCprescaler)*13*1000000   , 1000000 is the conversion factor from s to us
// es. 280 - 104 = 176

digitalWrite(ledx,LOW);    //on
delayMicroseconds(176);

int dataadc = analogRead(A0);
delayMicroseconds(40);

digitalWrite(ledx,HIGH);   //off
delayMicroseconds(9680);

float v = dataadc * (5.0 / 1023.0);

//y = 0.166x - 0.129
 Dustval = (v * 0.166) - 0.129;

}



void simpan(){
 byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
  &year);
  
while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
  pinMode(10, OUTPUT);

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    myFile.print("waktu=");
    myFile.print(hour); 
    myFile.print(":");
    myFile.print(minute); 
    myFile.print(":");
    myFile.println(second);
    myFile.print("Tanggal=");
    myFile.print(dayOfWeek); 
    myFile.print("/");
    myFile.print(dayOfMonth); 
    myFile.print("/");
    myFile.print(month);
    myFile.print("/");
    myFile.println(year); 
    myFile.print("CO2= ");
    myFile.println(ppm);
    myFile.print("CO= ");
    myFile.println(ppm2);
    myFile.print("Debu= ");
    myFile.println(Dustval);
    myFile.print("Suhu= ");
    myFile.println(suhu);
    myFile.print("Kelembaban= ");
    myFile.println(hum); 

    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }


}






h. VIDEO HASILNYA









No comments:

Post a Comment