mq lib

Dependents:   gather_sensor_data

Committer:
readysteadygo2006
Date:
Thu Sep 08 14:05:33 2016 +0000
Revision:
0:a43405a3d273
test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
readysteadygo2006 0:a43405a3d273 1 /**************************************************************************/
readysteadygo2006 0:a43405a3d273 2 /*!
readysteadygo2006 0:a43405a3d273 3 @file MQ135.cpp
readysteadygo2006 0:a43405a3d273 4 @author G.Krocker (Mad Frog Labs)
readysteadygo2006 0:a43405a3d273 5 @license GNU GPLv3
readysteadygo2006 0:a43405a3d273 6
readysteadygo2006 0:a43405a3d273 7 First version of an Arduino Library for the MQ135 gas sensor
readysteadygo2006 0:a43405a3d273 8 TODO: Review the correction factor calculation. This currently relies on
readysteadygo2006 0:a43405a3d273 9 the datasheet but the information there seems to be wrong.
readysteadygo2006 0:a43405a3d273 10
readysteadygo2006 0:a43405a3d273 11 @section HISTORY
readysteadygo2006 0:a43405a3d273 12
readysteadygo2006 0:a43405a3d273 13 v1.0 - First release
readysteadygo2006 0:a43405a3d273 14 */
readysteadygo2006 0:a43405a3d273 15 /**************************************************************************/
readysteadygo2006 0:a43405a3d273 16
readysteadygo2006 0:a43405a3d273 17 #include "MQ135.h"
readysteadygo2006 0:a43405a3d273 18
readysteadygo2006 0:a43405a3d273 19 /**************************************************************************/
readysteadygo2006 0:a43405a3d273 20 /*!
readysteadygo2006 0:a43405a3d273 21 @brief Default constructor
readysteadygo2006 0:a43405a3d273 22
readysteadygo2006 0:a43405a3d273 23 @param[in] pin The analog input pin for the readout of the sensor
readysteadygo2006 0:a43405a3d273 24 */
readysteadygo2006 0:a43405a3d273 25 /**************************************************************************/
readysteadygo2006 0:a43405a3d273 26 MQ135::MQ135(PinName pin) {
readysteadygo2006 0:a43405a3d273 27 _pin = pin;
readysteadygo2006 0:a43405a3d273 28 }
readysteadygo2006 0:a43405a3d273 29
readysteadygo2006 0:a43405a3d273 30 MQ135::~MQ135() {
readysteadygo2006 0:a43405a3d273 31 }
readysteadygo2006 0:a43405a3d273 32
readysteadygo2006 0:a43405a3d273 33 /**************************************************************************/
readysteadygo2006 0:a43405a3d273 34 /*!
readysteadygo2006 0:a43405a3d273 35 @brief Get the correction factor to correct for temperature and humidity
readysteadygo2006 0:a43405a3d273 36
readysteadygo2006 0:a43405a3d273 37 @param[in] t The ambient air temperature
readysteadygo2006 0:a43405a3d273 38 @param[in] h The relative humidity
readysteadygo2006 0:a43405a3d273 39
readysteadygo2006 0:a43405a3d273 40 @return The calculated correction factor
readysteadygo2006 0:a43405a3d273 41 */
readysteadygo2006 0:a43405a3d273 42 /**************************************************************************/
readysteadygo2006 0:a43405a3d273 43 float MQ135::getCorrectionFactor(float t, float h) {
readysteadygo2006 0:a43405a3d273 44 return CORA * t * t - CORB * t + CORC - (h-33.)*CORD;
readysteadygo2006 0:a43405a3d273 45 }
readysteadygo2006 0:a43405a3d273 46
readysteadygo2006 0:a43405a3d273 47 /**************************************************************************/
readysteadygo2006 0:a43405a3d273 48 /*!
readysteadygo2006 0:a43405a3d273 49 @brief Get the resistance of the sensor, ie. the measurement value
readysteadygo2006 0:a43405a3d273 50
readysteadygo2006 0:a43405a3d273 51 @return The sensor resistance in kOhm
readysteadygo2006 0:a43405a3d273 52 */
readysteadygo2006 0:a43405a3d273 53 /**************************************************************************/
readysteadygo2006 0:a43405a3d273 54 float MQ135::getResistance() {
readysteadygo2006 0:a43405a3d273 55 // AnalogIn ain(_pin);
readysteadygo2006 0:a43405a3d273 56 AnalogIn ain(PA_0);
readysteadygo2006 0:a43405a3d273 57 int val = ain.read();
readysteadygo2006 0:a43405a3d273 58 return ((1023./(float)val) * 5. - 1.)*RLOAD;
readysteadygo2006 0:a43405a3d273 59 }
readysteadygo2006 0:a43405a3d273 60
readysteadygo2006 0:a43405a3d273 61 /**************************************************************************/
readysteadygo2006 0:a43405a3d273 62 /*!
readysteadygo2006 0:a43405a3d273 63 @brief Get the resistance of the sensor, ie. the measurement value corrected
readysteadygo2006 0:a43405a3d273 64 for temp/hum
readysteadygo2006 0:a43405a3d273 65
readysteadygo2006 0:a43405a3d273 66 @param[in] t The ambient air temperature
readysteadygo2006 0:a43405a3d273 67 @param[in] h The relative humidity
readysteadygo2006 0:a43405a3d273 68
readysteadygo2006 0:a43405a3d273 69 @return The corrected sensor resistance kOhm
readysteadygo2006 0:a43405a3d273 70 */
readysteadygo2006 0:a43405a3d273 71 /**************************************************************************/
readysteadygo2006 0:a43405a3d273 72 float MQ135::getCorrectedResistance(float t, float h) {
readysteadygo2006 0:a43405a3d273 73 return getResistance()/getCorrectionFactor(t, h);
readysteadygo2006 0:a43405a3d273 74 }
readysteadygo2006 0:a43405a3d273 75
readysteadygo2006 0:a43405a3d273 76 /**************************************************************************/
readysteadygo2006 0:a43405a3d273 77 /*!
readysteadygo2006 0:a43405a3d273 78 @brief Get the ppm of CO2 sensed (assuming only CO2 in the air)
readysteadygo2006 0:a43405a3d273 79
readysteadygo2006 0:a43405a3d273 80 @return The ppm of CO2 in the air
readysteadygo2006 0:a43405a3d273 81 */
readysteadygo2006 0:a43405a3d273 82 /**************************************************************************/
readysteadygo2006 0:a43405a3d273 83 float MQ135::getPPM() {
readysteadygo2006 0:a43405a3d273 84 return PARA * pow((getResistance()/RZERO), -PARB);
readysteadygo2006 0:a43405a3d273 85 }
readysteadygo2006 0:a43405a3d273 86
readysteadygo2006 0:a43405a3d273 87 /**************************************************************************/
readysteadygo2006 0:a43405a3d273 88 /*!
readysteadygo2006 0:a43405a3d273 89 @brief Get the ppm of CO2 sensed (assuming only CO2 in the air), corrected
readysteadygo2006 0:a43405a3d273 90 for temp/hum
readysteadygo2006 0:a43405a3d273 91
readysteadygo2006 0:a43405a3d273 92 @param[in] t The ambient air temperature
readysteadygo2006 0:a43405a3d273 93 @param[in] h The relative humidity
readysteadygo2006 0:a43405a3d273 94
readysteadygo2006 0:a43405a3d273 95 @return The ppm of CO2 in the air
readysteadygo2006 0:a43405a3d273 96 */
readysteadygo2006 0:a43405a3d273 97 /**************************************************************************/
readysteadygo2006 0:a43405a3d273 98 float MQ135::getCorrectedPPM(float t, float h) {
readysteadygo2006 0:a43405a3d273 99 return PARA * pow((getCorrectedResistance(t, h)/RZERO), -PARB);
readysteadygo2006 0:a43405a3d273 100 }
readysteadygo2006 0:a43405a3d273 101
readysteadygo2006 0:a43405a3d273 102 /**************************************************************************/
readysteadygo2006 0:a43405a3d273 103 /*!
readysteadygo2006 0:a43405a3d273 104 @brief Get the resistance RZero of the sensor for calibration purposes
readysteadygo2006 0:a43405a3d273 105
readysteadygo2006 0:a43405a3d273 106 @return The sensor resistance RZero in kOhm
readysteadygo2006 0:a43405a3d273 107 */
readysteadygo2006 0:a43405a3d273 108 /**************************************************************************/
readysteadygo2006 0:a43405a3d273 109 float MQ135::getRZero() {
readysteadygo2006 0:a43405a3d273 110 return getResistance() * pow((ATMOCO2/PARA), (1./PARB));
readysteadygo2006 0:a43405a3d273 111 }
readysteadygo2006 0:a43405a3d273 112
readysteadygo2006 0:a43405a3d273 113 /**************************************************************************/
readysteadygo2006 0:a43405a3d273 114 /*!
readysteadygo2006 0:a43405a3d273 115 @brief Get the corrected resistance RZero of the sensor for calibration
readysteadygo2006 0:a43405a3d273 116 purposes
readysteadygo2006 0:a43405a3d273 117
readysteadygo2006 0:a43405a3d273 118 @param[in] t The ambient air temperature
readysteadygo2006 0:a43405a3d273 119 @param[in] h The relative humidity
readysteadygo2006 0:a43405a3d273 120
readysteadygo2006 0:a43405a3d273 121 @return The corrected sensor resistance RZero in kOhm
readysteadygo2006 0:a43405a3d273 122 */
readysteadygo2006 0:a43405a3d273 123 /**************************************************************************/
readysteadygo2006 0:a43405a3d273 124 float MQ135::getCorrectedRZero(float t, float h) {
readysteadygo2006 0:a43405a3d273 125 return getCorrectedResistance(t, h) * pow((ATMOCO2/PARA), (1./PARB));
readysteadygo2006 0:a43405a3d273 126 }