User Tools

Site Tools


amc2021:code:start

This is an old revision of the document!


Arduino Code

For Carbon Dioxide detection in air

#include "MQ135.h" 
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>  //Header file for LCD

LiquidCrystal_I2C lcd(0x27,16,2);//set the LCD aadress to x27 for a 16 chars and 2 line display

#define led          9                       //led on pin 9
 
const int gas_pin = A0;
MQ135 gasSensor = MQ135(gas_pin);
void setup(){

  lcd.init();                      // initialize the lcd 
  lcd.begin(16,2);
  lcd.backlight();
  lcd.clear();
  lcd.setCursor(4,0);
  lcd.print("Group L");

  pinMode(gas_pin,INPUT);  //MQ135 analog feed set for input
  pinMode(led,OUTPUT);     //led set for output
  
Serial.begin(9600); 
}

void loop(){
 float ppm = gasSensor.getPPM();
 Serial.println(ppm);
 delay(1000);
   lcd.clear();
   lcd.setCursor(0,0);          // set cursor of lcd to 1st row and 3rd column
   lcd.print("Air Quality: ");  // print message 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");
   }
 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");
  }

}

**For noise disturbance detection in the environment**

/* This code allows to follow the sound intensity using LM393 sensor: The number of times the sensor has detected a sound is summed up over a sampling time
SAMPLE_TIME. Then the sum (sampleBufferValue) is printed on serial monitor
A let is used to give a visual alarm if the sampleBufferValue
surpasses a given Threshold: Threshold*/
// 0 means silence and 1 means noise
const int OUT_PIN = 12; // The OUTPUT of the sound sensor is connected to the digital pin D12 of the Arduino
const int SAMPLE_TIME = 10; // The sampling time
const int Threshold = 100; // Threshold on cumulative counts for LED switching ON
unsigned long millisCurrent;
unsigned long millisLast = 0;
unsigned long millisElapsed = 0;
int sampleBufferValue = 0;

int led = 8; // LED is connected to the digital pin number 4 of the Arduino

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(led,OUTPUT); // the LED is connected as output
 
}

void loop() { // put your main code here, to run repeatedly:

  millisCurrent = millis();
  millisElapsed = millisCurrent - millisLast;
  if(digitalRead(OUT_PIN) == HIGH){ // HIGH means noise
   
  sampleBufferValue++;
  }
  if (millisElapsed > SAMPLE_TIME) { //here we will print the counts and test the threshold
  Serial.println(sampleBufferValue);
  if (sampleBufferValue > Threshold) {
    digitalWrite(led, HIGH); //blink LED 2 ms ON and 1 ms OFF
    delay(2);
    digitalWrite(led, LOW);
    delay(1);
  }
  digitalWrite(led, LOW);
  sampleBufferValue = 0;
  millisLast = millisCurrent;
  }
}
amc2021/code/start.1630578160.txt.gz ยท Last modified: 2023/01/05 14:38 (external edit)