# Help getting data from a MH-Z19C

**URL:** <https://forum.arduino.cc/t/help-getting-data-from-a-mh-z19c/867946>\
**Category:** Science and Measurement\
**Created:** [May 28, 2021, 5:45pm UTC](https://forum.arduino.cc/t/help-getting-data-from-a-mh-z19c/867946 "2021-05-28T17:45:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![wolf\_verniers](https://avatars.discourse-cdn.com/v4/letter/w/9fc348/32.png) [@wolf\_verniers](https://forum.arduino.cc/u/wolf_verniers)\
**Post date:** [May 28, 2021, 5:45pm UTC](https://forum.arduino.cc/t/help-getting-data-from-a-mh-z19c/867946/1 "2021-05-28T17:45:07Z")

</div>

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:

```cpp
#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:

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/5/4/3/543d9a56492d71c66114bfcca4276ea7dd23de4e.png)

Does anyone know how to resolve this issue?  
Thanks

---

<div class="post-metadata">

**Author:** ![Paul\_KD7HB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_kd7hb/32/1127958_2.png) [@Paul\_KD7HB](https://forum.arduino.cc/u/Paul_KD7HB)\
**Post date:** [May 28, 2021, 10:08pm UTC](https://forum.arduino.cc/t/help-getting-data-from-a-mh-z19c/867946/2 "2021-05-28T22:08:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ephitran](https://avatars.discourse-cdn.com/v4/letter/e/d9b06d/32.png) [@ephitran](https://forum.arduino.cc/u/ephitran)\
**Post date:** [May 28, 2021, 10:57pm UTC](https://forum.arduino.cc/t/help-getting-data-from-a-mh-z19c/867946/3 "2021-05-28T22:57:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cattledog](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cattledog/32/217816_2.png) [@cattledog](https://forum.arduino.cc/u/cattledog)\
**Post date:** [May 28, 2021, 11:29pm UTC](https://forum.arduino.cc/t/help-getting-data-from-a-mh-z19c/867946/4 "2021-05-28T23:29:51Z")

</div>

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

```cpp
#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() {}

```

---

<div class="post-metadata">

**Author:** ![wolf\_verniers](https://avatars.discourse-cdn.com/v4/letter/w/9fc348/32.png) [@wolf\_verniers](https://forum.arduino.cc/u/wolf_verniers)\
**Post date:** [May 29, 2021, 11:36am UTC](https://forum.arduino.cc/t/help-getting-data-from-a-mh-z19c/867946/5 "2021-05-29T11:36:25Z")

</div>

> [@cattledog](#):
>
> ```arduino
> #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?

---

<div class="post-metadata">

**Author:** ![cattledog](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cattledog/32/217816_2.png) [@cattledog](https://forum.arduino.cc/u/cattledog)\
**Post date:** [May 29, 2021, 2:01pm UTC](https://forum.arduino.cc/t/help-getting-data-from-a-mh-z19c/867946/6 "2021-05-29T14:01:02Z")

</div>

> [@wolf\_verniers](#):
>
> 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.

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

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 26, 2021, 2:01pm UTC](https://forum.arduino.cc/t/help-getting-data-from-a-mh-z19c/867946/7 "2021-09-26T14:01:25Z")

</div>

Closed on September 26, 2021, 2:01pm UTC

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