Created
September 17, 2020 13:39
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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