mq lib

Dependents:   gather_sensor_data

Revision:
0:a43405a3d273
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MQ135.cpp	Thu Sep 08 14:05:33 2016 +0000
@@ -0,0 +1,126 @@
+/**************************************************************************/
+/*!
+@file     MQ135.cpp
+@author   G.Krocker (Mad Frog Labs)
+@license  GNU GPLv3
+
+First version of an Arduino Library for the MQ135 gas sensor
+TODO: Review the correction factor calculation. This currently relies on
+the datasheet but the information there seems to be wrong.
+
+@section  HISTORY
+
+v1.0 - First release
+*/
+/**************************************************************************/
+
+#include "MQ135.h"
+
+/**************************************************************************/
+/*!
+@brief  Default constructor
+
+@param[in] pin  The analog input pin for the readout of the sensor
+*/
+/**************************************************************************/
+MQ135::MQ135(PinName pin) {
+    _pin = pin;
+}
+
+MQ135::~MQ135() {
+}
+
+/**************************************************************************/
+/*!
+@brief  Get the correction factor to correct for temperature and humidity
+
+@param[in] t  The ambient air temperature
+@param[in] h  The relative humidity
+
+@return The calculated correction factor
+*/
+/**************************************************************************/
+float MQ135::getCorrectionFactor(float t, float h) {
+  return CORA * t * t - CORB * t + CORC - (h-33.)*CORD;
+}
+
+/**************************************************************************/
+/*!
+@brief  Get the resistance of the sensor, ie. the measurement value
+
+@return The sensor resistance in kOhm
+*/
+/**************************************************************************/
+float MQ135::getResistance() {
+//  AnalogIn ain(_pin);
+  AnalogIn ain(PA_0);
+  int val = ain.read();
+  return ((1023./(float)val) * 5. - 1.)*RLOAD;
+}
+
+/**************************************************************************/
+/*!
+@brief  Get the resistance of the sensor, ie. the measurement value corrected
+        for temp/hum
+
+@param[in] t  The ambient air temperature
+@param[in] h  The relative humidity
+
+@return The corrected sensor resistance kOhm
+*/
+/**************************************************************************/
+float MQ135::getCorrectedResistance(float t, float h) {
+  return getResistance()/getCorrectionFactor(t, h);
+}
+
+/**************************************************************************/
+/*!
+@brief  Get the ppm of CO2 sensed (assuming only CO2 in the air)
+
+@return The ppm of CO2 in the air
+*/
+/**************************************************************************/
+float MQ135::getPPM() {
+  return PARA * pow((getResistance()/RZERO), -PARB);
+}
+
+/**************************************************************************/
+/*!
+@brief  Get the ppm of CO2 sensed (assuming only CO2 in the air), corrected
+        for temp/hum
+
+@param[in] t  The ambient air temperature
+@param[in] h  The relative humidity
+
+@return The ppm of CO2 in the air
+*/
+/**************************************************************************/
+float MQ135::getCorrectedPPM(float t, float h) {
+  return PARA * pow((getCorrectedResistance(t, h)/RZERO), -PARB);
+}
+
+/**************************************************************************/
+/*!
+@brief  Get the resistance RZero of the sensor for calibration purposes
+
+@return The sensor resistance RZero in kOhm
+*/
+/**************************************************************************/
+float MQ135::getRZero() {
+  return getResistance() * pow((ATMOCO2/PARA), (1./PARB));
+}
+
+/**************************************************************************/
+/*!
+@brief  Get the corrected resistance RZero of the sensor for calibration
+        purposes
+
+@param[in] t  The ambient air temperature
+@param[in] h  The relative humidity
+
+@return The corrected sensor resistance RZero in kOhm
+*/
+/**************************************************************************/
+float MQ135::getCorrectedRZero(float t, float h) {
+  return getCorrectedResistance(t, h) * pow((ATMOCO2/PARA), (1./PARB));
+}