#define MQ2pin A0
int LED=13;
float sensorValue; //variable to store sensor value
void setup()
{
Serial.begin(9600); // sets the serial port to 9600
pinMode(MQ2pin, INPUT);
pinMode(13,OUTPUT);
Serial.println("MQ2 warming up!");
delay(20000); // allow the MQ2 to warm up
}
void loop()
{
sensorValue = analogRead(MQ2pin); // read analog input pin 0
Serial.print("Sensor Value: ");
Serial.println(sensorValue);
if(sensorValue > 350)
{
Serial.println(" | Smoke detected!");
digitalWrite(LED,HIGH);
}
else
{
Serial.println(" NO Smoke detected!");
digitalWrite(LED,LOW);
}
delay(2000); // wait 2s for next reading
}