MH-Z14 / MH-Z19 CO2 sensor

Moderators: grovkillen, Stuntteam, TD-er

Message
Author
User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: MH-Z14 / MH-Z19 CO2 sensor

#41 Post by grovkillen » 28 Mar 2018, 20:22

I removed the warning on the wiki... haven't looked into that until you noticed it.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

s3030150
Normal user
Posts: 20
Joined: 29 Nov 2017, 14:27

Re: MH-Z14 / MH-Z19 CO2 sensor

#42 Post by s3030150 » 28 Mar 2018, 20:27

yeah so try calibrating it then.

you can use my test sketch which I put together from various sources around github. Disables ABC and performs calibration after 30 minutes, then it starts reading from serial.

Not sure if you should disable ABC at every start of your program or it remains disabled forever or until you enable it again. can somebody answer?

Code: Select all

#include <SoftwareSerial.h>
#include <SPI.h>
#include <Wire.h>


#define INTERVAL 5000
#define MH_Z19_RX D5 //RX
#define MH_Z19_TX D7 //TX



byte mhzResp[9];    // 9 bytes bytes response
byte mhzCmdReadPPM[9] = {0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79};
byte mhzCmdCalibrateZero[9] = {0xFF,0x01,0x87,0x00,0x00,0x00,0x00,0x00,0x78};
byte mhzCmdABCEnable[9] = {0xFF,0x01,0x79,0xA0,0x00,0x00,0x00,0x00,0xE6};
byte mhzCmdABCDisable[9] = {0xFF,0x01,0x79,0x00,0x00,0x00,0x00,0x00,0x86};
byte mhzCmdReset[9] = {0xFF,0x01,0x8d,0x00,0x00,0x00,0x00,0x00,0x72};
byte mhzCmdMeasurementRange1000[9] = {0xFF,0x01,0x99,0x00,0x00,0x00,0x03,0xE8,0x7B};
byte mhzCmdMeasurementRange2000[9] = {0xFF,0x01,0x99,0x00,0x00,0x00,0x07,0xD0,0x8F};
byte mhzCmdMeasurementRange3000[9] = {0xFF,0x01,0x99,0x00,0x00,0x00,0x0B,0xB8,0xA3};
byte mhzCmdMeasurementRange5000[9] = {0xFF,0x01,0x99,0x00,0x00,0x00,0x13,0x88,0xCB};

int shifts=0,co2ppm;

long previousMillis = 0;

SoftwareSerial co2Serial(MH_Z19_RX, MH_Z19_TX); // define MH-Z19

byte checksum(byte response[9]){
  byte crc = 0;
  for (int i = 1; i < 8; i++) {
    crc += response[i];
  }
  crc = 255 - crc + 1;
  return crc;
}


void disableABC() {
 co2Serial.write(mhzCmdABCDisable, 9);
}

void enableABC() {
 co2Serial.write(mhzCmdABCEnable, 9);
}

void setRange5000() {
 co2Serial.write(mhzCmdMeasurementRange5000, 9);
}

void calibrateZero(){
 co2Serial.write(mhzCmdCalibrateZero, 9);
}

int readCO2() {
  byte cmd[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};
  byte response[9];
  co2Serial.write(cmd, 9);
  // The serial stream can get out of sync. The response starts with 0xff, try to resync.
  while (co2Serial.available() > 0 && (unsigned char)co2Serial.peek() != 0xFF) {
    co2Serial.read();
    shifts++;
  }

  memset(response, 0, 9);
  co2Serial.readBytes(response, 9);

for (int i=0; i<9; i++) {
  Serial.print(" 0x");
  Serial.print(response[i], HEX);
}
Serial.println(" Response OK. Shifts="+String(shifts));


  if (response[1] != 0x86)
  {
    Serial.println(" Invalid response from co2 sensor!");
    return -1;
  }



  if (response[8] == checksum(response)) {
    int responseHigh = (int) response[2];
    int responseLow = (int) response[3];
    int ppm = (256 * responseHigh) + responseLow;
    return ppm;
  } else {
    Serial.println("CRC error!");
    return -1;
  }
}

void setup() {
  Serial.begin(115200);
  unsigned long previousMillis = millis();
  co2Serial.begin(9600); //Init sensor MH-Z19(14)
  delay(500);
  disableABC();
  //setRange5000();
  delay(1800000);
  calibrateZero();
  Serial.println("Zero was calibrated");
}

void loop() {
  unsigned long currentMillis = millis();
  if (abs(currentMillis - previousMillis) > INTERVAL)
  {
          previousMillis = currentMillis;
          Serial.print("Requesting CO2 concentration...");
          co2ppm=-999;
          co2ppm = readCO2();
          Serial.println("  PPM = " + String(co2ppm));
          //if(calibrated) { Serial.println("Zero was calibrated."); }
  }
}

Barb232
Normal user
Posts: 40
Joined: 12 Mar 2016, 16:40
Location: Earth
Contact:

