mirror of
https://github.com/miguel5612/MQSensorsLib.git
synced 2025-03-15 05:17:30 +03:00
Fixed example
This commit is contained in:
parent
d79a303ce9
commit
532a83e186
@ -21,10 +21,11 @@
|
||||
#define Voltage_Resolution 5
|
||||
#define pin A0 //Analog input 0 of your arduino
|
||||
#define type "MQ-3" //MQ3
|
||||
#define ADC_Bit_Resolution 10 // For arduino UNO/MEGA/NANO
|
||||
//#define calibration_button 13 //Pin to calibrate your sensor
|
||||
|
||||
//Declare Sensor
|
||||
MQUnifiedsensor MQ3(placa, Voltage_Resolution, pin, type);
|
||||
MQUnifiedsensor MQ3(placa, Voltage_Resolution, ADC_Bit_Resolution, pin, type);
|
||||
|
||||
//Variables
|
||||
float CH4, LPG, CO, Alcohol, Hexane, Benzine;
|
||||
|
@ -1,10 +1,11 @@
|
||||
#include "MQUnifiedsensor.h"
|
||||
|
||||
MQUnifiedsensor::MQUnifiedsensor(String Placa, double Voltage_Resolution, int pin, String type) {
|
||||
MQUnifiedsensor::MQUnifiedsensor(String Placa, double Voltage_Resolution, int ADC_Bit_Resolution, int pin, String type) {
|
||||
this->_pin = pin;
|
||||
this->_type = type; //MQ-2, MQ-3 ... MQ-309A
|
||||
this->_placa = Placa;
|
||||
this-> _VOLT_RESOLUTION = Voltage_Resolution;
|
||||
this-> _ADC_Bit_Resolution = ADC_Bit_Resolution;
|
||||
}
|
||||
void MQUnifiedsensor::init()
|
||||
{
|
||||
@ -53,6 +54,7 @@ void MQUnifiedsensor::serialDebug(bool onSetup)
|
||||
|
||||
Serial.println("Sensor: " + _type);
|
||||
Serial.print("Supply voltage: "); Serial.print(_VOLT_RESOLUTION); Serial.println(" VDC");
|
||||
Serial.print("ADC Resolution: "); Serial.print(_ADC_Bit_Resolution); Serial.println(" Bits");
|
||||
Serial.print("R0: "); Serial.print(_R0); Serial.println(" KΩ");
|
||||
Serial.print("RL: "); Serial.print(_RL); Serial.println(" KΩ");
|
||||
|
||||
@ -74,7 +76,7 @@ void MQUnifiedsensor::serialDebug(bool onSetup)
|
||||
String eq = "";
|
||||
if(_regressionMethod == "Linear") eq = "ratio*a + b";
|
||||
if(_regressionMethod == "Exponential") eq = "a*ratio^b";
|
||||
Serial.print("|"); Serial.print(_adc); Serial.print("| v = ADC*"); Serial.print(_VOLT_RESOLUTION); Serial.print("/1024 | "); Serial.print(_sensor_volt);
|
||||
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);
|
||||
Serial.print(" | RS = ((" ); Serial.print(_VOLT_RESOLUTION ); Serial.print("*RL)/Voltage) - RL| "); Serial.print(_RS_Calc); Serial.print(" | Ratio = RS/R0| ");
|
||||
Serial.print(_ratio); Serial.print( " | " + eq + " | "); Serial.print(_PPM); Serial.println(" |");
|
||||
}
|
||||
@ -90,7 +92,7 @@ float MQUnifiedsensor::readSensor()
|
||||
_RS_Calc = ((_VOLT_RESOLUTION*_RL)/_sensor_volt)-_RL; //Get value of RS in a gas
|
||||
if(_RS_Calc < 0) _RS_Calc = 0; //No negative values accepted.
|
||||
_ratio = _RS_Calc / this->_R0; // Get ratio RS_gas/RS_air
|
||||
if(_ratio <= 0 || _ratio>100) _ratio = 0.01; //No negative values accepted or upper datasheet recomendation.
|
||||
if(_ratio <= 0) _ratio = 0; //No negative values accepted or upper datasheet recomendation.
|
||||
if(_regressionMethod == "Exponential") _PPM= _a*pow(_ratio, _b);
|
||||
if(_regressionMethod == "Linear") _PPM= _a*_ratio + _b;
|
||||
if(_PPM < 0) _PPM = 0; //No negative values accepted or upper datasheet recomendation.
|
||||
|
@ -13,7 +13,7 @@
|
||||
class MQUnifiedsensor
|
||||
{
|
||||
public:
|
||||
MQUnifiedsensor(String Placa = "Arduino", double Voltage_Resolution = 5, int pin = 1, String type = "CUSTOM MQ");
|
||||
MQUnifiedsensor(String Placa = "Arduino", double Voltage_Resolution = 5, int ADC_Bit_Resolution = 10, int pin = 1, String type = "CUSTOM MQ");
|
||||
|
||||
//Functions to set values
|
||||
void init();
|
||||
@ -44,6 +44,7 @@ class MQUnifiedsensor
|
||||
byte _VOLT_RESOLUTION = 5.0; // if 3.3v use 3.3
|
||||
byte _ratioInCleanAir;
|
||||
byte _RL = 10; //Value in KiloOhms
|
||||
byte _ADC_Bit_Resolution = 10;
|
||||
|
||||
double _adc, _a, _b, _sensor_volt;
|
||||
float _R0, RS_air, _ratio, _PPM, _RS_Calc;
|
||||
|
Loading…
x
Reference in New Issue
Block a user