I’m working on a project to detect methane gas using an MQ-4 sensor and MQ-137 with an ESP32. I have an issue with reading the analog output. We don't have access to calibrate it using methane gas so we are testing it with alcohol, butane, and spoiled food.
Specifications:
- ESP32 5v
- MQ-4 Gas Sensor
- MQ-137 Gas Sensor
- 5v power supply
- RTC
- MicroSD
- DHT
Steps Taken:
- Pre heated mq4 for 24 hrs
- Verified wiring and connections.
- We are using a voltage regulator and the sensors voltage are 4.95 and 100 uF. It works on 5v +-1
- Calibrated the sensor in clean air.
- Tested with alcohol for confirmation.
Observations:
- Sensor reads 0 in clean air but it is unstable. There is a sudden spike and it drops down to 0.
- Once we tested it with a gas the analog read takes time to drop down to 0 even after removing the gas.
I already tested it using the code and formula for a simple calibration but there is an issue with stability. But i want to step back and make sure that my analog read sensor will read and stabilize to 0 on clean air. I already tested mq4 twice and this is the third one. I would buy again just to make sure.
Reference code and guide i used
Attachments:
Any help or advice is highly appreciated!
Edit 1:
I forgot to also mention that we are focusing on detecting methane that spoiled food produces.
using analog read:
#define gas_sensor 33
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(analogRead(33));
delay(1000);
}
using calibration:
Calculating RO
void setup() {
Serial.begin(9600); // Set baud rate
}
void loop() {
float sensor_volt;
float RS_air;
float R0;
float sensorValue = 0;
// Take 500 samples for averaging
for (int x = 0; x < 500; x++) {
sensorValue += analogRead(33); // Sum analog values of the sensor
}
sensorValue = sensorValue / 500.0;
// Convert the average sensor value to voltage
sensor_volt = sensorValue * (4.95 / 4095.0);
// Calculate sensor resistance in fresh air (RS_air)
RS_air = ((4.95 * 10.0) / sensor_volt) - 10.0;
// Calculate R0 (baseline resistance in clean air)
R0 = RS_air / 4.4;
// Print the R0 value to the serial monitor
Serial.print("R0 = ");
Serial.println(R0);
delay(1000);
}
Calculating ppm
#define gas_sensor 33 // Sensor pin (ESP32 GPIO pin)
float m = -0.318; // Slope (verify with MQ-4 datasheet)
float b = 1.133; // Y-Intercep t (verify with MQ-4 datasheet)
float R0 = 545.5; // Calibrated sensor resistance in clean air
void setup() {
Serial.begin(9600); // Start serial communication
}
void loop() {
float sensor_volt;
float RS_gas;
float ratio;
float sensorValue;
// Read sensor value from analog pin
sensorValue = analogRead(gas_sensor);
// Convert sensor value to voltage (using 4.99V supply)
sensor_volt = sensorValue * (4.95 / 4095.0);
// Calculate RS_gas (sensor resistance in the current environment)
RS_gas = ((4.95 * 10.0) / sensor_volt) - 10.0;
// Calculate the ratio of RS_gas to R0
ratio = RS_gas / R0;
// Calculate gas concentration in ppm (logarithmic scale)
double ppm_log = (log10(ratio) - b) / m;
double ppm = pow(10, ppm_log);
// Display ppm value on the Serial Monitor
Serial.print("Gas Concentration (ppm): ");
Serial.println(ppm);
Serial.print("Gas Concentration (analog read): ");
Serial.println(analogRead(33));
delay(1000);
}