upm  1.3.0
Sensor/Actuator repository for libmraa (v1.7.0)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Public Member Functions | Protected Member Functions
MHZ16 Class Reference

API support for the Grove CO2 sensor. More...

Detailed Description

This class implements support for the Grove CO2 sensor.

Its CO2 detection range is 0-2,000 ppm. It requires a 2-3 minute warm-up time before reporting valid data.

// Instantiate a MHZ16 serial CO2 sensor on uart 0.
// This example was tested on the Grove CO2 sensor module.
upm::MHZ16* co2 = new upm::MHZ16(0);
// make sure port is initialized properly. 9600 baud is the default.
if (!co2->setupTty(B9600))
cerr << "Failed to setup tty port parameters" << endl;
return 1;
cout << "Make sure that the sensor has had at least 3 minutes to warm up"
<< endl;
cout << "or you will not get valid results." << endl;
cout << "The temperature reported is not the ambient temperature, " << endl;
cout << "but rather the temperature of the sensor elements." << endl;
while (shouldRun)
cerr << "Failed to retrieve data" << endl;
cout << "CO2 concentration: " << co2->getGas() << " PPM, "
<< "Temperature (in C): " << co2->getTemperature() <<endl;

Public Member Functions

 MHZ16 (int uart)
 ~MHZ16 ()
bool dataAvailable (unsigned int millis)
int readData (char *buffer, int len)
int writeData (char *buffer, int len)
bool setupTty (speed_t baud=B9600)
bool verifyPacket (uint8_t *pkt, int len)
bool getData ()
int getGas ()
int getTemperature ()
void calibrateZeroPoint ()

Protected Member Functions

int ttyFd ()

Constructor & Destructor Documentation

MHZ16 ( int  uart)

MHZ16 constructor

uartDefault UART to use (0 or 1)
~MHZ16 ( )

MHZ16 destructor

Member Function Documentation

bool dataAvailable ( unsigned int  millis)

Checks to see if there is data available for reading

millisNumber of milliseconds to wait; 0 means no waiting.
True if there is data available for reading
int readData ( char *  buffer,
int  len 

Reads any available data in a user-supplied buffer. Note: the call blocks until data is available to be read. Use dataAvailable() to determine whether there is data available beforehand, to avoid blocking.

bufferBuffer to hold the data read
lenLength of the buffer
Number of bytes read
int writeData ( char *  buffer,
int  len 

Writes the data in the buffer to the device

bufferBuffer to hold the data read
lenLength of the buffer
Number of bytes written
bool setupTty ( speed_t  baud = B9600)

Sets up proper tty I/O modes and the baud rate. The default baud rate is 9,600 (B9600).

baudDesired baud rate.
True if successful
bool verifyPacket ( uint8_t *  pkt,
int  len 

Verifies the packet header and indicates its validity

pktPacket to check
True if the checksum is valid, false otherwise
bool getData ( )

Queries the sensor and gets the gas (CO2) concentration and temperature data. Should be called before other "get" functions.

True if successful
int getGas ( )

Returns the gas (CO2) concentration data.

Gas concentration
int getTemperature ( void  )

Returns the temperature data.

Temperature in Celsius
void calibrateZeroPoint ( )

Sets the zero point of the sensor

The documentation for this class was generated from the following files: