HOW to Use MQ-2 Gas Sensor 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
Connection Diagram
Test Code
//**********************************************************************************
/*
* Filename : MQ2
* Description : Read the basic usage of Digital, ADC,DAC and Voltage
* Auther : http//www.keyestudio.com
*/
//MQ_2 two pins 13, 34, respectively
#define PIN_ANALOG_IN 34
int digitalPin = 13;
//The following two variables hold the digital signal and adc values respectively
int analogVal = 0;
int adcVal = 0;
void setup() {
Serial.begin(9600);
pinMode(digitalPin, INPUT); //Digital pin 13 is set to input mode
}
//In loop(),the digitalRead()function is used to obtain the digital value,
//the analogRead() function is used to obtain the ADC value.
//and then the map() function is used to convert the value into an 8-bit precision DAC value.
//The input and output voltage are calculated according to the previous formula,
//and the information is finally printed out.
void loop() {
int digitalVal = digitalRead(digitalPin); //Read digital signal;
int adcVal = analogRead(PIN_ANALOG_IN);
int dacVal = map(adcVal, 0, 4095, 0, 255);
double voltage = adcVal / 4095.0 * 3.3;
Serial.printf("digitalVal: %d, \t ADC Val: %d, \t DAC Val: %d, \t Voltage: %.2fV\n",digitalVal, adcVal, dacVal, voltage);
if (digitalVal == 1) {
Serial.println(" Normal");
}
else {
Serial.println(" Exceeding");
}
delay(100); //Delay time 100 ms
}
//**********************************************************************************
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. Rotating the potentiometer on the sensor, we can adjust the red LED bright and not bright critical point. Open the monitor, set baud rate to 9600 and display the corresponding data and characters. When the sensor detects the smoke or combustible gas, the red LED lights up and the digital value changes from 1 to 0, the ADC value, DAC value and voltage value increase, as shown below.