I got MH-Z19C (CO2 ppm) (datasheet), and DHT11 (temperature) sensors from Ebay and followed instructions online to set it up with my ESP32 board (ESP32).

I'm using a breadboard and 5V/3.3V power supply to power the sensors, while the ESP32 is linked to my PC with USB.

I'm struggling to understand the reason why I can't get a reading from my sensors. I tried countless guides online and double-checked everything multiple times.

I also tried connecting DHT11 to my board's 3.3V directly but it didn't change anything.

How do you "debug" hardware? Is there no way to know what you're doing wrong? You have to get it exactly right or nothing works?

Serial monitor logs:

0 ppm
Failed to read from DHT sensor

MH-Z19C connections:

  • VIN/GND <-> Power Supply 5V +/- respectively
  • TX <-> ESP32 RX2
  • RX <-> ESP32 TX2

DHT11 connections:

  • Pin 1 <-> Power supply 3.3V
  • Pin 2 <-> 10k Ohm pull-up <-> ESP32 D2
  • Pin 4 <-> GND


Libraries installed:

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

// REQUIRES the following Arduino libraries:
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor

#include "DHT.h"
#include <SoftwareSerial.h>
#include <MHZ19.h>
#include <Wire.h>

// pin for uart reading
#define RX_PIN 16  // Rx pin which the MHZ19 Tx pin is attached to.
#define TX_PIN 17 // Tx pin which the MHZ19 Rx pin is attached to.

#define DHTPIN 2     // Digital pin connected to the DHT sensor
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
// Pin 15 can work but DHT must be disconnected during program upload.

#define DHTTYPE DHT11   // DHT 11

// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors.  This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
MHZ19 myMHZ19;
SoftwareSerial mySerial(RX_PIN, TX_PIN);

void setup() {

  // Start Co2 sensor
  mySerial.begin(9600);                               // (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))

void read_dht() { 
  /////// DHT SENSOR \\\\\\
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print(F("Humidity: "));
  Serial.print(F("%  Temperature: "));
  Serial.print(F("C "));
  Serial.print(F("F  Heat index: "));
  Serial.print(F("C "));


void loop() {
  // Wait a few seconds between measurements.

  int CO2;
  // Request CO2 (as ppm)
  CO2 = myMHZ19.getCO2();
  Serial.println(String(CO2) + " ppm");


MH-Z19C from Ebay MH-Z19C pins DHT11 pins dht11 wiring

  • 1
    test only one sensor at a time ... use the example code in the library
    – jsotola
    Feb 26, 2023 at 19:37
  • I did try one sensor at a time. I'll have a look at the library example. Feb 26, 2023 at 19:54
  • You don't need to use SoftwareSerial for the MH-Z19. You can use the Serial2 second builtin hardware serial port.
    – StarCat
    Feb 28, 2023 at 7:21
  • There's no dht.begin() in your setup() so your DHT11 never properly initializes. Look at some example code, like here: https://github.com/adafruit/DHT-sensor-library/blob/master/examples/DHTtester/DHTtester.ino.
    – StarCat
    Feb 28, 2023 at 7:29
  • Actually I am using dht.begin() currently but when copying/editing the code to SO, it got lost. I also tried with HardwareSerial without luck unfortunately. Feb 28, 2023 at 22:26

1 Answer 1


You did a good job of giving links. You could have improved your question by showing, not just telling us, how you connected the sensors.

How do you "debug" hardware? Is there no way to know what you're doing wrong? You have to get it exactly right or nothing works?

No, that is too hard. I like to divide and conquer and test sequentially.

As already mentioned, test one sensor at a time.

To illustrate, I broke out an old ESP32 and a DHT22 and went about hooking them up as per your example.

Here are the connections:

enter image description here

enter image description here

Then I dowload the library that you had mentioned and I load their simplest example, DHTtester. If I am relying heavily on Arduino Libraries, I am relying on someone else's code, so I need to look through it so I am sure I am using it correctly.

Looking through the code, I make sure it specifies:

#define DHTPIN 2     // Digital pin connected to the DHT sensor.
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

and I double check that I have it connected correctly . As you can see, I have the pull-up resistor on the dataline....+3.3v to one side of the resistor and the other side goes to the DHT data pin and to the ESP GPIO2 pin (see the pics?).

I double check and then run the code and the serial monitor reveals...

Humidity: 32.40%  Temperature: 23.40°C 74.12°F  Heat index: 22.64°C 72.75°F
Humidity: 32.70%  Temperature: 23.30°C 73.94°F  Heat index: 22.54°C 72.57°F
Humidity: 33.30%  Temperature: 23.30°C 73.94°F  Heat index: 22.55°C 72.60°F
Humidity: 34.10%  Temperature: 23.30°C 73.94°F  Heat index: 22.58°C 72.64°F
Humidity: 34.30%  Temperature: 23.30°C 73.94°F  Heat index: 22.58°C 72.65°F

...and if I breathe on the sensor, the RH jumps up....looks good.

If it had not have worked AND I was sure I had connected things correctly, I would have started going backwards....Can I get any program to work on the ESP, like a simple blink, there are lots of configuration options and it is easy to mess it up.

If I can get something to run, then I have to start to suspect the DHT - do I have another to test?...and so on and so forth.

Then I would repeat the same process with the CO2 sensor.

Then I would try to use them both at the same time.

Hope it helps.

  • So I basically replicated your setup exactly as it is with DHT and the resistor linking PIN to 3.3V in the same way you did. I'm still getting Temperature 0.00C and Humidity 0.00% so it is getting a reading, not failing, but the reading is always 0, very strange. I know that because if I disconnect the wires, then I get "error when reading temperature", "error when reading humidity". also followed this example to get info on sensors and it prints the driver version and range ok. Feb 27, 2023 at 4:45
  • I'll post a picture with the wirings tomorrow. Feb 27, 2023 at 4:46
  • Ok I added a photo. Feb 27, 2023 at 17:02
  • I wonder if your dht11 got damaged earlier. If you had a known good one, it would be very helpful.
    – DrG
    Feb 27, 2023 at 22:40
  • Not to my knowledge. I never used it before. To be honest, I'm more interested in the MH-Z19C sensor. I did some troubleshooting by following this "error codes" example from the library and it seems like the error code is 3 which stands for "Received data does not match the usual syntax expected". Feb 27, 2023 at 22:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.