my MH-Z19C works fine when connected to my Arduino Uno. After wiring it to my new Arduino Mega it stopped working. What tweaks are there to make when switching between those two different Arduinos?
#include <Arduino.h>
#include "MHZ19.h"
#include <SoftwareSerial.h> // Remove if using HardwareSerial
#define RX_PIN 6 // Rx pin which the MHZ19 Tx pin is attached to
#define TX_PIN 7 // 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
mySerial.begin(BAUDRATE); // (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()
{
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);
delay(2000);
}
}
You are foolish to use Software Serial on a Mega, and your time is better spent trying to get Serial1,2,or 3 working instead of using various pins for software serial without reading the documentation.
Not all pins on the Mega and Mega 2560 support change interrupts, so only the following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69).
to make it complete, it also works with my first sketch:
#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
mySerial.begin(BAUDRATE); // (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()
{
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);
getDataTimer = millis();
}
}
hello. i tried to use ur code but why i cant upload it? the error messages is "no matching function for call to 'MHZ19::MHZ19(HardwareSerial&)' do u know how to solve it? sorry i just new to arduino. thankyou
I think there are several errors in the code posted in Reply #13.
Try this modification of the basic library example code. It compiles for the Mega.
#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();
}
}
thanks for ur help. anyway, im new on this sensor and i just read the datasheet recently. but i still dont understand deeply about the callibration. does the autocalibration in the code already calibrate the sensor? or still i need to do zero calibration and span calibration? thankyou