ESP32 S3 UNO - Gas Sensor
This ESP32 S3 Uno gas sensor tutorial shows you how to connect an MQ2 gas sensor to the ESP32 S3 Uno form board and check the air with Arduino code. The MQ2 is a smoke sensor and LPG sensor in one small module. It also reacts to alcohol, propane, hydrogen, methane and carbon monoxide. You will see "gas present" messages and gas level numbers on the Serial Monitor.
In this tutorial, you will:
- Learn how the MQ2 gas sensor module works and why it needs to warm up
- Wire the MQ2 sensor with a 5V heater and a safe voltage divider on its outputs
- Read the DO pin to detect if gas is present
- Read the AO pin with analogRead() to measure the gas level

Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of MQ2 Gas Sensor
It helps to know what the MQ2 can and cannot tell you before you build anything. This way, you will read the numbers the right way later.
The MQ2 gas sensor reacts to LPG, smoke, alcohol, propane, hydrogen, methane and carbon monoxide in the air around it. The module gives you two outputs: one digital and one analog.
The sensor cannot tell these gases apart. It only gives one result for the whole mix of gases in the air. Still, this is enough to spot a gas leak or bad air, and then react, for example by sounding an alarm or turning on a fan.
Pinout
The module has only four pins, so the wiring is short. Two pins are for power and two pins carry the result.
VCC pin
Connect it to 5V. This pin also powers the small heater inside the sensor.
GND pin
Connect it to GND (0V).
DO pin
This is the digital output. It goes LOW when it finds flammable gas and stays HIGH when the air is clean. A small potentiometer on the module sets the gas level that switches it.
AO pin
This is the analog output. Its voltage rises when there is more gas and drops when there is less gas.

The module also has two LEDs. The PWR-LED turns on when the module has power. The DO-LED follows the DO pin: it is on when gas is found and off when the air is clean.
How It Works
The two outputs work in different ways. Knowing the difference helps you choose the right one for your project.
DO pin
The potentiometer on the module sets a gas threshold. When the gas level around the sensor is above this threshold, the DO pin goes LOW and the DO-LED turns on. When the gas level is below it, the DO pin goes HIGH and the DO-LED turns off.
AO pin
The AO voltage simply follows the gas level. More gas gives a higher voltage, and less gas gives a lower voltage. The potentiometer has no effect on the AO pin.
WARNING
The MQ2 module runs on 5V, so its DO and AO pins can reach about 5V. The ESP32 S3 Uno pins are NOT 5V tolerant. Never connect DO or AO straight to the board. Use a voltage divider on each output, as shown in the wiring section below.
The MQ2 Sensor Warm-up
The MQ2 has a heater inside, and it only gives good readings when it is hot. If you skip the warm-up, the first numbers will be too high and jump around.
If the sensor has not been used for a long time (more than a month), let it warm up for 24 to 48 hours before you trust the results. If you used it recently, 5 to 10 minutes is enough. The readings start high, then drop and settle after a short time.
To warm it up, connect VCC to a 5V supply and GND to ground and leave it on. You can use the 5V and GND pins of the ESP32 S3 Uno for this.
ESP32 S3 Uno Pinout
The image below shows the pinout diagram of the ESP32 S3 Uno form board. Use it to find the 5V, D2 and A0 header pins and their GPIO numbers when you wire the gas sensor.

Wiring Diagram
You can use the DO output, the AO output, or both. The heater stays on 5V, but each output goes through a voltage divider before it reaches the ESP32 S3 Uno.

