Trying to combine 2 codes but nothing appears

// MH-Z19B CO2 Sensor Serial Communication Example
#include "PMS.h"

PMS pms(Serial1);
PMS::DATA data;
// Define the serial port for MH-Z19B sensor (Serial3 on Arduino Mega)
#define MHZ19B_SERIAL Serial2

// Define the serial port for Bluetooth (Serial2 on Arduino Mega)
#define BLUETOOTH_SERIAL Serial3

// Define the baud rate for MH-Z19B sensor
#define MHZ19B_BAUDRATE 9600

// Define the command to read CO2 concentration
uint8_t MHZ19B_CMD_READ_CO2[] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};

// Define the zero point CO2 concentration (e.g., typical outdoor level)
#define ZERO_POINT_CO2 400 // ppm


void setup() {
  // Initialize Serial Monitor for debugging
  BLUETOOTH_SERIAL.begin(9600);
  Serial.begin(9600);
  //pms5003
  Serial1.begin(9600);

  // Initialize MH-Z19B Serial Port
  MHZ19B_SERIAL.begin(MHZ19B_BAUDRATE);
  
  // Wait for the sensor to warm up
  delay(2000);
  
  // Perform zero point calibration
  calibrateZeroPoint();
}

void loop() {

  void co2();
  void pm();
  
}

void co2(){

  if (pms.read(data))
  {
    Serial.print("PM 1.0 (ug/m3): ");
    Serial.println(data.PM_AE_UG_1_0);

    BLUETOOTH_SERIAL.print(data.PM_AE_UG_1_0);
    BLUETOOTH_SERIAL.print("ug/m3");
    BLUETOOTH_SERIAL.print(",");

    Serial.print("PM 2.5 (ug/m3): ");
    Serial.println(data.PM_AE_UG_2_5);

    BLUETOOTH_SERIAL.print(data.PM_AE_UG_2_5);
    BLUETOOTH_SERIAL.print("ug/m3");
    BLUETOOTH_SERIAL.print(",");

    Serial.print("PM 10.0 (ug/m3): ");
    Serial.println(data.PM_AE_UG_10_0);

    BLUETOOTH_SERIAL.print(data.PM_AE_UG_10_0);
    BLUETOOTH_SERIAL.print("ug/m3");
    BLUETOOTH_SERIAL.print(",");

    Serial.println();
  }

  // Send command to MH-Z19B sensor to read CO2 concentration
  MHZ19B_SERIAL.write(MHZ19B_CMD_READ_CO2, sizeof(MHZ19B_CMD_READ_CO2));
  
  // Wait for sensor to respond (80 ms)
  delay(80);
  
  // Read response from the sensor
  uint8_t response[9];
  MHZ19B_SERIAL.readBytes(response, 9);
  
  
  // Check if response is valid
  if (response[0] == 0xFF && response[1] == 0x86) {
    // Calculate CO2 concentration (high byte * 256 + low byte)
    int co2 = response[2] * 256 + response[3];
    
    // Print CO2 concentration to Bluetooth
    Serial.print(co2);
    Serial.println(" ppm");
    
    
    BLUETOOTH_SERIAL.print(co2);
    BLUETOOTH_SERIAL.print(" ppm");
    BLUETOOTH_SERIAL.print(";");

  } else {
    // Print error if response is invalid to Bluetooth
    Serial.println("Error: Invalid response from sensor");
  }
  
  // Delay before next reading
  delay(5000); // Adjust delay time as needed

}

void pm(){

  if (pms.read(data))
  {
    Serial.print("PM 1.0 (ug/m3): ");
    Serial.println(data.PM_AE_UG_1_0);

    Serial.print("PM 2.5 (ug/m3): ");
    Serial.println(data.PM_AE_UG_2_5);

    Serial.print("PM 10.0 (ug/m3): ");
    Serial.println(data.PM_AE_UG_10_0);

    Serial.println();
  }


}

void calibrateZeroPoint() {
  // Send command to MH-Z19B sensor to calibrate zero point
  // The zero point command for MH-Z19B is: FF 01 87 00 00 00 00 00 78
  uint8_t MHZ19B_CMD_CALIBRATE_ZERO_POINT[] = {0xFF, 0x01, 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78};
  MHZ19B_SERIAL.write(MHZ19B_CMD_CALIBRATE_ZERO_POINT, sizeof(MHZ19B_CMD_CALIBRATE_ZERO_POINT));
  
  // Wait for calibration to complete
  delay(3000); // Calibration takes approximately 3 seconds
  
}

Basically if i use each sensor alone, they work. But when i try to combine them, nothing appears on my serial monitor. I tried to define the co2 and pm code for each sensor then call it into the void loop() , but won t work.

Use

void loop() {

  co2();
  pm();
  
}

Comments that don't match the statement are worse than no comments. This might indicate that the wiring does not match the code; note that we can't possibly know.

done it in the meantime. I tried this code:

// MH-Z19B CO2 Sensor Serial Communication Example
#include "PMS.h"

PMS pms(Serial1);
PMS::DATA data;
// Define the serial port for MH-Z19B sensor (Serial3 on Arduino Mega)
#define MHZ19B_SERIAL Serial2

// Define the serial port for Bluetooth (Serial2 on Arduino Mega)
#define BLUETOOTH_SERIAL Serial3

// Define the baud rate for MH-Z19B sensor
#define MHZ19B_BAUDRATE 9600

// Define the command to read CO2 concentration
uint8_t MHZ19B_CMD_READ_CO2[] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};

// Define the zero point CO2 concentration (e.g., typical outdoor level)
#define ZERO_POINT_CO2 400 // ppm


void setup() {
  // Initialize Serial Monitor for debugging
  BLUETOOTH_SERIAL.begin(9600);
  Serial.begin(9600);
  //pms5003
  Serial1.begin(9600);

  // Initialize MH-Z19B Serial Port
  MHZ19B_SERIAL.begin(MHZ19B_BAUDRATE);
  
  // Wait for the sensor to warm up
  delay(2000);
  
  // Perform zero point calibration
  calibrateZeroPoint();
}
void pm(){

  if (pms.read(data))
  {
    Serial.print("PM 1.0 (ug/m3): ");
    Serial.println(data.PM_AE_UG_1_0);

    BLUETOOTH_SERIAL.print(data.PM_AE_UG_1_0);
    BLUETOOTH_SERIAL.print("ug/m3");
    BLUETOOTH_SERIAL.print(",");

    Serial.print("PM 2.5 (ug/m3): ");
    Serial.println(data.PM_AE_UG_2_5);

    BLUETOOTH_SERIAL.print(data.PM_AE_UG_2_5);
    BLUETOOTH_SERIAL.print("ug/m3");
    BLUETOOTH_SERIAL.print(",");

    Serial.print("PM 10.0 (ug/m3): ");
    Serial.println(data.PM_AE_UG_10_0);

    BLUETOOTH_SERIAL.print(data.PM_AE_UG_10_0);
    BLUETOOTH_SERIAL.print("ug/m3");
    BLUETOOTH_SERIAL.print(";");

    Serial.println();
  }


}


void co2(){

  if (pms.read(data))
  {
    Serial.print("PM 1.0 (ug/m3): ");
    Serial.println(data.PM_AE_UG_1_0);

    BLUETOOTH_SERIAL.print(data.PM_AE_UG_1_0);
    BLUETOOTH_SERIAL.print("ug/m3");
    BLUETOOTH_SERIAL.print(",");

    Serial.print("PM 2.5 (ug/m3): ");
    Serial.println(data.PM_AE_UG_2_5);

    BLUETOOTH_SERIAL.print(data.PM_AE_UG_2_5);
    BLUETOOTH_SERIAL.print("ug/m3");
    BLUETOOTH_SERIAL.print(",");

    Serial.print("PM 10.0 (ug/m3): ");
    Serial.println(data.PM_AE_UG_10_0);

    BLUETOOTH_SERIAL.print(data.PM_AE_UG_10_0);
    BLUETOOTH_SERIAL.print("ug/m3");
    BLUETOOTH_SERIAL.print(",");

    Serial.println();
  }

  // Send command to MH-Z19B sensor to read CO2 concentration
  MHZ19B_SERIAL.write(MHZ19B_CMD_READ_CO2, sizeof(MHZ19B_CMD_READ_CO2));
  
  // Wait for sensor to respond (80 ms)
  delay(80);
  
  // Read response from the sensor
  uint8_t response[9];
  MHZ19B_SERIAL.readBytes(response, 9);
  
  
  // Check if response is valid
  if (response[0] == 0xFF && response[1] == 0x86) {
    // Calculate CO2 concentration (high byte * 256 + low byte)
    int co2 = response[2] * 256 + response[3];
    
    // Print CO2 concentration to Bluetooth
    Serial.print(co2);
    Serial.println(" ppm");
    
    
    BLUETOOTH_SERIAL.print(co2);
    BLUETOOTH_SERIAL.print(" ppm");
    BLUETOOTH_SERIAL.print(",");

  } else {
    // Print error if response is invalid to Bluetooth
    Serial.println("Error: Invalid response from sensor");
  }
  
  // Delay before next reading
  delay(5000); // Adjust delay time as needed

}



void loop() {
  
  pm();
  co2();
  
  
}


void calibrateZeroPoint() {
  // Send command to MH-Z19B sensor to calibrate zero point
  // The zero point command for MH-Z19B is: FF 01 87 00 00 00 00 00 78
  uint8_t MHZ19B_CMD_CALIBRATE_ZERO_POINT[] = {0xFF, 0x01, 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78};
  MHZ19B_SERIAL.write(MHZ19B_CMD_CALIBRATE_ZERO_POINT, sizeof(MHZ19B_CMD_CALIBRATE_ZERO_POINT));
  
  // Wait for calibration to complete
  delay(3000); // Calibration takes approximately 3 seconds
  
}
I only get the co2 values.

I forgot to remove the comments. The hardware matches the software.

Pms5003 is on rx1, tx1 (rx-tx, tx-rx)
mh-z19b is on rx2, tx2 (same as above)
bt hc-05 is on rx3, tx3 (same as above)

// MH-Z19B CO2 Sensor Serial Communication Example
#include "PMS.h"

PMS pms(Serial1);
PMS::DATA data;
// Define the serial port for MH-Z19B sensor (Serial3 on Arduino Mega)
#define MHZ19B_SERIAL Serial2

// Define the serial port for Bluetooth (Serial2 on Arduino Mega)
#define BLUETOOTH_SERIAL Serial3

// Define the baud rate for MH-Z19B sensor
#define MHZ19B_BAUDRATE 9600

// Define the command to read CO2 concentration
uint8_t MHZ19B_CMD_READ_CO2[] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};

// Define the zero point CO2 concentration (e.g., typical outdoor level)
#define ZERO_POINT_CO2 400 // ppm


void setup() {
  // Initialize Serial Monitor for debugging
  BLUETOOTH_SERIAL.begin(9600);
  Serial.begin(9600);
  //pms5003
  Serial1.begin(9600);

  // Initialize MH-Z19B Serial Port
  MHZ19B_SERIAL.begin(MHZ19B_BAUDRATE);
  
  // Wait for the sensor to warm up
  delay(2000);
  
  // Perform zero point calibration
  calibrateZeroPoint();
}
void pm(){

  if (pms.read(data))
  {
    Serial.print("PM 1.0 (ug/m3): ");
    Serial.println(data.PM_AE_UG_1_0);

    BLUETOOTH_SERIAL.print(data.PM_AE_UG_1_0);
    BLUETOOTH_SERIAL.print("ug/m3");
    BLUETOOTH_SERIAL.print(",");

    Serial.print("PM 2.5 (ug/m3): ");
    Serial.println(data.PM_AE_UG_2_5);

    BLUETOOTH_SERIAL.print(data.PM_AE_UG_2_5);
    BLUETOOTH_SERIAL.print("ug/m3");
    BLUETOOTH_SERIAL.print(",");

    Serial.print("PM 10.0 (ug/m3): ");
    Serial.println(data.PM_AE_UG_10_0);

    BLUETOOTH_SERIAL.print(data.PM_AE_UG_10_0);
    BLUETOOTH_SERIAL.print("ug/m3");
    BLUETOOTH_SERIAL.print(";");

    Serial.println();
  }


}


void co2(){

  if (pms.read(data))
  {
    Serial.print("PM 1.0 (ug/m3): ");
    Serial.println(data.PM_AE_UG_1_0);

    BLUETOOTH_SERIAL.print(data.PM_AE_UG_1_0);
    BLUETOOTH_SERIAL.print("ug/m3");
    BLUETOOTH_SERIAL.print(",");

    Serial.print("PM 2.5 (ug/m3): ");
    Serial.println(data.PM_AE_UG_2_5);

    BLUETOOTH_SERIAL.print(data.PM_AE_UG_2_5);
    BLUETOOTH_SERIAL.print("ug/m3");
    BLUETOOTH_SERIAL.print(",");

    Serial.print("PM 10.0 (ug/m3): ");
    Serial.println(data.PM_AE_UG_10_0);

    BLUETOOTH_SERIAL.print(data.PM_AE_UG_10_0);
    BLUETOOTH_SERIAL.print("ug/m3");
    BLUETOOTH_SERIAL.print(",");

    Serial.println();
  }

  // Send command to MH-Z19B sensor to read CO2 concentration
  MHZ19B_SERIAL.write(MHZ19B_CMD_READ_CO2, sizeof(MHZ19B_CMD_READ_CO2));
  
  // Wait for sensor to respond (80 ms)
  delay(80);
  
  // Read response from the sensor
  uint8_t response[9];
  MHZ19B_SERIAL.readBytes(response, 9);
  
  
  // Check if response is valid
  if (response[0] == 0xFF && response[1] == 0x86) {
    // Calculate CO2 concentration (high byte * 256 + low byte)
    int co2 = response[2] * 256 + response[3];
    
    // Print CO2 concentration to Bluetooth
    Serial.print(co2);
    Serial.println(" ppm");
    
    
    BLUETOOTH_SERIAL.print(co2);
    BLUETOOTH_SERIAL.print(" ppm");
    BLUETOOTH_SERIAL.print(",");

  } else {
    // Print error if response is invalid to Bluetooth
    Serial.println("Error: Invalid response from sensor");
  }
  
  // Delay before next reading
  delay(5000); // Adjust delay time as needed

}



