Hey,
I'm currently trying to connect an MH-Z19C Co2 sensor to an Arduino Mega 2560 via one of the serial interfaces, unfortunately with moderate success.
#include <Arduino.h>
#include "MHZ19.h"
//#include <SoftwareSerial.h> // Remove if using HardwareSerial
//#define RX_PIN 10 // Rx pin which the MHZ19 Tx pin is attached to
//#define TX_PIN 11 // Tx pin which the MHZ19 Rx pin is attached to
//#define BAUDRATE 9600 // Device to MH-Z19 Serial baudrate (should not be changed)
MHZ19 myMHZ19; // Constructor for library
//SoftwareSerial mySerial(RX_PIN, TX_PIN); // (Uno example) create device to MH-Z19 serial
unsigned long getDataTimer = 0;
void setup()
{
Serial.begin(9600); // Device to serial monitor feedback
Serial1.begin(9600);
//mySerial.begin(BAUDRATE); // (Uno example) device to MH-Z19 serial start
//myMHZ19.begin(mySerial); // *Serial(Stream) refence must be passed to library begin().
myMHZ19.begin(Serial1);
myMHZ19.autoCalibration(); // Turn auto calibration ON (OFF autoCalibration(false))
}
void loop()
{
if (millis() - getDataTimer >= 2000)
{
int CO2;
/* note: getCO2() default is command "CO2 Unlimited". This returns the correct CO2 reading even
if below background CO2 levels or above range (useful to validate sensor). You can use the
usual documented command with getCO2(false) */
CO2 = myMHZ19.getCO2(); // Request CO2 (as ppm)
Serial.print("CO2 (ppm): ");
Serial.println(CO2);
int8_t Temp;
Temp = myMHZ19.getTemperature(); // Request Temperature (as Celsius)
Serial.print("Temperature (C): ");
Serial.println(Temp);
getDataTimer = millis();
}
}
wiring:
VIN to 5V
GND to GND
TX on 19
RX on 18
The power supply runs via a separate power pack with Imax=2.5A
The result is a correct output of the temperature and permanently 0ppm.
I had previously read the sensor via a PWM signal, which worked, so the sensor should be intact.
Does anyone have an idea why this might be?
User’s Manual:
https://cdn-reichelt.de/documents/datenb...NBLATT.pdf
Libary: