MH-Z14A CO2 sensor

  • Hello,

    Anyone has got a modified MH-Z14A CO2 sensor working please?
    Whenever I tried to use sub-type V_UNIT_PREFIX, I get the following error (My Sensors 1.5.4)

    MySensors: Unknown/Invalid sensor type (43)

    Additionally, I cannot get proper readings from PWM. This is the description (in Chinese - sorry)~红外传感器说明书V1.0.pdf

    I am using the following PINS: Pin23 - VIN, Pin 22 - GND, Pin26 - PWM.

    Any help is appreciated

  • OK, I manage to get it resolved now. Simple code really - I am going to convert it into MySensors now. One thing to note - there is an error in the formula. This is 5000ppm sensor, therefore "5000" multiplier has to be used in the formula.

    // set pin number:
    const int sensorPin = 2;     // the number of the sensor pin
    long ppm=0;
    unsigned long duration;
    void setup() {
      // initialize the sensor pin as an input
      pinMode(sensorPin, INPUT);
    void loop(){
      duration = pulseIn(sensorPin,HIGH,2000000);
      ppm = 5000 * (((duration)/1000)-2)/1000; 
        Serial.println("CO2 = " + String(ppm) + " ppm");

  • Hero Member

    @alexsh1 said:

    I am going to convert it into MySensors now

    you have it here:

  • @epierre I took your sketch and modified it. The problem is that the formula for CO2 is wrong for 5000ppm sensors. Additionally, I was getting the error in your sketch - MySensors: Unknown/Invalid sensor type (43). Not sure why.

    Anyway, my modification:

    #include <MySensor.h>  
    #include <SPI.h>
    #define CHILD_ID 0
    #define CO2_SENSOR_PWM_PIN 2
    unsigned long SLEEP_TIME = 30*1000; // Sleep time between reads (in milliseconds)
    float valAIQ =0.0;
    float lastAIQ =0.0;
    unsigned long duration;
    long ppm;
    MySensor gw;
    MyMessage msg(CHILD_ID, V_LEVEL);
    void setup()  
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("AIQ Sensor CO2 MH-Z14A", "1.0");
      // Register all sensors to gateway (they will be created as child devices)
      gw.present(CHILD_ID, S_AIR_QUALITY);  
      pinMode(CO2_SENSOR_PWM_PIN, INPUT);
    void loop() { 
      while(digitalRead(CO2_SENSOR_PWM_PIN) == HIGH) {;}
      //wait for the pin to go HIGH and measure HIGH time
      duration = pulseIn(CO2_SENSOR_PWM_PIN, HIGH, 2000000);
      ppm = 5000 * ((duration/1000) - 2)/1000;
      if ((ppm != lastAIQ)&&(abs(ppm-lastAIQ)>=10)) {
          lastAIQ = ceil(ppm);
      gw.sleep(SLEEP_TIME); //sleep for: sleepTime

  • Hero Member

    @alexsh1 sorry I don't read chinese, but my datasheet says 2000:

    are you using an alternate wiring or alternate version (the A at the end for enhanced or revised ?

  • @epierre Which sensor do you have? 2000ppm or 5000ppm? For 2000ppm you should use 2000 multiplier. I have 5000ppm, but the formula still gives me 2000 multiplier. Using 5000 multiplier seems to give a correct result.

    I have MH-Z14A 5000ppm. No idea what "A" stands for - there is very little documentation in English.

  • Hero Member

    @alexsh1 so mine is CO-MHZ14, no wiring on the 20+ pins, written on it 0~5000ppm

  • @epierre In this case you should use 5000 multiplier - please see above my sketch.
    There is a mistake in the datasheet file - it has been written for 2000ppm model.

    Mine has got 20+ pins and it is 0-5000ppm

  • hi
    this looks quite nice stuff, but how should i wire up the sensor to wemos d1 for example?
    do i have to use the TTL adapter mode on the board by shorting some pins or do i just need to give some power to the sensor and hook the PWM pin to the wemos board somehow?

    and how does this code comapre to these codes?

    or should i just get a usb/ttl adapter and use it over python then?

  • well, i got this far that i just soldered some connectors to pins 16-19 and now want to use the wemos board as USB to TTL adapter, so i could use the Co2 logger over python server, but how do i need to connect the Rx and Tx pins?

    does it have to be like a crossover cable or just normal straight?
    like Sensor Rx to Wemos Tx + S-Tx to W-Rx or just the regular connection like Rx to Rx and Tx to Tx?

    lol.... the 16-19 pins maybe doesnt have this somekind of 3.3v query pin and only has 5V input? or Rx&Tx run at 3,3 volts?

  • lol.... so i thought that maybe i could run it like this:
    but what is the (T1)PD5/IO5 pin on the wemos board ? D3 or D4? maybe D3 as GPIO0 then???:-S
    from here:

  • ok, so i got it working from the last link, but wonder what does this code do?
    CO2= int(2000*(duration_h-2)/(duration_h+(1004-duration_h)-4));

    and i actually changed it to this:
    (removed the space before "int" too)
    and this code somehow has "-4" not "-2" as in this forum topic?

    and it probably calibrates itself on the 1st startup hour and also does this every 24 hours?
    so wonder if i need to place the sensor in a relatively clean air(on a window?) or it doesnt really matter? and it is of course a bit colder near the window because i also opened it a bit to let more clean air in.......
    and when i first plugged it in, it started to show something like 495ppm, but then i unplugged it and took it to the window(in another room), and it then started from 1375ppm and has now been dropped down to 900....
    so i wonder why did it first show 495, but then 1375 suddenly.....

  • nice...
    i got this reset stack now:

    CO2 level: -10 ppm

    Soft WDT reset

    ctx: cont
    sp: 3ffef1f0 end: 3ffef3d0 offset: 01b0

    3ffef3a0: 00000000 00000000 00000001 40202281
    3ffef3b0: 3fffdad0 00000000 3ffee39c 402022ac
    3ffef3c0: feefeffe feefeffe 3ffee3b0 40100114
    ?⸮⸮⸮⸮DH⸮⸮⸮CO2 level: 395 ppm
    CO2 level: 395 ppm
    CO2 level: -10 ppm
    CO2 level: 405 ppm
    CO2 level: 405 ppm
    CO2 level: 1070 ppm
    CO2 level: 1075 ppm
    CO2 level: 1080 ppm

    atleast now i know that it started the calibration from the start again, but what could it all mean?
    and its probably best to use good quality cables too then... and perhaps good to not to touch the dupont cables too...

Log in to reply

Suggested Topics

  • 87
  • 5
  • 1
  • 2
  • 3
  • 8