This image is created using Fritzing. Click to enlarge image
| MQ2 Sensor Pin | ESP32 S3 Uno Pin |
|---|---|
| VCC | 5V |
| GND | GND |
| DO | D2 (GPIO18) through a voltage divider |
| AO | A0 (GPIO2) through a voltage divider |
Build one voltage divider for each output you use:
| Divider Part | Connection |
|---|---|
| 10 kΩ resistor (top) | from the sensor pin (DO or AO) to the board pin (D2 or A0) |
| 20 kΩ resistor (bottom) | from the board pin (D2 or A0) to GND |
WARNING
The wiring diagram image shows the sensor outputs going straight to D2 and A0. On the ESP32 S3 Uno, always add the voltage divider between them. The 10 kΩ / 20 kΩ divider turns a 5V signal into about 3.3V, which is safe for the ESP32 S3 Uno pins. Without it, the 5V signal from DO or AO can damage GPIO18 or GPIO2. If you have no 20 kΩ resistor, put two 10 kΩ resistors in series. A logic level converter also works for the DO pin.
ESP32 S3 Uno Code - Read value from DO pin
This sketch waits 20 seconds for the sensor to warm up. Then it reads the DO pin on D2 (GPIO18) again and again. It prints whether gas is present or not on the Serial Monitor.
Detailed Instructions
Follow these steps in order:
- New to the ESP32 S3 Uno? Follow ESP32 S3 Uno - Getting Started first.
- Wire it up as shown in the diagram, with the voltage divider on the DO pin.
- Connect the board to your computer with a USB Type-C cable.
- Open Arduino IDE, choose the ESP32S3 Dev Module board and the correct COM port.
- Copy the code above and paste it into Arduino IDE.
- Upload the code with the Upload button.
- Test it by holding the MQ2 sensor near the smoke or gas you want to detect.
- Check the result on the Serial Monitor.
- Tip: if the DO-LED stays on all the time, or never turns on, turn the small potentiometer on the module to change the sensitivity.
ESP32 S3 Uno Code - Read value from AO pin
This sketch also waits 20 seconds for warm-up. After that, it reads the AO pin on A0 (GPIO2) with analogRead() and prints the gas level. A bigger number means more gas.
In setup(), the code calls analogSetAttenuation(ADC_11db). By default, the ESP32 S3 Uno ADC can only measure a small voltage. With 11 dB attenuation, the analog pin can read the full range, from 0V up to about 3.3V. Without this line, the readings hit 4095 too early and every gas level looks the same.
The ESP32 S3 Uno has a 12-bit ADC, so the value goes from 0 to 4095, not from 0 to 1023. Because of the voltage divider, the board sees about two thirds of the real AO voltage.
Detailed Instructions
- Wire it up with the voltage divider on the AO pin.
- Copy the code above and paste it into Arduino IDE.
- Upload the code with the Upload button.
- Test it by holding the MQ2 sensor near the smoke or gas you want to detect.
- Check the result on the Serial Monitor.
- Tip: write down the value in clean air first. Then pick your own threshold above it to sound an alarm or turn on a fan.
With the DO or AO values, you can judge the air quality by your own rules, and then trigger an alarm or start a ventilation system.
Video Tutorial
Watch the video below to see this ESP32 S3 Uno project step by step.
Function References
These are the main functions used in the two sketches. Click a name to learn more about it.
FAQ
Can I power the MQ2 gas sensor from the 3.3V pin of the ESP32 S3 Uno?
No. The heater inside the MQ2 needs 5V to reach the right temperature. At 3.3V it stays too cold, and the readings are weak and wrong. Power VCC from the 5V pin and protect the outputs with a voltage divider.
Why do I need a voltage divider on the DO and AO pins?
The module runs on 5V, so its outputs can rise to about 5V. The ESP32 S3 Uno pins are NOT 5V tolerant, and 5V can damage them. A 10 kΩ and 20 kΩ divider brings the signal down to about 3.3V.
Why does the code call analogSetAttenuation(ADC_11db)?
It sets the ESP32 S3 Uno ADC input range to about 0 to 3.3V, so the full sensor signal maps to 0 to 4095. Without it, the range is smaller and the readings saturate early. The UNO R4 does not need this line, because it is for ESP32 boards only. Keep the voltage on the analog pin at 3.3V or less.
Can the MQ2 tell me which gas it found or the exact ppm?
No. The MQ2 gives one value for the whole mix of gases. It cannot say which gas it is. Getting real ppm values needs careful calibration with known gas levels, so most projects just use a threshold.
Why are the first readings so high?
The heater is still warming up. Wait 5 to 10 minutes after power-on, or 24 to 48 hours for a new sensor or one that sat unused for a long time. The readings drop and become steady once the sensor is hot.
What if my project needs more pins than the Uno headers give?
The ESP32 S3 Uno has two extra rows of holes with more GPIOs. GPIO15 and GPIO16 support I/O, PWM and analog input. GPIO45 and GPIO35 to GPIO42 support I/O and PWM. GPIO47 and GPIO48 are output only, with PWM. Solder pin headers to use them and write the GPIO number in your code. For a second gas sensor's AO pin, use GPIO15 or GPIO16. For a DO pin, use GPIO35 to GPIO42, but not GPIO47 or GPIO48, because they cannot read inputs. Keep the voltage divider on any new sensor output too.
Be careful with two groups of pins. First, GPIO47 and GPIO48 may run at 1.8V instead of 3.3V on some modules (especially ones with "V" in the name, like R8V or R16V), so 3.3V devices can be damaged or act strangely. Second, GPIO0, GPIO3 (D6), GPIO45 and GPIO46 (D9) are boot strapping pins, and a wrong connection can stop the board from booting or uploading code. Use the other extra pins first.
Troubleshooting
| Problem | Possible Cause | Solution |
|---|---|---|
| No COM port in Arduino IDE | USB driver missing or board not in upload mode | Install the CP210x or CH340 driver, try another USB cable, or hold BOOT while you press RESET |
| Sensor and PWR-LED stay off | VCC or GND not connected, or VCC on the wrong pin | Connect VCC to the 5V pin and GND to GND, then check the jumper wires |
| Readings are very high and unstable at start | Heater not warmed up yet | Wait 5 to 10 minutes, or 24 to 48 hours for a new sensor |
| DO always shows gas present or never shows it | Threshold potentiometer set too low or too high | Turn the potentiometer slowly while you watch the DO-LED in clean air |
| DO reads LOW even in clean air | Divider pulls the HIGH level too low on some modules | Check the resistor values, or use a logic level converter on the DO pin |
| AO value stays at 4095 | No attenuation set, or no voltage divider on AO | Keep analogSetAttenuation(ADC_11db) in setup() and add the 10 kΩ and 20 kΩ divider |
| AO value stays near 0 | AO wired to the wrong pin or divider wired wrong | Check that AO goes to A0 (GPIO2) through the 10 kΩ resistor, and the 20 kΩ resistor goes to GND |
| Serial Monitor shows strange characters | Wrong baud rate | Set the Serial Monitor to 9600 baud |