Help getting data from a MH-Z19C

Hi

For a school research paper I have to measure the CO2 value of a given room. I thought it would be easiest to do with Arduino and a CO2 sensor. Luckily the school still had a MH-Z19C lying around so i got to use that.
I am now programming the sensor to give me relevant data like CO2 value (ppm) and temperature (°C).
I have searched online and found a piece of code i thought would work well. It was written for use with Arduino Mega so I adapted it to work with Arduino uno. But every time I try to upload it i get:

exit status 1
'Serial' does not name a type

This is my code:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX

// Start serial in setup routine
Serial.begin(9600);

// Command and response
byte cmd[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};
byte response[9];

// Clear serial buffer
while (Serial.available() > 0) {
  Serial.read();
}

// Send command and read response
Serial.write(cmd, 9);
Serial.readBytes(response, 9);

// Check if response is valid
if (response[0] != 0xFF) {
  // wrong starting byte from co2 sensor
}
if (response[1] != 0x86) {
  // Wrong command from co2 sensor
}

int ppm = (256 * response[2]) + response[3];
int temp = response[4]-40;
byte status = response[5];
int minimum = (256 * response[6]) + response[7];

This is my wiring diagram:

Does anyone know how to resolve this issue?
Thanks

Check how you have substituted capital letters for lower case and the other way around. All the matters to the C compiler.
Paul

Change Serial to mySerial such as mySerial.begin(9600);

Your code does not contain setup() and loop() functions.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX

// Command and response
byte cmd[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};
byte response[9];

void setup()
{

  // Start serial in setup routine
  Serial.begin(9600);
  // Clear serial buffer
  while (Serial.available() > 0) {
    Serial.read();
  }

  // Send command and read response
  Serial.write(cmd, 9);
  Serial.readBytes(response, 9);

  // Check if response is valid
  if (response[0] != 0xFF) {
    // wrong starting byte from co2 sensor
  }
  if (response[1] != 0x86) {
    // Wrong command from co2 sensor
  }

  int ppm = (256 * response[2]) + response[3];
  int temp = response[4] - 40;
  byte status = response[5];
  int minimum = (256 * response[6]) + response[7];
}
void loop() {}

Why is there nothing in loop? Would't it have to be in loop to keep getting data?

I was merely demonstrating that your code needed setup() and loop() functions to compile. You are correct that setup() runs once and loop() repeats.

void setup() {
 // put your setup code here, to run once:
}
void loop() {
 // put your main code here, to run repeatedly:
}

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