void loop() {
  
  pm();
  co2();
  
  
}


void calibrateZeroPoint() {
  // Send command to MH-Z19B sensor to calibrate zero point
  // The zero point command for MH-Z19B is: FF 01 87 00 00 00 00 00 78
  uint8_t MHZ19B_CMD_CALIBRATE_ZERO_POINT[] = {0xFF, 0x01, 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78};
  MHZ19B_SERIAL.write(MHZ19B_CMD_CALIBRATE_ZERO_POINT, sizeof(MHZ19B_CMD_CALIBRATE_ZERO_POINT));
  
  // Wait for calibration to complete
  delay(3000); // Calibration takes approximately 3 seconds
  
}

now i only get co2 values and sometimes and randomly ( once ) the pms :
18:11:49.312 -> PM 1.0 (ug/m3): 2
18:11:49.312 -> PM 2.5 (ug/m3): 2
18:11:49.374 -> PM 10.0 (ug/m3): 2
18:11:49.374 ->
18:11:49.406 -> 404 ppm

The full code for pm() is duplicated inside co2()

Is that on purpose?

my bad

#include "PMS.h"

PMS pms(Serial1);
PMS::DATA data;

// Define the serial port for MH-Z19B sensor (Serial2 on Arduino Mega)
#define MHZ19B_SERIAL Serial2

// Define the serial port for Bluetooth (Serial3 on Arduino Mega)
#define BLUETOOTH_SERIAL Serial3

// Define the baud rate for MH-Z19B sensor
#define MHZ19B_BAUDRATE 9600

// Define the command to read CO2 concentration
uint8_t MHZ19B_CMD_READ_CO2[] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};

void setup() {
  // Initialize Serial Monitor for debugging
  BLUETOOTH_SERIAL.begin(9600);
  Serial.begin(9600);
  Serial1.begin(9600); // For PMS5003
  MHZ19B_SERIAL.begin(MHZ19B_BAUDRATE);
  
  // Wait for the sensor to warm up
  delay(2000);
}

void pm() {
  if (pms.read(data)) {
    Serial.print("PM 1.0 (ug/m3): ");
    Serial.println(data.PM_AE_UG_1_0);

    Serial.print("PM 2.5 (ug/m3): ");
    Serial.println(data.PM_AE_UG_2_5);

    Serial.print("PM 10.0 (ug/m3): ");
    Serial.println(data.PM_AE_UG_10_0);
  } else {
    Serial.println("Failed to read PM data");
  }
}

void co2() {
  // Send command to MH-Z19B sensor to read CO2 concentration
  MHZ19B_SERIAL.write(MHZ19B_CMD_READ_CO2, sizeof(MHZ19B_CMD_READ_CO2));
  
  // Wait for sensor to respond (80 ms)
  delay(80);
  
  // Read response from the sensor
  uint8_t response[9];
  MHZ19B_SERIAL.readBytes(response, 9);
  
  // Check if response is valid
  if (response[0] == 0xFF && response[1] == 0x86) {
    // Calculate CO2 concentration (high byte * 256 + low byte)
    int co2 = response[2] * 256 + response[3];
    
    // Print CO2 concentration to Serial Monitor
    Serial.print("CO2 Level: ");
    Serial.print(co2);
    Serial.println(" ppm");
  } else {
    // Print error if response is invalid
    Serial.println("Error: Invalid response from sensor");
  }
}

void loop() {
  // Read PM data and print
  pm();
  
  // Wait 5 seconds
  delay(5000);
  
  // Read CO2 data and print
  co2();
  
  // Wait 5 seconds
  delay(5000);
}

know it prints randomly some pms values then it stays on "Failed to read PM data"

What if you try

  const int timeOut = 5000;
  if (pms.readUntil(data, timeOut)) {

and remove the second delay(5000); inside loop() ?

now its working.

20:18:05.071 -> PM 1.0 (ug/m3): 2
20:18:05.071 -> PM 2.5 (ug/m3): 3
20:18:05.117 -> PM 10.0 (ug/m3): 4
20:18:05.117 -> CO2 Level: 401 ppm
20:18:11.216 -> PM 1.0 (ug/m3): 2
20:18:11.216 -> PM 2.5 (ug/m3): 3
20:18:11.249 -> PM 10.0 (ug/m3): 3
20:18:11.249 -> CO2 Level: 400 ppm

Thanks!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.