2019-04-30 08:52:47 -05:00
|
|
|
#include "MQUnifiedsensor.h"
|
|
|
|
|
2020-03-26 09:56:29 -05:00
|
|
|
MQUnifiedsensor::MQUnifiedsensor(String Placa, int Voltage_Resolution, int pin, String type) {
|
2019-04-30 08:52:47 -05:00
|
|
|
this->_pin = pin;
|
2020-03-26 09:56:29 -05:00
|
|
|
this->_type = type; //MQ-2, MQ-3 ... MQ-309A
|
|
|
|
this->_placa = Placa;
|
|
|
|
this-> _VOLT_RESOLUTION = Voltage_Resolution;
|
|
|
|
}
|
|
|
|
MQUnifiedsensor::serialDebug(boolean onSetup)
|
|
|
|
{
|
|
|
|
if(onSetup)
|
2019-05-23 15:17:39 -05:00
|
|
|
{
|
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:");
|
|
|
|
Serial.println("setR0, setRL, setA, setB where you will have to send as parameter the new value, example: mySensor.setR0(20); //R0 = 20K");
|
|
|
|
|
|
|
|
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 ");
|
|
|
|
|
|
|
|
Serial.println("Sensor:" + _type);
|
|
|
|
Serial.println("Supply voltage:" + _VOLT_RESOLUTION);
|
|
|
|
Serial.println("R0: " + _R0);
|
|
|
|
Serial.println("RL: " + _RL);
|
|
|
|
|
|
|
|
Serial.println("Model: Logarithmic regression with parameters.");
|
|
|
|
Serial.println(_type + ":" + "a:" + _a + " | b:" + _b);
|
|
|
|
|
|
|
|
Serial.println("Development board: " + _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)
|
|
|
|
{
|
|
|
|
Serial.println("| ****************************************************************" + _type + "****************************************************************|");
|
|
|
|
Serial.println("|ADC_In | Equation_V_ADC | Voltage_ADC | Equation_R | Resistance_RS | EQ_Ratio | Ratio (RS/R0) | Equation_PPM | PPM |");
|
|
|
|
_firstFlag = true; //Headers are printed
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
String eq = "";
|
|
|
|
if(regression == "Exponential") eq = "a*ratio^b"
|
|
|
|
Serial.println("|" + _adc + "|" + "v = ADC*" + _VOLT_RESOLUTION + "/1024" + "|" + _sensor_volt + "|" + "RS = ((" + _VOLT_RESOLUTION + "*RL)/Voltage) - RL" + "|" + _RS_Calc + "|" + "Ratio = RS/R0" + "|" + _ratio + "|" + eq + "|" + _PPM);
|
|
|
|
}
|
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();
|
|
|
|
}
|
2019-05-25 10:46:44 -05:00
|
|
|
void MQUnifiedsensor::setVoltResolution(float voltaje)
|
|
|
|
{
|
2019-05-29 22:13:13 -05:00
|
|
|
_VOLT_RESOLUTION = voltaje;
|
2019-05-25 10:46:44 -05:00
|
|
|
}
|
2020-03-26 10:12:04 -05:00
|
|
|
void MQUnifiedsensor::init()
|
2019-05-24 17:13:54 -05:00
|
|
|
{
|
|
|
|
pinMode(_pin, INPUT);
|
2019-04-30 08:52:47 -05:00
|
|
|
}
|
2020-03-26 10:09:36 -05:00
|
|
|
float MQUnifiedsensor::readSensor(String nameLectureRequeired)
|
2019-04-30 08:52:47 -05:00
|
|
|
{
|
2019-08-14 20:47:24 -05:00
|
|
|
setSensorCharacteristics(nameLectureRequeired, print); //In this function update _a and _b
|
2019-08-15 20:47:42 -05:00
|
|
|
//More explained in: https://jayconsystems.com/blog/understanding-a-gas-sensor
|
2019-08-14 20:47:24 -05:00
|
|
|
_RS_Calc = ((_VOLT_RESOLUTION*_RLValue)/_sensor_volt)-_RLValue; //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
|
2019-09-08 12:49:02 -05:00
|
|
|
if(_ratio <= 0 || _ratio>100) _ratio = 0.01; //No negative values accepted or upper datasheet recomendation.
|
2019-08-14 21:33:24 -05:00
|
|
|
_PPM= _a*pow(_ratio, _b);
|
2019-09-08 12:49:02 -05:00
|
|
|
if(_PPM < 0) _PPM = 0; //No negative values accepted or upper datasheet recomendation.
|
|
|
|
if(_PPM > 10000) _PPM = 9999; //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
|
|
|
}
|
2019-05-24 17:13:54 -05:00
|
|
|
String MQUnifiedsensor::getnameLecture()
|
2019-05-23 16:51:24 -05:00
|
|
|
{
|
2019-05-24 22:15:53 -05:00
|
|
|
return _nameLectureRequeired;
|
2019-05-23 16:51:24 -05:00
|
|
|
}
|
2020-03-26 10:09:36 -05:00
|
|
|
float MQUnifiedsensor::calibrate() {
|
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
|
2019-09-08 12:49:02 -05:00
|
|
|
RS_air = ((_VOLT_RESOLUTION*_RLValue)/_sensor_volt)-_RLValue; //Calculate RS in fresh air
|
|
|
|
if(RS_air < 0) RS_air = 0; //No negative values accepted.
|
2019-05-29 20:58:22 -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
|
|
|
}
|
2019-06-01 18:00:10 -05:00
|
|
|
double MQUnifiedsensor::getVoltage(int read) {
|
2019-08-14 21:33:24 -05:00
|
|
|
double voltage;
|
2019-06-01 18:00:10 -05:00
|
|
|
if(read)
|
|
|
|
{
|
|
|
|
double avg = 0.0;
|
|
|
|
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;
|
|
|
|
}
|
2019-07-22 22:27:47 -05:00
|
|
|
|
|
|
|
|
2019-05-24 17:13:54 -05:00
|
|
|
void MQUnifiedsensor::setR0(double R0) {
|
|
|
|
this->_R0 = R0;
|
2019-05-24 22:09:11 -05:00
|
|
|
}
|
2019-07-22 22:27:47 -05:00
|
|
|
|
|
|
|
double MQUnifiedsensor::getR0() {
|
|
|
|
return _R0;
|
|
|
|
}
|
2019-09-28 11:05:55 -05:00
|
|
|
void MQUnifiedsensor::setRL(double RL) {
|
|
|
|
this->_RLValue = RL;
|
|
|
|
}
|
|
|
|
|
|
|
|
double MQUnifiedsensor::getRL() {
|
|
|
|
return _RLValue;
|
|
|
|
}
|
|
|
|
|
2019-05-24 22:14:34 -05:00
|
|
|
double MQUnifiedsensor::stringToDouble(String & str)
|
2019-05-24 22:09:11 -05:00
|
|
|
{
|
|
|
|
return atof( str.c_str() );
|
2019-05-24 17:13:54 -05:00
|
|
|
}
|