2019-04-30 08:52:47 -05:00
|
|
|
#include "MQUnifiedsensor.h"
|
|
|
|
|
2020-03-26 22:18:07 -05:00
|
|
|
MQUnifiedsensor::MQUnifiedsensor(String Placa, float Voltage_Resolution, int ADC_Bit_Resolution, int pin, String type) {
|
2019-04-30 08:52:47 -05:00
|
|
|
this->_pin = pin;
|
2020-03-26 22:18:07 -05:00
|
|
|
Placa.toCharArray(this->_placa, 20);
|
|
|
|
type.toCharArray(this->_type, 6);
|
|
|
|
//this->_type = type; //MQ-2, MQ-3 ... MQ-309A
|
|
|
|
//this->_placa = Placa;
|
2020-03-26 09:56:29 -05:00
|
|
|
this-> _VOLT_RESOLUTION = Voltage_Resolution;
|
2020-03-26 11:39:50 -05:00
|
|
|
this-> _ADC_Bit_Resolution = ADC_Bit_Resolution;
|
2020-03-26 09:56:29 -05:00
|
|
|
}
|
2020-03-26 10:56:52 -05:00
|
|
|
void MQUnifiedsensor::init()
|
|
|
|
{
|
|
|
|
pinMode(_pin, INPUT);
|
|
|
|
}
|
2020-03-26 22:18:07 -05:00
|
|
|
void MQUnifiedsensor::setA(float a) {
|
2020-03-26 10:23:51 -05:00
|
|
|
this->_a = a;
|
|
|
|
}
|
2020-03-26 22:18:07 -05:00
|
|
|
void MQUnifiedsensor::setB(float b) {
|
2020-03-26 10:23:51 -05:00
|
|
|
this->_b = b;
|
|
|
|
}
|
2020-03-26 22:18:07 -05:00
|
|
|
void MQUnifiedsensor::setR0(float R0) {
|
2020-03-26 10:56:52 -05:00
|
|
|
this->_R0 = R0;
|
|
|
|
}
|
2020-03-26 22:18:07 -05:00
|
|
|
void MQUnifiedsensor::setRL(float RL) {
|
2020-03-26 10:56:52 -05:00
|
|
|
this->_RL = RL;
|
|
|
|
}
|
2020-03-26 22:18:07 -05:00
|
|
|
void MQUnifiedsensor::setVoltResolution(float voltage_resolution)
|
2020-03-26 10:56:52 -05:00
|
|
|
{
|
|
|
|
_VOLT_RESOLUTION = voltage_resolution;
|
|
|
|
}
|
2020-03-26 22:18:07 -05:00
|
|
|
void MQUnifiedsensor::setRegressionMethod(int regressionMethod)
|
2020-03-26 10:56:52 -05:00
|
|
|
{
|
2020-03-26 22:18:07 -05:00
|
|
|
//this->_regressionMethod = regressionMethod;
|
2020-03-26 10:56:52 -05:00
|
|
|
this->_regressionMethod = regressionMethod;
|
|
|
|
}
|
2020-03-26 22:18:07 -05:00
|
|
|
float MQUnifiedsensor::getR0() {
|
2020-03-26 10:56:52 -05:00
|
|
|
return _R0;
|
|
|
|
}
|
2020-03-26 22:18:07 -05:00
|
|
|
float MQUnifiedsensor::getRL() {
|
2020-03-26 10:56:52 -05:00
|
|
|
return _RL;
|
|
|
|
}
|
2020-03-26 22:18:07 -05:00
|
|
|
float MQUnifiedsensor::getVoltResolution()
|
|
|
|
{
|
|
|
|
return _VOLT_RESOLUTION;
|
|
|
|
}
|
|
|
|
String MQUnifiedsensor::getRegressionMethod()
|
|
|
|
{
|
|
|
|
if(_regressionMethod == 1) return "Exponential";
|
|
|
|
else return "Linear";
|
|
|
|
}
|
|
|
|
float MQUnifiedsensor::getA() {
|
|
|
|
return _a;
|
|
|
|
}
|
|
|
|
float MQUnifiedsensor::getB() {
|
|
|
|
return _b;
|
|
|
|
}
|
2020-03-26 10:56:52 -05:00
|
|
|
void MQUnifiedsensor::serialDebug(bool onSetup)
|
2020-03-26 09:56:29 -05:00
|
|
|
{
|
|
|
|
if(onSetup)
|
2019-05-23 15:17:39 -05:00
|
|
|
{
|
2020-03-26 11:17:46 -05:00
|
|
|
Serial.println();
|
2020-03-26 09:56:29 -05:00
|
|
|
Serial.println("************************************************************************************************************************************************");
|
|
|
|
Serial.println("MQ sensor reading library for arduino");
|
|
|
|
|
|
|
|
Serial.println("Note: remember that all the parameters below can be modified during the program execution with the methods:");
|
2020-03-26 11:24:39 -05:00
|
|
|
Serial.println("setR0, setRL, setA, setB where you will have to send as parameter the new value, example: mySensor.setR0(20); //R0 = 20KΩ");
|
2020-03-26 09:56:29 -05:00
|
|
|
|
|
|
|
Serial.println("Authors: Miguel A. Califa U - Yersson R. Carrillo A - Ghiordy F. Contreras C");
|
|
|
|
Serial.println("Contributors: Andres A. Martinez - Juan A. Rodríguez - Mario A. Rodríguez O ");
|
|
|
|
|
2020-03-26 22:18:07 -05:00
|
|
|
Serial.print("Sensor: "); Serial.println(_type);
|
2020-03-26 11:24:39 -05:00
|
|
|
Serial.print("Supply voltage: "); Serial.print(_VOLT_RESOLUTION); Serial.println(" VDC");
|
2020-03-26 11:39:50 -05:00
|
|
|
Serial.print("ADC Resolution: "); Serial.print(_ADC_Bit_Resolution); Serial.println(" Bits");
|
2020-03-26 11:24:39 -05:00
|
|
|
Serial.print("R0: "); Serial.print(_R0); Serial.println(" KΩ");
|
|
|
|
Serial.print("RL: "); Serial.print(_RL); Serial.println(" KΩ");
|
2020-03-26 09:56:29 -05:00
|
|
|
|
2020-03-26 11:59:34 -05:00
|
|
|
Serial.print("Model: "); if(_regressionMethod == "Exponential") Serial.println("Exponential"); else Serial.println("Linear");
|
2020-03-26 22:18:07 -05:00
|
|
|
Serial.print(_type); Serial.print(" -> a: "); Serial.print(_a); Serial.print(" | b: "); Serial.println(_b);
|
2020-03-26 09:56:29 -05:00
|
|
|
|
2020-03-26 22:18:07 -05:00
|
|
|
Serial.print("Development board: "); Serial.println(_placa);
|
2019-05-23 15:17:39 -05:00
|
|
|
}
|
2020-03-26 09:56:29 -05:00
|
|
|
else
|
2019-05-23 15:17:39 -05:00
|
|
|
{
|
2020-03-26 09:56:29 -05:00
|
|
|
if(!_firstFlag)
|
|
|
|
{
|
2020-03-26 22:18:07 -05:00
|
|
|
Serial.print("| ********************************************************************"); Serial.print(_type); Serial.println("*********************************************************************|");
|
2020-03-26 11:24:39 -05:00
|
|
|
Serial.println("|ADC_In | Equation_V_ADC | Voltage_ADC | Equation_RS | Resistance_RS | EQ_Ratio | Ratio (RS/R0) | Equation_PPM | PPM |");
|
2020-03-26 09:56:29 -05:00
|
|
|
_firstFlag = true; //Headers are printed
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-26 11:39:50 -05:00
|
|
|
Serial.print("|"); Serial.print(_adc); Serial.print("| v = ADC*"); Serial.print(_VOLT_RESOLUTION); Serial.print("/"); Serial.print(pow(2, _ADC_Bit_Resolution)); Serial.print(" | "); Serial.print(_sensor_volt);
|
2020-03-26 11:08:53 -05:00
|
|
|
Serial.print(" | RS = ((" ); Serial.print(_VOLT_RESOLUTION ); Serial.print("*RL)/Voltage) - RL| "); Serial.print(_RS_Calc); Serial.print(" | Ratio = RS/R0| ");
|
2020-03-26 22:18:07 -05:00
|
|
|
Serial.print(_ratio); Serial.print( " | ");
|
2020-03-28 12:23:38 -05:00
|
|
|
if(_regressionMethod == 1) Serial.println("ratio*a + b");
|
|
|
|
else Serial.print("pow(10, (log10(ratio)-b)/a)");
|
2020-03-26 22:18:07 -05:00
|
|
|
Serial.print(" | "); Serial.print(_PPM); Serial.println(" |");
|
2020-03-26 09:56:29 -05:00
|
|
|
}
|
2019-05-23 15:17:39 -05:00
|
|
|
}
|
2019-04-30 08:52:47 -05:00
|
|
|
}
|
2019-08-14 20:47:24 -05:00
|
|
|
void MQUnifiedsensor::update()
|
|
|
|
{
|
|
|
|
_sensor_volt = this->getVoltage();
|
|
|
|
}
|
2020-03-26 22:18:07 -05:00
|
|
|
float MQUnifiedsensor::validateEcuation(float ratioInput)
|
2020-03-26 20:13:27 -05:00
|
|
|
{
|
2020-03-27 21:02:15 -05:00
|
|
|
//Serial.print("Ratio input: "); Serial.println(ratioInput);
|
|
|
|
//Serial.print("a: "); Serial.println(_a);
|
|
|
|
//Serial.print("b: "); Serial.println(_b);
|
2020-03-26 20:13:58 -05:00
|
|
|
//Usage of this function: Unit test on ALgorithmTester example;
|
2020-03-27 21:02:15 -05:00
|
|
|
if(_regressionMethod == 1) _PPM= _a*pow(ratioInput, _b);
|
2020-03-28 12:23:38 -05:00
|
|
|
else
|
|
|
|
{
|
2020-03-28 12:58:25 -05:00
|
|
|
// https://jayconsystems.com/blog/understanding-a-gas-sensor
|
|
|
|
double ppm_log = (log10(ratioInput)-_b)/_a; //Get ppm value in linear scale according to the the ratio value
|
|
|
|
_PPM = pow(10, ppm_log); //Convert ppm value to log scale
|
2020-03-28 12:23:38 -05:00
|
|
|
}
|
2020-03-27 21:02:15 -05:00
|
|
|
//Serial.println("Regression Method: "); Serial.println(_regressionMethod);
|
|
|
|
//Serial.println("Result: "); Serial.println(_PPM);
|
2020-03-26 20:13:27 -05:00
|
|
|
return _PPM;
|
|
|
|
}
|
2020-03-26 10:56:52 -05:00
|
|
|
float MQUnifiedsensor::readSensor()
|
2019-04-30 08:52:47 -05:00
|
|
|
{
|
2019-08-15 20:47:42 -05:00
|
|
|
//More explained in: https://jayconsystems.com/blog/understanding-a-gas-sensor
|
2020-03-26 10:56:52 -05:00
|
|
|
_RS_Calc = ((_VOLT_RESOLUTION*_RL)/_sensor_volt)-_RL; //Get value of RS in a gas
|
2019-09-08 12:49:02 -05:00
|
|
|
if(_RS_Calc < 0) _RS_Calc = 0; //No negative values accepted.
|
2019-08-14 20:47:24 -05:00
|
|
|
_ratio = _RS_Calc / this->_R0; // Get ratio RS_gas/RS_air
|
2020-03-26 11:39:50 -05:00
|
|
|
if(_ratio <= 0) _ratio = 0; //No negative values accepted or upper datasheet recomendation.
|
2020-03-28 12:25:48 -05:00
|
|
|
if(_regressionMethod == 1) _PPM= _a*pow(_ratio, _b); // <- Source excel analisis https://github.com/miguel5612/MQSensorsLib_Docs/tree/master/Internal_design_documents
|
2020-03-28 12:23:38 -05:00
|
|
|
else
|
|
|
|
{
|
2020-03-28 12:25:48 -05:00
|
|
|
// https://jayconsystems.com/blog/understanding-a-gas-sensor <- Source of linear ecuation
|
2020-03-28 12:23:38 -05:00
|
|
|
double ppm_log = (log10(_ratio)-_b)/_a; //Get ppm value in linear scale according to the the ratio value
|
|
|
|
_PPM = pow(10, ppm_log); //Convert ppm value to log scale
|
|
|
|
}
|
2019-09-08 12:49:02 -05:00
|
|
|
if(_PPM < 0) _PPM = 0; //No negative values accepted or upper datasheet recomendation.
|
2020-03-26 11:17:46 -05:00
|
|
|
//if(_PPM > 10000) _PPM = 99999999; //No negative values accepted or upper datasheet recomendation.
|
2019-05-24 17:02:46 -05:00
|
|
|
return _PPM;
|
2019-04-30 08:52:47 -05:00
|
|
|
}
|
2020-03-26 11:54:18 -05:00
|
|
|
float MQUnifiedsensor::calibrate(float ratioInCleanAir) {
|
2019-05-29 20:58:22 -05:00
|
|
|
//More explained in: https://jayconsystems.com/blog/understanding-a-gas-sensor
|
|
|
|
/*
|
|
|
|
V = I x R
|
|
|
|
VRL = [VC / (RS + RL)] x RL
|
|
|
|
VRL = (VC x RL) / (RS + RL)
|
|
|
|
Así que ahora resolvemos para RS:
|
|
|
|
VRL x (RS + RL) = VC x RL
|
|
|
|
(VRL x RS) + (VRL x RL) = VC x RL
|
|
|
|
(VRL x RS) = (VC x RL) - (VRL x RL)
|
|
|
|
RS = [(VC x RL) - (VRL x RL)] / VRL
|
|
|
|
RS = [(VC x RL) / VRL] - RL
|
|
|
|
*/
|
2019-07-22 22:27:47 -05:00
|
|
|
float RS_air; //Define variable for sensor resistance
|
|
|
|
float R0; //Define variable for R0
|
2020-03-26 10:56:52 -05:00
|
|
|
RS_air = ((_VOLT_RESOLUTION*_RL)/_sensor_volt)-_RL; //Calculate RS in fresh air
|
2019-09-08 12:49:02 -05:00
|
|
|
if(RS_air < 0) RS_air = 0; //No negative values accepted.
|
2020-03-26 11:54:18 -05:00
|
|
|
R0 = RS_air/ratioInCleanAir; //Calculate R0
|
2019-09-08 12:49:02 -05:00
|
|
|
if(R0 < 0) R0 = 0; //No negative values accepted.
|
2019-05-29 20:58:22 -05:00
|
|
|
return R0;
|
2019-04-30 08:52:47 -05:00
|
|
|
}
|
2020-03-26 22:18:07 -05:00
|
|
|
float MQUnifiedsensor::getVoltage(int read) {
|
|
|
|
float voltage;
|
2019-06-01 18:00:10 -05:00
|
|
|
if(read)
|
|
|
|
{
|
2020-03-26 22:18:07 -05:00
|
|
|
float avg = 0.0;
|
2019-06-01 18:00:10 -05:00
|
|
|
for (int i = 0; i < retries; i ++) {
|
2020-03-26 09:56:29 -05:00
|
|
|
_adc = analogRead(this->_pin);
|
|
|
|
avg += _adc;
|
2019-06-01 18:00:10 -05:00
|
|
|
delay(retry_interval);
|
|
|
|
}
|
2019-06-01 18:21:14 -05:00
|
|
|
voltage = (avg/ retries) * _VOLT_RESOLUTION / (pow(2, ADC_RESOLUTION) - 1);
|
2019-04-30 08:52:47 -05:00
|
|
|
}
|
2019-08-14 21:33:24 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
voltage = _sensor_volt;
|
|
|
|
}
|
2019-04-30 08:52:47 -05:00
|
|
|
return voltage;
|
|
|
|
}
|
2020-03-26 22:18:07 -05:00
|
|
|
float MQUnifiedsensor::stringTofloat(String & str)
|
2019-05-24 22:09:11 -05:00
|
|
|
{
|
|
|
|
return atof( str.c_str() );
|
2019-05-24 17:13:54 -05:00
|
|
|
}
|