HOW to Make a Smoke Alarm with ESP32
Description
In this experiment, we will make a smoke alarm by a TM16504-Digit segment module, a gas sensor and an active buzzer.
Required Components
|
|
|
|
---|---|---|---|
ESP32 Board*1 |
ESP32 Expansion Board*1 |
Keyestudio Active Buzzer*1 |
Keyestudio TM16504-Digit Segment Module*1 |
|
|
|
|
keyestudio Analog Gas Senso*1 |
3P Dupont Wire*2 |
4P Dupont Wire*1 |
Micro USB Cable*1 |
Connection Diagram
Test Code
//**********************************************************************************
/*
* Filename : smoke alarm
* Description : MQ2 controls a buzzer and a four-digit analog smoke tester
* Auther : http//www.keyestudio.com
*/
#include "TM1650.h" //Import the TM1650 library file
int adcVal = 0; //display ADC value
//the interfaces are GPIO21 and GPIO22
#define DIO 21
#define CLK 22
TM1650 DigitalTube(CLK,DIO);
void setup() {
DigitalTube.setBrightness(); //set brightness, 0---7, default : 2
DigitalTube.displayOnOFF(); //display on or off, 0=display off, 1=display on, default : 1
for(char b=1;b<5;b++){
DigitalTube.clearBit(b); //DigitalTube.clearBit(0 to 3); Clear bit display.
}
// DigitalTube.displayDot(1,true); //Bit0 display dot. Use before displayBit().
DigitalTube.displayBit(1,0); //DigitalTube.Display(bit,number); bit=0---3 number=0---9
pinMode(15, OUTPUT);//the buzzer is connected to GPIO15
}
void loop() {
adcVal = analogRead(34);//Read the ADC values of MQ2
displayFloatNum(adcVal);;//Four digit tube display adcVal values
if (adcVal > 1000) {//ADC value is greater than 1000
digitalWrite(15, HIGH); // buzzer alarming
} else {//or else
digitalWrite(15, LOW); //Turn off the buzzer
}
delay(100);//delay 100ms
}
void displayFloatNum(float adcVal){
if(adcVal > 9999)
return;
int dat = adcVal*10;
//DigitalTube.displayDot(2,true); //Bit0 display dot. Use before displayBit().
if(dat/10000 != 0){
DigitalTube.displayBit(1, dat%100000/10000);
DigitalTube.displayBit(2, dat%10000/1000);
DigitalTube.displayBit(3, dat%1000/100);
DigitalTube.displayBit(4, dat%100/10);
return;
}
if(dat%10000/1000 != 0){
DigitalTube.clearBit(1);
DigitalTube.displayBit(2, dat%10000/1000);
DigitalTube.displayBit(3, dat%1000/100);
DigitalTube.displayBit(4, dat%100/10);
return;
}
if(dat%1000/100 != 0){
DigitalTube.clearBit(1);
DigitalTube.clearBit(2);
DigitalTube.displayBit(3, dat%1000/100);
DigitalTube.displayBit(4, dat%100/10);
return;
}
DigitalTube.clearBit(1);
DigitalTube.clearBit(2);
DigitalTube.clearBit(3);
DigitalTube.displayBit(4, dat%100/10);
}
//**********************************************************************************
Code Explanation
Define an integer variable val to store the analog value of the smoke sensor, and then we display the analog value in the four-digit digital tube, and then set a threshold, and when the threshold is reached, the buzzer will sound.
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 concentration of combustible gas exceeds the standard, the active buzzer module will give an alarm, and the four-digit digital tube will display the concentration value.