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
Reference:
- https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide/
- https://www.electroniclinic.com/mh-z19b-ndir-co2-sensor-with-arduino-mhz19b/
- ESP32 Pinout
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.
DHT dht(DHTPIN, DHTTYPE);
MHZ19 myMHZ19;
SoftwareSerial mySerial(RX_PIN, TX_PIN);
void setup() {
Serial.begin(9600);
// 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!"));
return;
}
// 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(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("C "));
Serial.print(f);
Serial.print(F("F Heat index: "));
Serial.print(hic);
Serial.print(F("C "));
Serial.print(hif);
Serial.println(F("F"));
}
void loop() {
// Wait a few seconds between measurements.
delay(5000);
int CO2;
// Request CO2 (as ppm)
CO2 = myMHZ19.getCO2();
Serial.println(String(CO2) + " ppm");
read_dht();
}
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:
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.Adam Cherti– Adam Cherti2023年02月27日 04:45:33 +00:00Commented Feb 27, 2023 at 4:45
-
I'll post a picture with the wirings tomorrow.Adam Cherti– Adam Cherti2023年02月27日 04:46:08 +00:00Commented Feb 27, 2023 at 4:46
-
Ok I added a photo.Adam Cherti– Adam Cherti2023年02月27日 17:02:28 +00:00Commented 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– DrG2023年02月27日 22:40:38 +00:00Commented 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".Adam Cherti– Adam Cherti2023年02月27日 22:53:50 +00:00Commented Feb 27, 2023 at 22:53
dht.begin()
in yoursetup()
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.dht.begin()
currently but when copying/editing the code to SO, it got lost. I also tried withHardwareSerial
without luck unfortunately.