RobTillaart / MTP40F

Arduino library for MTP40F CO2 sensor.
MIT License
4 stars 1 forks source link

Arduino CI Arduino-lint JSON check GitHub issues

License: MIT GitHub release PlatformIO Registry

MTP40F

Arduino library for MTP40F CO2 sensor.

Description

The library for the MTP40F CO2 sensor is experimental as not all functionality is tested. It is based upon my https://github.com/RobTillaart/MTP40C library.

The MTP40F sensor is an NDIR (Non Dispersive InfraRed) CO2 sensor. It returns the CO2 level in PPM = parts per million, and in normal outside air it should be between 400 - 420.

The sensor communicates over a 9600 baud serial (TTL) interface with a microprocessor or PC. This implies that calls which can take up to 25 bytes can take as much as about 20 milliseconds.

On the other hand this low baud rate implies it will work over relative long distances. This signal quality over longer distances is not investigated.

The MTP40F has more interface options, I2C, PWM and ALARM. This library does not support these other interfaces for now. However minimal examples are added to have a starter but these need to be tested if and how well these work.

Warnings

During tests with an UNO the communication over Software Serial did fail a few times. Therefore it is important to always check return values to make your project more robust.

During tests it became clear that the sensor needs time to process commands e.g. setSelfCalibration().

CRC errors

Since 0.2.0 the CRC of the sensor responses are verified by the library. This will make the communication more robust by identifying the CRC errors.

The low level request() checks the CRC of the communication with the device. A new error-code MTP40F_INVALID_CRC is defined, however this error code need to be integrated better in the library.

Hardware interface MTP40-F

Has TTL level RS232, I2C and PWM IO.

                       TOPVIEW MTP40-F
                       +-------------+
                       |             | 
    VCC (3v3 out)  5 --|             |-- 1 Vin
    TX / SDA       6 --|             |-- 2 GND
    RX / SCL       7 --|             |-- 3 ALARM
    R/T            8 --|             |-- 4 PWM / I2C
    GND            9 --|             |
                       |             |
                       +-------------+
Pin Name Description
1 Vin 4.2V - 5.5V
2 GND idem
3 ALARM HIGH above 1000 PPM, LOW below 800 PPM (hysteresis)
4 PWM/I2C PWM out
5 VCC_O 3V3 out for serial
6 TX / SDA Transmit 9600 baud or SDA
7 RX / SCL Receive 9600 baud or SCL
8 R/T select Serial or I2C
9 GND idem

Links

Multi device

Sometimes you need to control more devices than possible. This is possible with a multiplexer e.g. HC4052 which can select between four devices.

Drawback of using a multiplexer is that it takes more administration in your code e.g. which device is on which channel. Furthermore using a multiplexer will slow down the access.

Interface

#include "MTP40F.h"

Constructor

Type Model Notes
2 MTP40C for ref only
3 MTP40D for ref only
5 MTP40F
255 MTP40 for ref only

CO2 Measurement

Timeout communication

The library can set a maximum timeout in the communication with the sensor. Normally this is not needed to set as the default of 100 milliseconds is long enough for even the longest command. This timeout is needed if the sensor did not process the command correctly, preventing the host to wait indefinitely.

Calibration

Please read datasheet before using these functions to understand the process of calibration.

Note the outdoor calibration CO2 level differs per day and one should check a local airport or weather station for a good reference.

The University of San Diego keeps track of CO2 for a long time now. See - https://keelingcurve.ucsd.edu/

Air pressure calibration

For pressure conversion - https://github.com/RobTillaart/pressure

SinglePointCorrection calibration

It takes a relative short time (few minutes) to calibrate the sensor in a known gas concentration.

As far as known the SPC value can not be retrieved from the sensor.

Self calibration

Self calibration is a process in which the sensor takes the minimum values over a longer period between 24 - 720 hours as the reference for minimum outdoor values. Note that 720 hours is 30 days / 1 month. Default seems to be 168 hours == 1 week.

Note: read datasheet!

Error codes

Value Definition
0x00 MTP40F_OK
0x01 MTP40F_INVALID_AIR_PRESSURE
0x02 MTP40F_INVALID_GAS_LEVEL
0x10 MTP40F_INVALID_CRC
0xFF MTP40F_INVALID_ADDRESS
0xFFFF MTP40F_REQUEST_FAILED

Future

Must

Should

Could

Wont

Support

If you appreciate my libraries, you can support the development and maintenance. Improve the quality of the libraries by providing issues and Pull Requests, or donate through PayPal or GitHub sponsors.

Thank you,