Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
Lesson 04: Gas Sensor Module (MQ-2)
In this lesson, you will learn how to measure gas concentrations using an MQ-2 sensor with an ESP32 Development Board. We’ll cover reading the analog output of the gas sensor and displaying it on the serial monitor. This project is ideal for beginners in electronics, providing hands-on experience with sensors and microcontrollers while teaching about analog signal processing and serial communication.
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
|---|---|---|
Universal Maker Sensor Kit |
94 |
You can also buy them separately from the links below.
Component Introduction |
Purchase Link |
|---|---|
ESP32 & Development Board (ESP32 Board) |
|
Wiring
Code
Code Analysis
The first line of code is a constant integer declaration for the gas sensor pin. We use the pin 25 to read the output from the gas sensor.
const int sensorPin = 25;
The
setup()function is where we initialize our serial communication at a baud rate of 9600. This is necessary to print the readings from the gas sensor to the serial monitor.void setup() { Serial.begin(9600); // Start serial communication at 9600 baud rate }
The
loop()function is where we continuously read the analog value from the gas sensor and print it to the serial monitor. We use theanalogRead()function to read the analog value from the sensor. We then wait for 50 milliseconds before the next reading. This delay gives some breathing space for the serial monitor to process the data.Note
MQ2 is a heating-driven sensor that usually requires preheating before use. During the preheating period, the sensor typically reads high and gradually decreases until it stabilizes.
void loop() { Serial.print("Analog output: "); Serial.println(analogRead(sensorPin)); // Read the analog value of the gas sensor and print it to the serial monitor delay(50); // Wait for 50 milliseconds }