malokhvii-eduard / arduino-mhz19

💥 Arduino library for Winsen MH-Z19 – infrared carbon dioxide sensor. The library allows using the sensor over hardware or software UART on any board that supports Arduino.
https://registry.platformio.org/libraries/malokhvii-eduard/arduino-mhz19
MIT License
22 stars 2 forks source link

Device never ready #3

Closed adimux closed 1 year ago

adimux commented 1 year ago
Hi first of all thanks for providing this library! I have a ESP32 module connected to MH-Z19C like so: MH-Z19C ESP32
VIN 3V3
GND GND
RX TX2 (17)
TX RX2 (16)

As seen in the schematics of ESP32 RX2 is PIN 16 and TX 2 is PIN 17.

I took your basic example but the device never seems to be "ready". I also tried to remove the "ready" condition to see if it's able to get any reading but it's always - 1 ppm.

What did I do wrong? How can I debug this? Thank you!

#include <Mhz19.h>
#include <SoftwareSerial.h>

SoftwareSerial softwareSerial(16, 17); 

Mhz19 sensor;

void setup() {
  Serial.begin(9600);
  softwareSerial.begin(9600);

  sensor.begin(&softwareSerial);
  sensor.setMeasuringRange(Mhz19MeasuringRange::Ppm_2000);
  sensor.enableAutoBaseCalibration();

  Serial.println("Preheating...");  // Preheating, 3 minutes
  while (!sensor.isReady()) {
    delay(50);
    Serial.println("Preheating...");
  }

  Serial.println("Ready...");
}

void loop() {
  auto carbonDioxide = sensor.getCarbonDioxide();
  Serial.println(String(carbonDioxide) + " ppm");
  if (carbonDioxide >= 0) {
    Serial.println(String(carbonDioxide) + " ppm");
  }

  delay(3000);
}
malokhvii-eduard commented 1 year ago

@adimux Hi! Thanks for the issue and sorry for the long reply. You have the wrong pins in the firmware. Can you swap the RX and TX pins only in the code? So it has to be SoftwareSerial softwareSerial(17, 16);. Please, let me know if it helps you.

malokhvii-eduard commented 1 year ago

@adimux Can you try the example code with a hardware serial?

malokhvii-eduard commented 1 year ago

@adimux Please try to use macros for pin mappings. Here's a header with pin mappings.