Re: MH-Z14 / MH-Z19 CO2 sensor

#43 Post by Barb232 » 28 Mar 2018, 20:31

i will do this tomorrow, thank you

TD-er
Core team member
Posts: 8752
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: MH-Z14 / MH-Z19 CO2 sensor

#44 Post by TD-er » 28 Mar 2018, 20:45

Just be very careful with the calibration commands.
The 400ppm calibration should be done in the same environment (temp/humidity) as the room it is being operated in.
And make sure you do not run it more than once per minute or so.
I've ruined one of my sensors by running it too fast in succession and that sensor is totally useless now since the 0 ppm level is now above 400 ppm.

Just have a thorough look at the code of the plugin of ESPeasy, or the same code, but more compact, in Tasmota.

s3030150
Normal user
Posts: 20
Joined: 29 Nov 2017, 14:27

Re: MH-Z14 / MH-Z19 CO2 sensor

#45 Post by s3030150 » 28 Mar 2018, 20:47

the manufacturer says the sensor should be calibrated by calling calibration at least 20 minutes after operating the sensor in outdoor air, as far as I understood. why to calibrate it indoors?

TD-er
Core team member
Posts: 8752
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: MH-Z14 / MH-Z19 CO2 sensor

#46 Post by TD-er » 28 Mar 2018, 20:53

s3030150 wrote: 28 Mar 2018, 20:47 the manufacturer says the sensor should be calibrated by calling calibration at least 20 minutes after operating the sensor in outdoor air, as far as I understood. why to calibrate it indoors?
The thing is that (at least here in Holland) the air outside is much colder and therefore higher relative humidity.
What they mean is that you should perform calibration in about 400 ppm CO2 concentration.
That's outside, when you're not among lots of people or in the city.
When you need to do recalibration, it is probably because the sensor did not experience well ventilated air in the last few days and then it is quite useless to do the calibration in the same room.

But you have to take into account the temperature and humidity and make sure no IR light enters the sensor (sunlight)

s3030150
Normal user
Posts: 20
Joined: 29 Nov 2017, 14:27

Re: MH-Z14 / MH-Z19 CO2 sensor

#47 Post by s3030150 » 28 Mar 2018, 21:05

That makes sense, thanks

Barb232
Normal user
Posts: 40
Joined: 12 Mar 2016, 16:40
Location: Earth
Contact:

Re: MH-Z14 / MH-Z19 CO2 sensor

#48 Post by Barb232 » 29 Mar 2018, 09:02

Finally, it works.

i downloaded the document
http://www.winsen-sensor.com/d/files/in ... ver1_0.pdf

And i tried the method 8a (manual calibration)
I soldered a button between GND and HD and pressed the button for 7 seconds
waited 20 min with open window
Results: values 395 ppm
Lets wait and see
photo follows
using espeasy

Code: Select all

44042105 : MHZ19: PPM value: 395 Temp/S/U values: 21/0/0.00
44042105 : EVENT: co2sensor#PPM=395.00
44042107 : EVENT: co2sensor#Temperature=21.00
44042108 : EVENT: co2sensor#U=0.00
what is the meaning of U?

a few minutes later ...

Code: Select all

Log
285758 : EVENT: co2sensor#Temperature=21.00
285759 : EVENT: co2sensor#U=0.00
296021 : MHZ19: PPM value: 588 Temp/S/U values: 21/0/0.00
296022 : EVENT: co2sensor#PPM=588.00
296046 : EVENT: co2sensor#Temperature=21.00
296047 : EVENT: co2sensor#U=0.00
301398 : WD : Uptime 5 ConnectFailures 0 FreeMem 18664
306850 : MHZ19: PPM value: 540 Temp/S/U values: 21/0/0.00
306851 : EVENT: co2sensor#PPM=540.00
306875 : EVENT: co2sensor#Temperature=21.00
306875 : EVENT: co2sensor#U=0.00
316871 : MHZ19: PPM value: 497 Temp/S/U values: 21/0/0.00
316871 : EVENT: co2sensor#PPM=497.00
316895 : EVENT: co2sensor#Temperature=21.00
316896 : EVENT: co2sensor#U=0.00
327697 : MHZ19: PPM value: 459 Temp/S/U values: 21/0/0.00
327698 : EVENT: co2sensor#PPM=459.00
327725 : EVENT: co2sensor#Temperature=21.00
327726 : EVENT: co2sensor#U=0.00
331398 : WD : Uptime 6 ConnectFailures 0 FreeMem 18664
btw: i want to make a traffic light

red: ?
yellow: ?
green: ?

and how can i do that with a gpio LED rgb? rules

what value-ranges should i use?


/robin

s3030150
Normal user
Posts: 20
Joined: 29 Nov 2017, 14:27

Re: MH-Z14 / MH-Z19 CO2 sensor

#49 Post by s3030150 » 29 Mar 2018, 10:26

Not able to help you with espeasy since I'm not using it, but you need to keep the window open for half an hour and after that calibrate, otherwise you will calibrate to wrong value

Barb232
Normal user
Posts: 40
Joined: 12 Mar 2016, 16:40
Location: Earth
Contact:

Re: MH-Z14 / MH-Z19 CO2 sensor

#50 Post by Barb232 » 29 Mar 2018, 10:28

yes, i did this

TD-er
Core team member
Posts: 8752
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: MH-Z14 / MH-Z19 CO2 sensor

#51 Post by TD-er » 29 Mar 2018, 19:33

I would say 400 - 700 ppm = green
700 - 1400 = orange
1400+ = red.

Barb232
Normal user
Posts: 40
Joined: 12 Mar 2016, 16:40
Location: Earth
Contact:

Re: MH-Z14 / MH-Z19 CO2 sensor

#52 Post by Barb232 » 29 Mar 2018, 19:35

thats are my practical results, too.
Thank you

I wrote a short blog Post in my Blog, works fine.

https://blog.moneybag.de/fhem-luftquali ... asy-wemos/
Screenshot 2018-03-29 21.22.53.JPG
Screenshot 2018-03-29 21.22.53.JPG (38.1 KiB) Viewed 17456 times

Erik82
New user
Posts: 2
Joined: 16 Apr 2018, 14:12

Re: MH-Z14 / MH-Z19 CO2 sensor

#53 Post by Erik82 » 16 Apr 2018, 14:24

I recently received my MH-Z14a sensor, I want to use it to send CO2 values to my home automation system via MQTT.

When I got it, I first hooked it up to a arduino nano and the PWM example form this site: http://domoticx.com/arduino-co2-sensor-mh-z14/
It seemed to work, The red light was flashing every few seconds, and I got some plausible values.

I now want to do the same with on the ESP8266 ESP-12. When I use the same sketch, I got some odd values and the red light was not flashing. I thing the problem is that my ESP circuit is working on 3.3v, while the sensor requires 5v.
To overcome that problem, I tried using one of these as power supply for the sensor: http://www.mpja.com/images/30175-large.jpg , while keeping the 3.3v power supply for the ESP. Unfortunately the result stayed the same.

I then tried switching to UART instead of PWM, and used a simplified versiopn of this sketch to test it: https://github.com/bertrik/mhz19/blob/m ... 2meter.ino
Unfortunately, it seems that the command bool result = exchange_command(0x86, data, 3000); in that sketch always returns 0 for me.
Any idea where I'm going wrong?

TD-er
Core team member
Posts: 8752
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: MH-Z14 / MH-Z19 CO2 sensor

#54 Post by TD-er » 16 Apr 2018, 23:14

Are you sure you set TX on the ESP to RX on the sensor and vice verse?
You could also try this sketch: (section with the UART implementation)
http://domoticx.com/arduino-co2-sensor-mh-z14/

Erik82
New user
Posts: 2
Joined: 16 Apr 2018, 14:12

Re: MH-Z14 / MH-Z19 CO2 sensor

#55 Post by Erik82 » 21 Apr 2018, 17:44

Thanks, it seems like that was actually the case, now that I switched the RX and TX wires, I am able to get some plausible output from the sensor.
Does anyone know by the way what type of the 7 pin connector is? it seems to be as JST connector, but not with the standard 2.54 pitch spacing.

Edit: Figured out that it's most likey a JST-SH connector with 1.0mm spacing.

I did get my MH-Z14a working with the calibration sample from s3030150. I combined it with some code to send the values with a ESP8266 over MQTT (I'm sending it to home assistant)
If anyone is interested: https://github.com/eriknl1982/esp8266_mh-z14a-mqtt

The only issue I'm now facing is that after a while (sometimes a few hours, sometimes a day) the sensor starts returning 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 as response. The only way to get back to normal values is by powering the sensor down and up again.
anyone else experiencing this?

sincze
Normal user
Posts: 43
Joined: 15 Jul 2016, 12:54

Re: MH-Z14 / MH-Z19 CO2 sensor

#56 Post by sincze » 18 Mar 2019, 20:09

Barb232 wrote: 29 Mar 2018, 09:02 Finally, it works.

i downloaded the document
http://www.winsen-sensor.com/d/files/in ... ver1_0.pdf

And i tried the method 8a (manual calibration)
I soldered a button between GND and HD and pressed the button for 7 seconds
waited 20 min with open window
Results: values 395 ppm
Lets wait and see
photo follows
using espeasy
.....

and how can i do that with a gpio LED rgb? rules

what value-ranges should i use?

/robin
You guy saved my life. It is now working.
I used a dupont cable to connect the 2 pins together for 7 seconds. :lol:
esp-co2-v2.JPG
esp-co2-v2.JPG (17.22 KiB) Viewed 14660 times

Post Reply

Who is online

Users browsing this forum: No registered users and 67 guests