This is the quick start guide for the Driver for MH-Z19 CO2 sensor, with step-by-step instructions on how to configure and use the driver in simple use cases.
The use cases contain several code fragments. The code fragments in the steps for setup can be copied into a custom initialization function, while the steps for usage can be copied into, e.g., the main application function.
MH-Z19 Driver use cases
Initialise the driver
- The following must be added to the project:
- Add to application initialization: Initialise the driver:
- Note
- If FreeRTOS is used then the initialise function Initialise the driver must be called before
vTaskStartScheduler()
is called.
If it is wanted to inject a call-back function, then it must be done like this
- Create a call back function:
void myCo2CallBack(uint16_t ppm)
{
}
- Note
- The call-back function will called from an Interrupt Service Routine (ISR), so it must be very short and efficient!!
The call-back function will be called by the driver when a new CO2 value is returned by the sensor.
The call-back function is injected like this
- Note
- The call-back function will called from an Interrupt Service Routine (ISR), so it must be very short and efficient!!
Perform a CO2 measuring
In this use case, a CO2 measuring will be performed.
- Note
- The driver must be initialised Initialise the driver before a measuring can be performed.
- Define a variable to get the CO2 ppm in and a return code variable.
- Ask the driver to perform measuring.
- When the driver has received the new ppm value from the sensor the specified call back function (see Initialise the driver) will be called.