upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
Public Member Functions

API for the TP401 Gas Sensor. More...

Detailed Description

The TP401 Gas Sensor module is useful for monitoring air purity indoors. It can detect CO and a wide range of other harmful gases, but, due to a limited detection range, it should be used only when qualitative results are needed. Example applications are air recirculation, ventilation systems, and refreshing sprayers. The sensor is linear and should be roughly sensitive to 0-20 ppm CO from 0-4 V. Note: the sensor requires 2-3 minutes to warm up initially and 48 hours of operation to stabilize completely.

// Give a qualitative meaning to the value from the sensor
airQuality(uint16_t value)
if (value < 50)
return "Fresh Air";
if (value < 200)
return "Normal Indoor Air";
if (value < 400)
return "Low Pollution";
if (value < 600)
return "High Pollution - Action Recommended";
return "Very High Pollution - Take Action Immediately";
upm::TP401 airSensor(0);
cout << airSensor.name() << endl;
fprintf(stdout, "Heating sensor for 3 minutes...\n");
// wait 3 minutes for sensor to warm up
for (int i = 0; i < 3; i++) {
if (i) {
fprintf(stdout, "Please wait, %d minute(s) passed..\n", i);
fprintf(stdout, "Sensor ready!\n");
while (true) {
uint16_t value = airSensor.getSample(); // Read raw value
float ppm = airSensor.getPPM(); // Read CO ppm (can vary slightly from previous read)
fprintf(stdout, "raw: %4d ppm: %5.2f %s\n", value, ppm, airQuality(value).c_str());
upm_delay_us(2500000); // Sleep for 2.5s
return 0;

Public Member Functions

 TP401 (int gasPin)
 ~TP401 ()
std::string name ()
float getPPM ()
- Public Member Functions inherited from Gas
 Gas (int gasPin)
virtual ~Gas ()
virtual int getSampledWindow (unsigned int freqMS, int numberOfSamples, uint16_t *buffer)
virtual int findThreshold (thresholdContext *ctx, unsigned int threshold, uint16_t *buffer, int len)
virtual int getSampledData (thresholdContext *ctx)
virtual int getSample ()
virtual void printGraph (thresholdContext *ctx, uint8_t resolution)

Additional Inherited Members

- Protected Attributes inherited from Gas
mraa::Aio m_aio

Constructor & Destructor Documentation

TP401 ( int  gasPin)

TP401 constructor

gasPinAnalog pin where the sensor is connected
~TP401 ( )

TP401 destructor

Member Function Documentation

std::string name ( )

Returns the name of the sensor

Name of the sensor

Here is the call graph for this function:

float getPPM ( )

Returns one sample in parts per million (ppm) of CO in the air based on the following sensor calibration: 0-4 V is roughly 0-20 ppm CO

New sample converted to ppm CO

Here is the call graph for this function:

Here is the caller graph for this function:

Inheritance diagram for TP401:
Inheritance graph
Collaboration diagram for TP401:
Collaboration graph

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