ESP32 S3 - Gas Sensor

Learn how to use the ESP32 S3 with an MQ2 gas sensor to monitor air quality and detect flammable gases like LPG, smoke, propane, hydrogen, methane, and carbon monoxide in your environment. This tutorial walks you through every step, from wiring the sensor to writing and uploading the code, so you can build a working gas detection system quickly.

What you'll build:

  1. A digital gas detector that triggers when gas concentration exceeds a set threshold
  2. An analog gas concentration monitor that tracks gradual changes in air quality
  3. A Serial Monitor display showing real-time gas detection status and values
  4. A foundation for practical safety projects like alarms and ventilation controllers
ESP32 S3 - Gas Sensor

Hardware Preparation

1×ESP32 S3 WROOM N16R8
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×MQ2 Gas Sensor
1×Breadboard
1×Jumper Wires

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of MQ2 Gas Sensor

The MQ2 gas sensor is a versatile air quality module capable of detecting a wide range of flammable gases and smoke. It provides both a digital threshold output and an analog concentration output, making it suitable for everything from simple alarms to detailed air quality logging with the ESP32 S3.

Key Specifications

  • Detects multiple gases: LPG, smoke, alcohol, propane, hydrogen, methane, and carbon monoxide
  • Dual output options: digital (DO) and analog (AO) pins
  • Built-in potentiometer for sensitivity adjustment
  • Operating voltage: 5V power supply
  • Two LED indicators for power and gas detection status
  • The MQ2 sensor detects the overall presence of flammable gases rather than identifying individual gas types
  • Requires a warm-up period before accurate readings are possible
  • Low cost and straightforward to integrate with the ESP32 S3

Pinout

The MQ2 gas sensor module has four connection pins that map directly to your ESP32 S3:

  1. VCC pin: Connect to 5V power supply
  2. GND pin: Connect to ground (0V)
  3. DO pin: Digital output — outputs LOW when gas is detected above threshold, HIGH when below threshold
  4. AO pin: Analog output — voltage increases with gas concentration
MQ2 Gas Sensor Pinout

LED indicators:

  • PWR-LED: Power indicator — lights up when sensor receives power
  • DO-LED: Gas detection indicator — turns on when gas concentration exceeds threshold (DO pin is LOW)

How It Works

Digital output (DO pin):

  • Built-in potentiometer lets you set the gas concentration threshold
  • When gas concentration exceeds threshold: DO pin outputs LOW and DO-LED turns on
  • When gas concentration is below threshold: DO pin outputs HIGH and DO-LED turns off
  • Ideal for simple gas detection alarms

Analog output (AO pin):

  • Voltage increases as gas concentration increases
  • Voltage decreases as gas concentration decreases
  • Provides precise measurement of gas levels
  • Potentiometer adjustment doesn't affect AO pin readings
  • Perfect for monitoring gradual changes in air quality

The MQ2 Sensor Warm-up

The MQ2 gas sensor needs time to warm up before providing accurate readings.

Warm-up requirements:

  • First-time use or after long storage (1+ month): Warm up for 24-48 hours
  • Regular use: Warm up for 5-10 minutes only
  • During warm-up: Readings start high, then gradually decrease until stable
  • How to warm up: Simply connect VCC and GND to power (5V) or your ESP32 S3

Why warm-up matters:

  • Ensures sensor accuracy and stability
  • Without proper warm-up, readings will be unreliable

Wiring Diagram

Connecting the MQ2 gas sensor to your ESP32 S3 is straightforward — you can use the digital output, analog output, or both depending on your project needs. Pay attention to the power requirement: the MQ2 sensor needs 5V to operate correctly, so connect VCC to the 5V pin on your ESP32 S3.

Safety Notes

Always ensure the MQ2 sensor is powered from 5V rather than 3.3V, as insufficient voltage will result in inaccurate readings. Handle open flames or gas sources carefully when testing the sensor, and work in a well-ventilated area to avoid accidentally accumulating flammable gases during development.

The wiring diagram between ESP32 S3 MQ2 gas sensor

This image is created using Fritzing. Click to enlarge image

MQ2 Pin ESP32 S3 Pin
VCC 5V
GND GND
DO GPIO41
AO GPIO15

ESP32 S3 Code - Read value from DO pin

The following code reads the digital output from the MQ2 gas sensor to detect the presence of flammable gases. It monitors the DO pin every second and prints a clear status message to the Serial Monitor so you can see immediately when gas is detected or the air returns to normal.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-gas-sensor */ #define DO_PIN 41 // The Arduino Nano ESP32's pin connected to DO pin of the MQ2 sensor void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(115200); // 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"); }

Detailed Instructions

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Copy the code: Copy the above code and open it with Arduino IDE.
  3. Upload code: Click Upload button on Arduino IDE to upload code to ESP32 S3.
  4. Test the sensor: Place the MQ2 gas sensor near smoke or a flammable gas source you want to detect.
  5. Check results: Open the Serial Monitor to see gas detection status.
  6. Adjust sensitivity: If the LED stays on or off constantly, turn the potentiometer to fine-tune sensor sensitivity.
  7. Pro Tip: Test with a lighter (without igniting) to verify the sensor detects gas properly before deploying your project.
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
[06/16/2026 10:23:45] The gas is NOT present [06/16/2026 10:23:46] The gas is NOT present [06/16/2026 10:23:47] The gas is NOT present [06/16/2026 10:23:48] The gas is NOT present [06/16/2026 10:23:49] The gas is present [06/16/2026 10:23:50] The gas is present [06/16/2026 10:23:51] The gas is present [06/16/2026 10:23:52] The gas is present
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

ESP32 S3 Code - Read value from AO pin

The following code reads the analog output from the MQ2 gas sensor to measure gas concentration as a numerical value. The ESP32 S3's ADC converts the sensor voltage into a 12-bit value between 0 and 4095, where higher numbers indicate greater gas concentration, allowing you to track air quality changes over time.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-gas-sensor */ #define AO_PIN 15 // The Arduino Nano ESP32's pin connected to AO pin of the MQ2 sensor void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(115200); // set the ADC attenuation to 11 dB (up to ~3.3V input) analogSetAttenuation(ADC_11db); Serial.println("Warming up the MQ2 sensor"); delay(20000); // wait for the MQ2 to warm up } void loop() { int gasValue = analogRead(AO_PIN); Serial.print("MQ2 sensor AO value: "); Serial.println(gasValue); }

Detailed Instructions

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Copy the code: Copy the above code and open it with Arduino IDE.
  3. Upload code: Click Upload button on Arduino IDE to upload code to ESP32 S3.
  4. Test the sensor: Place the MQ2 gas sensor near smoke or a flammable gas source you want to detect.
  5. Monitor values: Open the Serial Monitor to see analog gas concentration readings.
  6. Interpret readings: Lower values indicate cleaner air; higher values indicate more gas is present.
  7. Pro Tip: Record baseline values in clean air first, then compare them when testing with gas sources to understand your sensor's response range.
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
[06/16/2026 14:32:10] MQ2 sensor AO value: 142 [06/16/2026 14:32:11] MQ2 sensor AO value: 138 [06/16/2026 14:32:12] MQ2 sensor AO value: 145 [06/16/2026 14:32:13] MQ2 sensor AO value: 587 [06/16/2026 14:32:14] MQ2 sensor AO value: 1245 [06/16/2026 14:32:15] MQ2 sensor AO value: 1967 [06/16/2026 14:32:16] MQ2 sensor AO value: 2431 [06/16/2026 14:32:17] MQ2 sensor AO value: 3156 [06/16/2026 14:32:18] MQ2 sensor AO value: 3892 [06/16/2026 14:32:19] MQ2 sensor AO value: 4023
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Using your readings:

  • Use DO or AO values to determine air quality based on your requirements
  • Trigger alarms when gas is detected
  • Automatically turn on ventilation systems
  • Log data for air quality monitoring over time

※ NOTE THAT:

This tutorial uses the analogRead() function to read values from an ADC (Analog-to-Digital Converter) connected to a sensor or component. The ESP32 S3's ADC is suitable for projects that do not require high accuracy. However, for projects needing precise measurements, keep the following in mind:

  • The ESP32 S3's ADC is not perfectly accurate and might require calibration for correct results. Each ESP32 S3 board can vary slightly, so calibration is necessary for each individual board.
  • Calibration can be challenging, especially for beginners, and might not always yield the exact results you want.

For projects requiring high precision, consider using an external ADC (e.g ADS1115) with the ESP32 S3 or using another Arduino, such as the Arduino Uno R4 WiFi, which has a more reliable ADC. If you still want to calibrate the ESP32 S3's ADC, refer to the ESP32 ADC Calibration Driver.

Application and Project Ideas

The ESP32 S3 paired with the MQ2 gas sensor opens up a wide range of practical safety and environmental monitoring projects you can build and deploy at home or in the field.

  1. Kitchen Gas Alarm: Detect LPG leaks near stoves and trigger a buzzer and LED alert before dangerous concentrations build up.
  2. Ventilation Controller: Automatically activate an exhaust fan when indoor air quality drops below an acceptable threshold.
  3. Tutorial Smoke Detector: Monitor a garage or tutorial for smoke from burning materials and alert occupants early.
  4. IoT Air Quality Logger: Use the ESP32 S3's WiFi to upload gas sensor readings to a cloud dashboard for remote monitoring.
  5. Smart Home Safety System: Integrate with a home automation platform to send push notifications when gas is detected.
  6. Camping Propane Monitor: Build a portable device that alerts you to propane leaks near camping stoves or RV appliances.

Video Tutorial

Watch the step-by-step video walkthrough for this ESP32 S3 project below.

Challenge Yourself

These challenges are designed to push your ESP32 S3 and MQ2 gas sensor project further, progressing from simple additions to full IoT safety systems.

  1. Beginner: Add a buzzer that beeps when gas is detected using the digital DO output.
  2. Beginner: Wire up an RGB LED that changes color based on analog gas concentration levels — green for clean air, yellow for moderate, red for high.
  3. Intermediate: Create a data logger that records timestamped gas readings to an SD card for later analysis.
  4. Intermediate: Build a web server hosted on the ESP32 S3 that displays real-time air quality data in a browser on your phone.
  5. Advanced: Combine multiple MQ sensors (MQ2, MQ135) to detect different gas types and visualize all readings on an OLED screen.
  6. Advanced: Create a full IoT air quality monitoring system that sends email or SMS alerts via a cloud service when dangerous gas levels are detected.

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!