IoT Drivers  v3.3.0 (S2022)
Engineering in Software Technology
Quick start guide for MH-Z19 CO2 Driver

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

  1. The following must be added to the project:
    #include <mh_z19.h>
  2. Add to application initialization: Initialise the driver:
    // The parameter is the USART port the MH-Z19 sensor is connected to - in this case USART3
    mh_z19_initialise(ser_USART3);
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

  1. Create a call back function:
    void myCo2CallBack(uint16_t ppm)
    {
    // Here you can use the CO2 ppm value
    }
    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

mh_z19_injectCallBack(myCo2CallBack)
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.
  1. Define a variable to get the CO2 ppm in and a return code variable.
    uint16_t ppm;
  2. Ask the driver to perform measuring.
    if (rc != OK)
    {
    // Something went wrong
    }
  3. When the driver has received the new ppm value from the sensor the specified call back function (see Initialise the driver) will be called.
mh_z19_takeMeassuring
mh_z19_returnCode_t mh_z19_takeMeassuring(void)
Perform a new CO2 meassuring.
mh_z19_injectCallBack
void mh_z19_injectCallBack(void(*mh_z19_callBack)(uint16_t))
Inject a call-back function to the driver.
mh_z19_returnCode_t
mh_z19_returnCode_t
MH-Z19 Driver return codes.
Definition: mh_z19.h:52
mh_z19.h
Driver to CO2 using the Intelligent Infrared CO2 Module MH-Z19.
mh_z19_initialise
void mh_z19_initialise(serial_comPort_t com_port)
Initialise the driver.