Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This sample code is for smoke detector using MQ2 sensor on Maker NANO
#define MQ2 A0
#define GREEN_LED 4
#define YELLOW_LED 5
#define RED_LED 6
#define PIEZO 8
int sensorValue = 0;
void setup()
{
Serial.begin(9600); // Initialize serial communications with the PC
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(YELLOW_LED, OUTPUT);
pinMode(PIEZO, OUTPUT);
pinMode(MQ2, INPUT);
digitalWrite(GREEN_LED, HIGH);
}
void loop()
{
sensorValue = analogRead(MQ2);
Serial.println(sensorValue);
if (sensorValue > 300)
{
digitalWrite(GREEN_LED, LOW);
digitalWrite(RED_LED, HIGH);
tone(PIEZO, 1000, 300);
delay(700);
}
else
{
noTone;
digitalWrite(GREEN_LED, HIGH);
digitalWrite(RED_LED, LOW);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment