MH-Z19B problem!

Good morning everyone!

Recently, I´ve been dealing with a problem that I cannot work out.

I’m using a WIFI LoRa 32 V2.1 alongside a Co2 sensor, specifically the MH-Z19B.

This sensor use is connected to the ESP32 through UART via Serial Port.

I’ve been using this sketch but it doesn´t work. All the co2 values that I get are 0, always :frowning:

#include <Arduino.h>
#include “MHZ19.h”
#include <SoftwareSerial.h> // Remove if using HardwareSerial
#include <Wire.h>

#define RX_PIN 4 // Rx pin which the MHZ19 Tx pin is attached to (SCL)
#define TX_PIN 15 // Tx pin which the MHZ19 Rx pin is attached to (SDA)
#define BAUDRATE 9600 // Device to MH-Z19 Serial baudrate (should not be changed)

MHZ19 myMHZ19; // Constructor for library
SoftwareSerial mySerial(RX_PIN, TX_PIN);

unsigned long getDataTimer = 0;

void setup()

Wire.begin(4, 15);




void loop()
if (millis() - getDataTimer >= 5000)
int CO2;

    CO2 = myMHZ19.getCO2();                             
    Serial.print("CO2 (ppm): ");                      

    int8_t Temp;
    Temp = myMHZ19.getTemperature();                     
    Serial.print("Temperature (C): ");                  

    getDataTimer = millis();


I tried to modified each parameter, changing the RX and TX pins, trying with different cables and even using another sensor but I don’t know which is the issue.

Besides, I used a ESP8266 with the MH-Z19B and it works perfectly. Therefore, I assume that is an issue related to the WIFI LoRa 32 V2.1.

It might be a matter of incompatibility with some pin. I don’t really know…

Has anyone used this sensor with WIFI LoRa 32 V2.1 and did it work?

I am open to any suggestions.

Thak you very much in advance.

i have same problem with 1/2aa lora node, and it can be beause of power problems, your co2 sensor can only work from 3.6v maybe you use 3.3 pin or something that gives you les voltage

1 Like

But if you look the satasheet, it says that it works from 4.5 to 5.5V…

1 Like

i took my data from online shop in my contry, so its not so true, but i use MH-Z16, and datasheet says 4.5~5.5, so like yours, but it works 3.0 and more, so i think the accuracy might be not so good, but it works, and i recomend you to try use external battery just for power the sensor, to check the problem is in voltag or no, exactly now im building fire sensor, and cant solve power problems

I’ve tried with 3’3V as well as 5V and I always get the same value: 0

1 Like

check 5v pin by voltmeter, and you connect the esp by usb or use battery?

I checked 5V with a voltmeter and it’s okay. So I believe it isn`t a matter of voltage.

I connect the esp via USB

ok, so i dont have any ideas, sorry for useless info

No worries, thank you anyway for helping me!

I appreciate indeed!

Are you sure 1115200 instead of 115200? I’ve never seen 1115200.

OMG haha i’m sorry, it was just a mistake while writing. In my code it is written properly


Well, after spendig hours and hours in front of my computer… EUREKA!

I found the right library and sketch for me!

If anyone want to get the code, just let me know.

Thanks everyone!