HOW to Build a Fire Alarm with ESP32
Description
In this experiment, we will make a fire alarm system. Just use a flame sensor to control an active buzzer to emit sounds.
Required Components
|
|
|
---|---|---|
ESP32 Board*1 |
ESP32 Expansion Board*1 |
Keyestudio DIY电Active Buzzer*1 |
|
|
|
Micro USB Cable*1 |
3P Dupont Wire*2 |
keyestudio DIY Flame Sensor*1 |
Connection Diagram
Test Code
/*
* Description : Controlling the buzzer by flame sensor.
* Auther : http//www.keyestudio.com
*/
int item = 0;
void setup() {
Serial.begin(9600);
pinMode(4, INPUT);//Flame sensor digital pin is connected to GPIO4
pinMode(15, OUTPUT);//Buzzer pin is connected to GPIO15
}
void loop() {
item = digitalRead(4);//Read the digital level output by the flame sensor
Serial.println(item);//Newline print level signal
if (item == 0) {//Flame detected
digitalWrite(15, HIGH);//Turn on the buzzer
} else {//Otherwise, turn off the buzzer
digitalWrite(15, LOW);
}
delay(100);//Delay 100ms
}
Code Explanation
This flame sensor uses an analog pin and a digital pin. When a flame is detected, the digital pin outputs a low level. In this experiment we will use the digital port.
Test Result
Connect the wires according to the experimental wiring diagram, compile and upload the code to the ESP32. After uploading successfully,we will use a USB cable to power on. When the sensor detects the flame, the external active buzzer will emit sounds, otherwise the active buzzer will not emit sounds.