CO2 sensor

This example shows how to use UART to connect MH-Z19 CO2 sensor to Z-Uno board.
  • Z-Uno board
  • Breadboard
  • Wires
  • MH-Z19 CO2 sensor
        char response[9];
byte cmd[9] = { 0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79 };
int newValue;
int responseHigh;
int responseLow;
int ppm;

ZUNO_SETUP_SLEEPING_MODE(ZUNO_SLEEPING_MODE_ALWAYS_AWAKE);

ZUNO_SETUP_CHANNELS(ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_CO2_LEVEL, 0, SENSOR_MULTILEVEL_SIZE_TWO_BYTES, SENSOR_MULTILEVEL_PRECISION_ZERO_DECIMALS, getter));

void setup() {
  Serial1.begin(9600); //connecting to sensor via UART
}

void loop() {
  int i = 0;
  for (i = 0; i < 9; i++) {
    Serial1.write(cmd[i]);
  }

  delay(100);

  i = 0;
  while (Serial1.available()) {
    response[i] = Serial1.read();
    i++;
  }

  responseHigh = (int) response[2]; // returned High level concentration
  responseLow  = (int) response[3]; // returned Low level concentration
  newValue = (256 * responseHigh) + responseLow;
  if ((newValue > (ppm + 100)) || (newValue < (ppm - 100))) {
    ppm = newValue;
    zunoSendReport(1);
  }
  delay(1000);
}

int getter(void) {
  return newValue;
}

      char response[9];                                                       //storage incoming data
byte cmd[9] = { 0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79 }; //bytes for send request to get sensor data

int newValueCO2, responseHigh, responseLow, ppm;           //Co2 calculation and storage variables
byte temperature, newValueTemp, lastTemp;                  //Temperature calculation and storage variables

ZUNO_SETUP_SLEEPING_MODE(ZUNO_SLEEPING_MODE_ALWAYS_AWAKE); //Z-Uno Sleaping mode
ZUNO_SETUP_CHANNELS(
  ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_CO2_LEVEL, 0, SENSOR_MULTILEVEL_SIZE_TWO_BYTES, SENSOR_MULTILEVEL_PRECISION_ZERO_DECIMALS, getterCO2),                                  //Z-Uno channel for Co2
  ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_TEMPERATURE, SENSOR_MULTILEVEL_SCALE_CELSIUS, SENSOR_MULTILEVEL_SIZE_TWO_BYTES, SENSOR_MULTILEVEL_PRECISION_ZERO_DECIMALS, getterTemp)  //Z-Uno channel for Temperature
);

void setup() {
  Serial1.begin(9600); //connecting to sensor via UART (7,8 pin)

}

void loop() {
//send request to get data
  int i = 0;
  for (i = 0; i < 9; i++) {
    Serial1.write(cmd[i]);
  }
  delay(100);

//recive and storage requested data from sensor
  i = 0;
  while (Serial1.available()) {
    response[i] = Serial1.read();
    delay(100);
    i++;
  }
  
//Assign variables to the received data for further processing
  responseHigh = (int) response[2];      // returned High level concentration
  responseLow  = (int) response[3];      // returned Low level concentration
  temperature  = (int) response[4] - 40; // return temperature


//Сalculate the Co2 value and compare it with the previous one, if the difference is 100 ppm then we send a new value to the controller
  newValueCO2 = (256 * responseHigh) + responseLow;
  if ((newValueCO2 > (ppm + 100)) || (newValueCO2 < (ppm - 100))) {
    ppm = newValueCO2;
    zunoSendReport(1);
  }

//Compare the new temperature value with the old one, if it differs, we send new data to the controller
  if (temperature != lastTemp) {
    lastTemp = temperature;
    zunoSendReport(1);
  }
  delay(1000);
}


int getterCO2(void) {
  return newValueCO2;
}

int getterTemp(void) {
  return (temperature * 100);
}

Information from datasheet