MQ-4 Gas Sensor unstable

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

mq4 datasheet

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); 
}

Please post your code here using code tags in the forum toolbar.

Try another 24 h.

This archived Electronic Nose project is a great resource on the MQ series.

1 Like

Not in the same detector, typically, fyi. Don't forget to monitor oxygen while measuring natural gas in case there is something else in the atmosphere throwing the LEL measurement off, but that may be outside the scope of your project.

That's a feature, not a bug. They work by heating a metal filament that takes time to cool off, like a hot pan, if you will. That's what you're seeing there.

Did you try reading into a circular buffer?

Does you conversion formula for ADC readings into ppm or %LEL cause your readings to go outside of some min max range that's predefined in your code?
No way to get help unless we see your code.

Also, no access to a gas range/stove? In a well-ventilated environment and in the absence of equipment against which to calibrate, turning the gas on with no flame (obviously) for a brief period could allow you to guesstimate the %LEL coming out of the range.

Assuming that the range, when lit, produces an almost uniformly bluish flame at the burner with few orange wicks at the top of the flame tells you the gas regulator is clean and operating correctly.

You can then assume that the gas concentration at the level where the flame just self extinguishes at a predetermined gas setting is very approximately the %LEL which for natural gas is 50,000ppm or 5% gas concentration in normal room air.

Keep in mid; however, that in monitoring for Gas leak detection, you're not looking to get anywhere near the LEL before the atmosphere is considered untenable.
You aren't looking to measure the LEL; you're after the % of the LEL and that distinction is critical.

Your "alarm" on your detector, whatever you use such as a buzzer or lights or both, should alarm at 10% of the LEL for natural gas

LEL = 5% of room air by volume. 1% of 1,000,000 is 10,000, thus 5% of all the air is 50,000ppm. That's not the measurement you want to use. You need a massive fudge factor for things like faulty equipment or user error (almost always user error). That fudge factor (at least for firefighters in North America) is no go past 10% without full PPE and a charged hoseline at the ready, and only to mitigate the atmosphere through ventilation and then bail until the levels go down.

That's 10% of 5% of room air by volume, or 5000ppm.

It is not really necessary in most cases to test for the %UEL (15% or 150,000ppm) since gas detection isn't concerned with that where the presence of the target gas is known based on things like odor or cause (striking a buried gas line with an excavator, for example). The reason it's not too important in real world cases is that the gas concentration only needs to be lower than the lower explosive limit (eventually zero ppm) because the actions required to mitigate an LEL or a UEL environment are the same.

1 Like

I updated it. Thanks!

Hi, @shiva10
Welcome to the forum.

What is your Vin voltage and where is it coming from?
I cannot see your power supply for the DC-DC converter.

Tom.. :smiley: :+1: :coffee:

Does you conversion formula for ADC readings into ppm or %LEL cause your readings to go outside of some min max range that's predefined in your code?
No way to get help unless we see your code.

I edited and updated the code. I use ppm because I want to detect methane in spoiled food. We have this container setup that has the sensors inside. We put the spoiled food inside and we waited for 1-2 hrs and it has not produced any methane at all. But when we tested the sensor for butane and alcohol it works just fine. There is an unstable reading with or without food:

0
0
0
32
0
0
128
0
0
96

But when i use butane or alcohol. The sensors are somewhat reading it not going 0.

176
148
256
162
330
256

Apologies, I haven't done much research on the %LEL yet, as we've focused on working with ppm readings. However, I understand that both ppm and %LEL are ways to read gas concentration. Correct me if im wrong, but does an increase in ppm readings correlate with the LEL rising? Like they are proportional

We use the 5v pin of the esp32. The voltage regulator is set to 5v as well. But it is being divided across the sensors. So the voltage the sensors receive for mq4 is 4.95v.

We use this to power the esp32. We use a battery 18650, 3000 mah

power supply we use

battery

The PPM readings are meaningless in the absolute sense, unless the sensor is calibrated against one or more known gas mixtures with known PPM values for the measured gas.

At best with an uncalibrated sensor, changes in either the raw and PPM readings merely inform you that something in the gaseous environment has changed.

2 Likes

Where in your schematic is the power supply and battery?

Tom... :smiley: :+1: :coffee: :australia:

Does it apply to your project? Why are you detecting methane off spoiled food? The future application of your project, if any would determine whether or not you want to know if your spoiled food is creating an indoor explosive atmosphere.

Isn't ESP32 a 3.3V ADC? I could be wrong, maybe your boards are different from my ESP32. Did you read this?
https://docs.espressif.com/projects/esp-idf/en/v4.2/esp32/api-reference/peripherals/adc.html

Yes, proportional. %LEL is the percentage of lower explosive limit for some target flammable/explosive gas. Parts per million can be applied to any gas mixture, of course.
In the case of methane, the lower explosive limit (the minimum concentration in the air required to ignite) is 5% of the total air in the space, room or container. Since 1% of 1,000,000 is always 10,000, you can just multiply 10,000 * 5(%) to convert the %LEL to PPM.
Let's see how good my memory is...the %LEL for ammonia, by contrast is 28% (most people forget that, yep, it'll blow up) and for propane it's 2.6% .

So converting these to PPM would be 280,000ppm and 26,000ppm, respectively.
The more PPM of methane you have, the proportional increase approaching the LEL before which it's said to be too lean to ignite, through it's flammable range up to the %UEL (15% for methane) and beyond (above which the mixture of methane and room air is now too rich to burn, to eventually 1,000,000ppm.

LEL and UEL are used in Gas and any industry concerned with gas safety like the Fire Service. PPM is used across all industries, including the Gas and Fire Safety industries.
PPM is great for low level leak detection; %LEL is great for the whole container of room air, generally.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.