HOW To Detect Some Gas with ESP32
Description
This analog gas sensor - MQ2 is used in gas leakage detecting equipment in consumer electronics and industrial markets.
This sensor is suitable for detecting LPG, I-butane, propane, methane, alcohol, Hydrogen and smoke. It has high sensitivity and quick response.
In addition, the sensitivity can be adjusted by rotating the potentiometer.
In the experiment, we read the analog value at the A0 port and the D0 port to determine the content of gas.
Working Principle
The greater the concentration of smoke, the greater the conductivity, the lower the output resistance, the greater the output analog signal.
When in use, the A0 terminal reads the analog value of the corresponding gas; the D0 terminal is connected to an LM393 chip (voltage comparator), we can adjust the alarm threshold of the measured gas through the potentiometer, and output the digital value at D0. When the measured gas content exceeds the critical point, the D0 terminal outputs a low level. When the measured gas content does not exceed the critical point, the D0 terminal outputs a high level.
Required Components
|
|
|
|
|
---|---|---|---|---|
ESP32 Board*1 |
ESP32 Expansion Board*1 |
keyestudio DIY Analog Gas Sensor*1 |
4P Dupont Wire*1 |
Micro USB Cable*1 |
Connection Diagram
Test Code
# Import Pin, ADC and DAC modules.
from machine import ADC,Pin,DAC
import time
# Turn on and configure the ADC with the range of 0-3.3V
mq2_D = Pin(13, Pin.IN)
adc=ADC(Pin(34))
adc.atten(ADC.ATTN_11DB)
adc.width(ADC.WIDTH_12BIT)
# Read digital value and ADC value once every 0.1seconds, convert ADC value to DAC value and Voltage value and output it,
# and print these data to “Shell”.
while True:
digitalVal = mq2_D.value()
adcVal=adc.read()
dacVal=adcVal//16
voltage = adcVal / 4095.0 * 3.3
print("digitalVal:",digitalVal,"ADC Val:",adcVal,"DACVal:",dacVal,"Voltage:",voltage,"V", end = " ")
if digitalVal == 0:
print("Exceeding")
else:
print("Normal")
time.sleep(0.1)
Test Result
Connect the wires according to the experimental wiring diagram and power on. Click “Run current script”, the code starts executing. The “shell” window will display the corresponding data and string. After powering on, by rotating the potentiometer on the sensor, we can adjust the red LED bright and not bright critical point.
When the sensor detects the smoke or combustible gas, the red LED lights up and the digital value in the “Shell” window changes from 1 to 0, the ADC value, DAC value and voltage value increase, as shown below. Press “Ctrl+C”or click“Stop/Restart backend”to exit the program.