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();
}
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.