/*
* This ESP32 code is created by esp32io.com
*
* This ESP32 code is released in the public domain
*
* For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-gas-sensor
*/
#define DO_PIN 16 // ESP32's pin GPIO16 connected to DO pin of the MQ2 sensor
void setup() {
// initialize serial communication
Serial.begin(9600);
// initialize the ESP32's pin as an input
pinMode(DO_PIN, INPUT);
Serial.println("Warming up the MQ2 sensor");
delay(20000); // wait for the MQ2 to warm up
}
void loop() {
int gasState = digitalRead(DO_PIN);
if (gasState == HIGH)
Serial.println("The gas is NOT present");
else
Serial.println("The gas is present");
}