ESP32 MH-Z19b CO2 Sensor

I have an ESP32 DEVKit V1 connected to an MH-Z19b CO2 sensor.

The sensor needs 4.5V to 5.5V so I connected it externally to USB.

The MHZ Pins are connected to the pins in the code, I connected Rx - Tx and Tx - Rx. I tried other Pins like Pins 41 and 40 or 25 and 27 as Rx and Tx pins on the board, nothing works. I always get the same output.

The Code I use is the following:

#include "MHZ19.h"                                        
#include "SoftwareSerial.h"                                    

 

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

MHZ19 myMHZ19;                                             // Constructor for library
SoftwareSerial mySerial(RX_PIN, TX_PIN);                   // create device to MH-Z19 serial


void setup()
{
    Serial.begin(115200);                                   // Datenrate serieller Monitor
   
    mySerial.begin(BAUDRATE);                               // (Uno example) device to MH-Z19 serial start   
    myMHZ19.begin(mySerial);                                // *Serial(Stream) refence must be passed to library begin(). 

    myMHZ19.autoCalibration();                              // Turn auto calibration ON (OFF autoCalibration(false))

}

unsigned long getDataTimer = 0;
    
void loop()
{

  if (millis() - getDataTimer >= 2000)
    {
  int  CO2 = myMHZ19.getCO2();                             // Request CO2 (as ppm)
  Serial.print("CO2 (ppm): ");                      
  Serial.print(CO2);                                
  int8_t Temp = myMHZ19.getTemperature();                 // Request Temperature (as Celsius)
  Serial.print("  Temperatur CO2Sens (°C): ");                  
  Serial.println(Temp);                               
  getDataTimer = millis();
    }
}

The Output i get is: "CO2 (ppm): 0 Temperatur CO2Sens (°C): -17" and it won't change. Do you have an idea where the mistake is?

Thanks a lot!

You have a big possibility you fried your ESP32, that is a 3V3 volt device and 5 volts tends to destroy them. You need level shifters to connect them together. Also posting an annotated schematic would help us understand your project better. Be sure to show all connections, power, ground, and power sources.

1 Like

if you not destroy ESP then you should be prepared to waiting 2-5 minutes while sensor heating up. my sensors show in this time: "500", then something between 400 and 600.

https://www.winsen-sensor.com/d/files/infrared-gas-sensor/mh-z19b-co2-ver1_0.pdf says:

Working voltage 4.5 ~ 5.5 V DC

Output signal
UART(TTL interface level 3.3V)

Hi, @ardualbino
Welcome to the forum.

Have you Googled;

MH-Z19b CO2 sensor esp32

Tom.. :grinning: :+1: :coffee: :australia:

Okay, didnt know this.

I did it like here. Just Power and ground are connected to USB and not to the ESP32. I also tried different Rx and Tx Pins on the ESP32 as mentioned above.

So its not a voltage problem, right? Output Voltage is ok fpr ESP32 and Input Voltage is 5V from external USB Cable.

Waited way more than 5 minutes, so thats not the problem.

are you sure Vin is direct connected to VUSB ?

Hi,
Do you have a DMM? (Digital MultiMeter)

Thanks... Tom.. :grinning: :+1: :coffee: :australia:

Yes, i have got one.

Sensor VIN is directly connected to USB VOUT and Senesor Ground directly to USB Gorund

It has to be either something with the code or the Rx Tx Pins on the sensor. I tested everything with arduino Uno on PWN Pin and Power by USB Cable and it worked fine.

I just dont want to use the arduino uno because i want to host a webserver where the Data is received.

you can test it now on PWM pin.
did you tested on UNO over UART?

Use it to measure the supply voltage GOING to the sensor module.
That is Vin to gnd.

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

Yeah, i tested Rx and Tx on arduino uno WITH USB as power supply to the sensor and it also worked perfectly fine.
Where can the mistake be?

Mistake cant be there. Everything works on arduino uno. I dont know why it doesnt work with ESP32...

Have you done this?

Measure the supply to the sensor when using the UNO and then when using the ESP32.
Simple test.

Thanks.. Tom.. :grinning: :+1: :coffee: :coffee: :coffee: :australia:

I Measured the Voltage on the sensor pins now. Its in bot cases (UNO and ESP32) around 5V.