How to Use ESP32 with MP-4 Combustible Gas Sensor
How to Use ESP32 with MP-4 Combustible Gas Sensor
This guide explains how to use ESP32 with the MP-4 combustible gas sensor. This is a small, inexpensive sensor that is easy to use and can detect butane gas (from lighters) and alcohol. We will read analog values to observe voltage changes when the sensor detects combustible gases.
Required Components
- NodeMCU ESP32 board (ESP-WROOM-32)
- MP-4 or MP4 Combustible Gas Sensor
- Breadboard
- Jumper wires (male-to-male and male-to-female)
- Micro USB cable for connecting ESP32 to computer
Wiring ESP32 to MP-4
The MP-4 sensor has 3 pins. Connect them to ESP32 as follows:
| ESP32 | MP-4 |
|---|---|
| 3V3 | VCC |
| GND | GND |
| D35 | A0 |
Arduino Code for Reading Gas Values
// Define the pin connected to the sensor
const int gasSensorPin = 35; // Analog pin on ESP32
void setup() {
Serial.begin(115200);
// Set ADC resolution to 12-bit (0-4095)
analogReadResolution(12);
}
void loop() {
// Read analog value from sensor
int sensorValue = analogRead(gasSensorPin);
// Convert to voltage (ESP32 uses 3.3V reference)
float voltage = sensorValue * (3.3 / 4095.0);
// Display in Serial Monitor
Serial.print("Voltage: ");
Serial.print(voltage);
Serial.print(" V | Analog: ");
Serial.println(sensorValue);
delay(500);
}
How to use the code:
- Open Arduino IDE
- Paste the code above
- Select correct ESP32 board and Port
- Upload the code
- Open Serial Monitor and set baud rate to 115200
Testing with Butane Gas
After uploading the code, open Serial Monitor. You will see voltage and analog values displayed.
Testing steps:
- Observe the baseline reading (normal value)
- Hold a lighter near the sensor and release the butane gas (do not ignite)
- Watch the voltage and analog values increase
- Remove the lighter and observe the values gradually returning to normal as the butane dissipates
Testing with Alcohol
Alcohol can also be used for testing, with some differences:
- Alcohol vapor is lighter than air and has no pressure to rise like butane from a lighter
- You need to position the sensor face-down toward the alcohol
- Results will be similar to butane testing
Factors Affecting Readings
Voltage and analog values change based on environmental conditions:
- Gas concentration - higher concentration produces higher readings
- Air movement - wind will cause readings to fluctuate
- Gas pressure - gases from a lighter (pressurized) produce clearer readings
- Ambient temperature - affects sensor performance
Practical Applications
The readings can be applied in various ways:
- Set a threshold to trigger alerts when values exceed the limit
- Send data via Wi-Fi for remote notifications
- Build a gas leak detection system
Example code for threshold detection:
const int gasSensorPin = 35;
const int threshold = 2000; // Adjust based on your environment
void setup() {
Serial.begin(115200);
analogReadResolution(12);
}
void loop() {
int sensorValue = analogRead(gasSensorPin);
if (sensorValue > threshold) {
Serial.println("Gas detected!");
// Add your alert or device activation code here
}
Serial.print("Analog: ");
Serial.println(sensorValue);
delay(500);
}
Points to adjust: The threshold value should be calibrated based on your actual environment. Start by observing the normal baseline value in regular air, then set the threshold about 20-30% above the baseline.
Summary
The MP-4 sensor is easy to use. With just 3 connections, you can read combustible gas detection via analog pins. Values change based on gas quantity and concentration. This can be extended into alert systems or gas leak detection systems as needed.
อยากทำโปรเจคแบบนี้?
รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน
If you need Arduino project service or urgent IoT development, see full service details on the home page
จ้างทำโปรเจคเลย