ESPHOME + MQ4 Help

Hi I wanted to know if it is possible to use the MQ4 sensor in the same way as the MQ2, and (if so), if the formula reported by OttoWinter “((5.0-x) / x) / 9.8” was fine for this sensor too. Thank you

1 Like

In principle there is no reason why it won’t work, but you’ll have to consult the mq4 datasheet to work out the formula.

They are pretty similar but still have some important differences in sensing so you’ll need to do some adjustments according at datasheet of each one !
Datasheets avalaible on that page if needed :wink: https://www.mysensors.org/build/gas

Hi, looking forward to your work.

This is an old thread but have you managed to to have MQ4 in ESPHome and if so can you share the config? Many thanks!

the PPM formula for MQ4 could be picked up from https://www.teachmemicro.com/mq-4-methane-gas-sensor-arduino/
if anyone was able to get this working in ESPHome please share your thoughts! :slight_smile:

Hey Adrian,
Were you able to figure in out in the meantime? I also have a MQ4 connected to an ESP32, and I get the voltage. And now with just some simple math I can convert into PPM CH4. But in yaml the sensor block I cannot get the log and the raising to the power of 10 done. Do you know how to do that?
– Paul

Hi Paul,
In Home assistant you just get a value… I just made a rule if is over 40 then it detect gas so it turns off the gas at meter + notification… It works but I had some false positives …
If you have figured out the math maybe you can share the formula and we can do a template sensor in HA rather than in ESPhome…

Regards,
Adrian

Hi Adrian,
I’ll give it a spin. I’ll let you know if I succeeded.

– Paul

Hi Adrian,

I did a bit of math. The difficulty is that the scales are both log. So you need to determine the slope of the line in the log/log scale. If you work it out you can program it as follows:

  - platform: adc
    pin: GPIO36
    name: "MQ4 CH4 sensor"
    accuracy_decimals: 0
    id: MQ4_CH4
    update_interval: 30s
    filters: 
     - lambda: return (pow(10, log10((0.12/x)*(5-x)/(5-0.12))/(-0.35))) ;
    unit_of_measurement: "ppm"

But is all fairness, your suggestion of having a limit works out to do the same. Just that in this case you get an approximate PPM number.

– Paul