Skip to content

Instantly share code, notes, and snippets.

@suadanwar
Created September 17, 2020 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suadanwar/6edb02ce392c5c31a66db3470937fd2d to your computer and use it in GitHub Desktop.
Save suadanwar/6edb02ce392c5c31a66db3470937fd2d to your computer and use it in GitHub Desktop.
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