1. Introduction to MQ-2
The gas sensitive material used in the MQ-2 gas sensor is tin dioxide (SnO2) with low conductivity in clean air. When there is combustible gas in the environment where the sensor is located, the conductivity of the sensor increases with the concentration of combustible gas in the air. The change in conductivity can be converted into an output signal corresponding to the gas concentration using a simple circuit. MQ-2 gas sensor has high sensitivity to liquefied petroleum gas, propane and hydrogen, and is also ideal for the detection of natural gas and other flammable vapors. This sensor detects a wide range of combustible gases and is a low-cost sensor suitable for many applications.
2. Circuit connection
ESP32 | MQ-2 |
---|---|
3v3 | VCC |
GND | GND |
DO | GPIO15 |
TO THE | GPIO2 |
3. MicroPython code
The configuration here measures 3.3V, in order to adapt to my ESP32.
from machine import Pin, ADC
import time
# 模拟量
ps2_y = ADC(Pin(2))
ps2_y.atten(ADC.ATTN_11DB) # 这里配置测量量程为3.3V
# 数字量
p15 = Pin(15, Pin.IN)
# 循环检测
while True:
val_y = ps2_y.read() # 0-4095
light = p15.value() # 1为没有危险气体,0为有危险气体。
print(val_y, light)
time.sleep(0.1)
if light == 0:
print("检测到危险气体,请远离!")