HOW to Use Alcohol Sensor with ESP32
Description
In the last experiment, we made a smoke alarm. In this experiment, we combine the active buzzer, the MQ-3 alcohol sensor, and a four-digit digital tube to test the alcohol concentration through the alcohol sensor. Then, the concentration to control the active buzzer alarm and the four-digit digital tube to display the concentration. So as to achieve the simulation effect of alcohol detector.
Components Required
|
|
|
|
---|---|---|---|
ESP32 Board*1 |
ESP32 Expansion Board*1 |
Active Buzzer*1 |
Keyestudio DIY TM1650 4-Digit Tube Display*1 |
|
|
|
|
keyestudio Alcohol Sensor*1 |
3P Dupont Wire*2 |
4P Dupont Wire*1 |
Micro USB Cable*1 |
Connection Diagram
Test Code
//**********************************************************************************
/*
* Filename : breathalyzer
* Description : MQ3 controls a buzzer and a four-digit tube to simulate a breathalyzer.
* 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 MQ3
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 ADC value of the alcohol sensor, then we display the analog value in the four-digit display module and set a threshold.
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 different alcohol concentrations are detected, the active buzzer module will alarm, and the four-digit digital display will show the concentration value.