I'm trying to read CO2 data from a MH-Z19C sensor with an ESP32 board but I'm not getting anything. The error code from the library is 2 indicating a timeout (no data received).
Did I get the wiring wrong? I wonder if the sensor is fried ?
Library: MH-Z19 by Jonathan Dempsey
Connections:
- MH-Z19C RX to TX2 (17)
- MH-Z19C TX to RX2 (16)
- MH-Z19C VIN to Power Supply 5V
- MH-Z19C GND to Power Supply 3V
Code:
#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.
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 loop() {
// Wait a few seconds between measurements.
delay(5000);
int CO2;
// Request CO2 (as ppm)
CO2 = myMHZ19.getCO2();
Serial.println(String(CO2) + " ppm");
if (myMHZ19.errorCode != RESULT_OK) {
Serial.print("Failed to receive CO2 value: ");
Serial.println(myMHZ19.errorCode);
}
}
-
1It is too difficult to follow your wiring in those pictures. A schematic diagram would be much better. You have mentioned a 3.3v and a 5v supply. Do these share a common ground?6v6gt– 6v6gt02/28/2023 05:20:24Commented Feb 28, 2023 at 5:20
-
1It is unclear how the GND are connected between the boards. It will be better to have a schematic than those pictures.hcheung– hcheung02/28/2023 05:20:48Commented Feb 28, 2023 at 5:20
-
ok edited. note that the board is powered with USB while the sensor is powered by a power supply module.Adam Cherti– Adam Cherti02/28/2023 22:25:46Commented Feb 28, 2023 at 22:25
-
I would connect the esp gnd to the z19c gnd.DrG– DrG03/01/2023 00:10:56Commented Mar 1, 2023 at 0:10
-
1It means connect PIN1 of ESP32S(GND) to pin2 of of Z19C(GND), basic 101 electronics.hcheung– hcheung03/01/2023 01:53:55Commented Mar 1, 2023 at 1:53
1 Answer 1
This is not a Winsen Mh-z19 sensor (or any clone following it's specs) so you won't be able to read data using a mhz19 library. Most probably it's a HC8 sensor and someone would have to rewrite library to get data from it. Here are specs: https://pcus.ru/download/hc8.pdf and some more info: https://spezifisch.codeberg.page/posts/2022-08-23/co2-sensor-reverse-engineering/
-
how do you know it's not mh-z19?Adam Cherti– Adam Cherti12/15/2023 03:23:36Commented Dec 15, 2023 at 3:23