MH-Z19C CO2 sensor wrong reading

I have connected up one of the MH-Z19C co2 sensors to an esp8266 nodemcu and I am getting bad data from the co2 sensor. 5000ppm which is the max value it can read. The temp value of the sensor is correct however:

This is the config:

esphome:
  name: living_room
  platform: ESP8266
  board: nodemcuv2

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_pw

captive_portal:

# Enable logging
logger:
  hardware_uart: UART1

# Enable Home Assistant API
api:

ota:

uart:
  rx_pin: GPIO3
  tx_pin: GPIO1
  baud_rate: 9600

sensor:
  - platform: mhz19
    co2:
      name: "MH-Z19 CO2 Value"
    temperature:
      name: "MH-Z19 Temperature"
    update_interval: 10s
    automatic_baseline_calibration: true

Can anyone who might have hooked one of these up assist me in setting it up correctly?
The red led light on the sensor itself is flashing on and off (not sure what this means?).
Also, the little white fabric over that light, does it need to be removed? It came with 0 instructions.

Hi

I have one of MH-Z19C. You should NOT Remote the white filter.

I checken my logs
I get:


[21:05:33][D][mhz19:046]: MHZ19 Received CO₂=468ppm Temperature=20°C Status=0x40

I see that i get status 0x40 and you get 0x0.

So maybe something is wrong. Maybe you could try to disconnect or cross them tx or rx cable and see if the status change.

When I connect it to my esp I did the classic mistake to connect the pin defined rx to rx on the mh-z19 and tx to tx. When they should be crossed rx to tx .

Mine doesn’t have a led so I cannot help there.
I t have automatic calibration. So if you leave it on for a time maybe it will fix it self.

Sorry that I don’t have so much solution to offer. But I hope you figure it out.

/Mattias

1 Like

So I have the same version as the guy in this video, the one with 7 cables https://www.youtube.com/watch?v=Gp-5t1zlnuw
Which is where I got my pin layout from (for some reason I could not find anything on the 19C version without the pins)

Connected it like so:
Red -> 3.3v
Black -> GND
Blue -> TX
Green -> RX

He has blue/green cables swapped in his video but that just gives me an error which goes away if I swap them around (this could be what you mentioned above rx to tx, tx to rx)
Could be it just takes time but seems like maybe my cabling is not correct or maybe my yaml.
Can you please post your yaml config for this sensor so that I can compare?
Thanks

Hi

I see that you are using the pins marked with tx and rx. I know that they maybe used by the esp itself. I suggest to try to use the pin GPIO 4 and GPIO5 instead (Usually d1 and d2). The esphome will use the softserial instead. Read that the esp8266 always uses soft serial. More reason to use more safe pins like the gpio4 and gpio5.
That the only thing that I could come up with.

/Mattias

My config below.
Note that it really old I have not updated mine to the latest version. It keeps going.

esphomeyaml:
  name: co2sensor
  platform: ESP8266
  board: d1

wifi:
  ssid: *****
  password: *******

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: '********'

ota:
  password: '*********'

uart:
  rx_pin: 4
  tx_pin: 5
  baud_rate: 9600

sensor:
  - platform: mhz19
    co2:
      name: "MH-Z19 CO2 Value"
    temperature:
      name: "MH-Z19 Temperature"
    update_interval: 60s```

Thanks and yes I have tried those pins also but I am using rx/tx just fine with an SGP30 and it works fine as long as I change the logger to UART1 otherwise it uses UART0 by default which creates a conflict.
Will try this again today just in case

Ok so I figured out something, if I don’t call this service

api:
  services:
    - service: mhz19_calibrate_zero
      then:
        - mhz19.calibrate_zero: my_mhz19_id

The value is always stuck at 5000 ppm
Once I do I start to see it, around 500 ish which is better but according to the documentation https://esphome.io/components/sensor/mhz19.html it says to leave it in a stable env for around 20 mins but still not sure on the setup of automatic_baseline_calibration should it be true or false?
How exactly is the calibration working?

Hi.

The calibration uses the fact that the carbon dioxide content in the air is around 400 ppm. So when it measures during the calibration on. It looks for the lowest point of the measurement and sets it to 400.
So the big question is should you go on automatic baseline calibration or not. If you do that it will recalibrate it self every 24 hours I think. That means that sometimes it get a lowest point that is not 400. Which affect the values for upcoming time until the next calibration.
So there is no straight answer. If you have a can of calibration gas. You could calibrate it with that and it will not need calibration for a long time. But If you dont have that. I guess that you have to try some and see if you get something that seems reasonable.

/Mattias

Looks like this does not suffer from drift after time (like the SGP30 sensor) so if I can calibrate it once correctly, will it keep the correct value indefinitely, will it reset on restart/power off?
Also, when I call the mhz19_calibrate_zero service, does it just set the current value as the zero value? Or does it read it for some time then picks one automatically?

I dont know about the restart/reboot. It should be pretty long term stable what I have understand.
I dont know in such detail about how the calibration works if it a one short low point or if it starts and pick the lowest part during a certain timeperiod.
This is the library that esphome uses.


There are some links to there to some pdf. But It not clear in detail how it works.

/Mattias

Hi ad_dracula,

have you figured out how to solve this issue?
I have exactly the same problem. Temperature is ok, but CO2 level is at 5000 ppm without change.
Changed pins, did some calibrations (I don’t know if the sensor really made a calibration) but no relsult.

Any new ideas?

Regards
Lars

Yes I did, check out my post here: ESPHome Temp Humidity LUX CO2 Motion Sensor :slight_smile:

1 Like

THX… I’ll try it!

I still have the problem of a co_2 value of 5000 ppm. What was your soution? I can’t see the different.

I copied your code. after starting I get the value of 500 ppm and after 10 seconds it changes to 5000 and is frozen on this value.
I have no ideas.

Give it some time and make sure your wiring is correct.
Also do the manual reset via esphome

ok, I’ll try. thx

hey, after 2 1/2 days I looked at my system and suddenly the MH-Z19 is giving me correct data!
You were right, just wait…

THX again

Lars

I got the same results:

Power connected to 3v pin, from 5v pin of nodemcu it can’t get any data, I don’t understand why it’s happens.

Maybe u r right, and I need to leave sensor for few days for get correct result… Sound strange.

And I got Satus=0x00 also.

Hi everyone !

I just want to meantion that you can manually calibrate the MH-Z19 sensor by connecting GND pin and HD pin for a minimun of 7 seconds in fresh air (outside). This will set the sensor to 400ppm but you must be in an enviroment were the air is around 400ppm (outside depending on were you live).

4 Likes

Is it possible to change sensor max value ?

the code is also suitable for another Winsen sensor, but the maximum value would need to be changed