mirror of
https://github.com/miguel5612/MQSensorsLib.git
synced 2025-07-02 02:11:04 +03:00
Compare commits
58 Commits
Author | SHA1 | Date | |
---|---|---|---|
59a2e5852e | |||
30c0979cb4 | |||
6f9b00666e | |||
d2e0be3651 | |||
3cd2fd8e7d | |||
2bdb91c1b5 | |||
42689ff5f3 | |||
ea48475a3c | |||
c066b2ec1c | |||
144f480f6f | |||
46d416abad | |||
f7fae4e03c | |||
c9c1549600 | |||
af291df03d | |||
748ff3055c | |||
a6abda567f | |||
92f3c30e82 | |||
da3adb7335 | |||
14b0f908f3 | |||
6376cfc11f | |||
c927504bf5 | |||
337d962862 | |||
f7f2c1f3f4 | |||
1c131eed65 | |||
b6bff185f3 | |||
a7e89570fd | |||
35180270ac | |||
52bccfbff1 | |||
3368613dd7 | |||
9f41530cda | |||
e862774fe0 | |||
27465dfb99 | |||
e3f5e2ec0c | |||
6a035cc5d0 | |||
80025e8398 | |||
d3241a1e2f | |||
4b71c626b0 | |||
07b6948f76 | |||
990e6878ef | |||
6322e9c03d | |||
964204706f | |||
1f8310a6e7 | |||
700d367d93 | |||
4be0a40997 | |||
6aa78fd575 | |||
c934152cd7 | |||
525a09644b | |||
16affe479f | |||
4a3d46f47e | |||
bf6d90db69 | |||
1818c3cf1a | |||
874be50106 | |||
71c0d82314 | |||
11bef492e5 | |||
728450025a | |||
a059971123 | |||
e9425f7047 | |||
beab80c403 |
2
.github/pull_request_template.md
vendored
2
.github/pull_request_template.md
vendored
@ -12,6 +12,8 @@ Please delete options that are not relevant.
|
||||
- [ ] New feature (non-breaking change which adds functionality)
|
||||
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
|
||||
- [ ] This change requires a documentation update
|
||||
- [ ] I have updated the version in `library.properties` to reflect my changes (advance by 0.01 for minor changes, or a whole number for new features)
|
||||
- [ ] I have submitted the pull request
|
||||
|
||||
# How Has This Been Tested?
|
||||
|
||||
|
@ -20,3 +20,7 @@ Pull request will be check by three main authors who are linked in
|
||||
[REAMDE.md](https://github.com/miguel5612/MQSensorsLib/README.md) and they determine to merge to
|
||||
master branch. Later than assessment and debbuging procedures was completed, the contribution
|
||||
will be released.
|
||||
|
||||
## Versioning
|
||||
|
||||
When making a pull request, please remember to update the `library.properties` file to reflect the changes you've made. If your change is minor, consider advancing the version by 0.01. If you're adding a new feature, you may want to advance the version by a whole number.
|
39
README.md
39
README.md
@ -31,6 +31,7 @@ We present a unified library for MQ sensors, this library allows to read MQ sign
|
||||
* [Sensor manufacturers](#Sensor-manufacturers)
|
||||
* [Contributing](#Contributing)
|
||||
* [Authors](#Authors)
|
||||
* [Be a sponsor 💖](#Sponsor)
|
||||
|
||||
## Getting Started
|
||||
|
||||
@ -50,7 +51,7 @@ We present a unified library for MQ sensors, this library allows to read MQ sign
|
||||
MQUnifiedsensor MQ4(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin, Type);
|
||||
// Setup
|
||||
MQ4.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ4.setA(1012.7); MQ4.setB(-2.786); // Configurate the ecuation values to get CH4 concentration
|
||||
MQ4.setA(1012.7); MQ4.setB(-2.786); // Configure the equation to to calculate CH4 concentration
|
||||
MQ4.setR0(3.86018237); // Value getted on calibration
|
||||
// Loop
|
||||
MQ4.init();
|
||||
@ -67,6 +68,7 @@ float ppmCH4 = MQ4.readSensor();
|
||||
* AO -> Analog Output of the sensor
|
||||
##### Data of board that you should have
|
||||
* RL Value in KOhms
|
||||
* If the sensor uses a different supply voltage than the ADC reference, call `yourSensor.setVCC(<voltage>);`
|
||||
##### Graph
|
||||

|
||||
#### RS/R0 value (From datasheet of your sensor)
|
||||
@ -81,13 +83,15 @@ float ppmCH4 = MQ4.readSensor();
|
||||
### ESP8266-ESP32
|
||||

|
||||
|
||||
### ESP32 WROOM 32D
|
||||
The ESP32 WROOM 32D does not need an external power supply. A0 goes to PIN36, Vcc to 3v3 and GND to any GND port on the board. Check the **ESP2/ESP32_WROOM_32** folder to fixing the measuring issue when connecting to wifi.
|
||||
|
||||
### Manuals
|
||||
#### User Manual (v1.0) 12.2019
|
||||
[Manual](https://drive.google.com/open?id=1BAFInlvqKR7h81zETtjz4_RC2EssvFWX)
|
||||
#### User Manual (v2.0) 04.2020
|
||||
[Manual](https://github.com/miguel5612/MQSensorsLib_Docs/blob/master/Docs/MQSensorLib_2.0.pdf)
|
||||
|
||||
|
||||
### Serial debug (optional)
|
||||
If your sensor is an **MQ2** (Same for others sensors):
|
||||
* To enable on setup wrote
|
||||
@ -118,16 +122,17 @@ You'll need Arduino desktop app 1.8.9 or later.
|
||||
### Sensor manufacturers:
|
||||
| Sensor | Manufacture | URL Datasheet |
|
||||
|----------|----------|----------|
|
||||
| MQ-2 | Pololulu| [datasheet](https://www.pololu.com/file/0J309/MQ2.pdf) |
|
||||
| MQ-3 | Sparkfun | [datasheet](https://www.sparkfun.com/datasheets/Sensors/MQ-3.pdf) |
|
||||
| MQ-4 | Sparkfun | [datasheet](https://www.sparkfun.com/datasheets/Sensors/Biometric/MQ-4.pdf) |
|
||||
| MQ-5 | parallax | [datasheet](https://www.parallax.com/sites/default/files/downloads/605-00009-MQ-5-Datasheet.pdf) |
|
||||
| MQ-6 | Sparkfun | [datasheet](https://www.sparkfun.com/datasheets/Sensors/Biometric/MQ-6.pdf) |
|
||||
| MQ-7 | Sparkfun | [datasheet](https://www.sparkfun.com/datasheets/Sensors/Biometric/MQ-7.pdf) |
|
||||
| MQ-8 | Sparkfun | [datasheet](https://dlnmh9ip6v2uc.cloudfront.net/datasheets/Sensors/Biometric/MQ-8.pdf) |
|
||||
| MQ-9 | Haoyuelectronics | [datasheet](http://www.haoyuelectronics.com/Attachment/MQ-9/MQ9.pdf) |
|
||||
| MQ-131 | Sensorsportal | [datasheet](http://www.sensorsportal.com/DOWNLOADS/MQ131.pdf) |
|
||||
| MQ-2 | HANWEI Electronics| [datasheet](https://www.pololu.com/file/0J309/MQ2.pdf) |
|
||||
| MQ-3 | HANWEI Electronics | [datasheet](https://www.sparkfun.com/datasheets/Sensors/MQ-3.pdf) |
|
||||
| MQ-4 | HANWEI Electronics | [datasheet](https://www.sparkfun.com/datasheets/Sensors/Biometric/MQ-4.pdf) |
|
||||
| MQ-5 | HANWEI Electronics | [datasheet](https://www.parallax.com/sites/default/files/downloads/605-00009-MQ-5-Datasheet.pdf) |
|
||||
| MQ-6 | HANWEI Electronics | [datasheet](https://www.sparkfun.com/datasheets/Sensors/Biometric/MQ-6.pdf) |
|
||||
| MQ-7 | HANWEI Electronics | [datasheet](https://www.sparkfun.com/datasheets/Sensors/Biometric/MQ-7.pdf) |
|
||||
| MQ-8 | HANWEI Electronics | [datasheet](https://dlnmh9ip6v2uc.cloudfront.net/datasheets/Sensors/Biometric/MQ-8.pdf) |
|
||||
| MQ-9 | HANWEI Electronics | [datasheet](http://www.haoyuelectronics.com/Attachment/MQ-9/MQ9.pdf) |
|
||||
| MQ-131 | HANWEI Electronics | [datasheet](http://www.sensorsportal.com/DOWNLOADS/MQ131.pdf) |
|
||||
| MQ-135 | HANWEI Electronics | [datasheet](https://www.electronicoscaldas.com/datasheet/MQ-135_Hanwei.pdf) |
|
||||
| MQ-136 | HANWEI Electronics | [datasheet](https://github.com/miguel5612/MQSensorsLib_Docs/blob/master/Datasheets/MQ136%20-%20Hanwei.pdf) |
|
||||
| MQ-303A | HANWEI Electronics | [datasheet](http://www.kosmodrom.com.ua/pdf/MQ303A.pdf) |
|
||||
| MQ-309A | HANWEI Electronics | [datasheet](http://www.sensorica.ru/pdf/MQ-309A.pdf) |
|
||||
|
||||
@ -169,10 +174,18 @@ Examples/MQ-board.ino
|
||||
* [Data sheets](https://github.com/miguel5612/MQSensorsLib_Docs/tree/master/Datasheets) - Curves and behavior for each sensor, using logarithmic graphs.
|
||||
* [Main purpose](https://github.com/miguel5612/MQSensorsLib_Docs/blob/master/static/img/bg.jpg) - Every sensor has high sensibility for a specific gas or material.
|
||||
|
||||
## Issues
|
||||
|
||||
Consulte [docs/issues.md](docs/issues.md) para un resumen de los problemas abiertos y sus soluciones.
|
||||
|
||||
## Contributing
|
||||
|
||||
Please read [CONTRIBUTING.md](https://github.com/miguel5612/MQSensorsLib/blob/master/CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us.
|
||||
|
||||
## Reviewers
|
||||
|
||||
* **PhD. Jacipt A Ramón V.** - [*GitHub*]() - [CV](https://scienti.minciencias.gov.co/cvlac/visualizador/generarCurriculoCv.do?cod_rh=0000512702)
|
||||
|
||||
## Authors
|
||||
|
||||
* **Miguel A. Califa U.** - [*GitHub*](https://github.com/miguel5612) - [CV](https://scienti.colciencias.gov.co/cvlac/visualizador/generarCurriculoCv.do?cod_rh=0000050477)
|
||||
@ -236,6 +249,10 @@ This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md
|
||||
}
|
||||
|
||||
|
||||
## Sponsor
|
||||
|
||||
* [Paypal](https://www.paypal.com/paypalme/miguel5612)
|
||||
|
||||
|
||||
<!-- MARKDOWN LINKS & IMAGES -->
|
||||
<!-- https://www.markdownguide.org/basic-syntax/#reference-style-links -->
|
||||
|
33
docs/issues.md
Normal file
33
docs/issues.md
Normal file
@ -0,0 +1,33 @@
|
||||
# Issues y soluciones
|
||||
|
||||
Este documento resume los problemas reportados en el repositorio y las soluciones propuestas o implementadas.
|
||||
|
||||
## Abiertos
|
||||
|
||||
### #75 MQ-3 sensor and CH4 gas reading 'ovf' failure
|
||||
**Estado:** resuelto en la rama `main`
|
||||
|
||||
El usuario reporta desbordamiento ("ovf") al utilizar valores muy altos en `setA` y `setB` para leer CH4 con un sensor MQ-3. La solución propuesta es revisar los valores utilizados en `setA` y `setB`, ya que `2*10^31` en C++ no corresponde a `2e31`. Se recomienda usar notación exponencial (`2e31`) o `pow(10,31)` y comprobar que los parámetros no excedan el rango de `float`.
|
||||
|
||||
**Actualización:** se añadieron validaciones de entrada, predicción de desbordamiento mediante logaritmos y verificación del resultado final para evitar valores "ovf" incluso con coeficientes muy altos. Las nuevas funciones limitan `setA` y `setB` y emplean cálculos en doble precisión.
|
||||
|
||||
### #74 possible error in the calculation formula for `_RS_Calc`
|
||||
**Estado:** resuelto en la rama `work`
|
||||
|
||||
Se detectó que la resistencia del sensor se calculaba con `_VOLT_RESOLUTION` en lugar del voltaje de alimentación real. Se añadieron los métodos `setVCC` y `getVCC` y se modificaron las ecuaciones para usar `VCC`. Esta corrección se refleja en la versión 3.0.1 de la biblioteca.
|
||||
|
||||
### #70 Parameters to model temperature and humidity dependence
|
||||
**Estado:** resuelto en la rama `work`
|
||||
|
||||
Se añadieron variables opcionales de "correction factor" en todos los ejemplos y se extendieron las funciones `calibrate` y `readSensorR0Rs` para aceptar este parámetro opcional. Así, el usuario puede ajustar las lecturas en función de temperatura y humedad cuando el datasheet lo permita. Los coeficientes deben consultarse para cada sensor.
|
||||
|
||||
### #67 Sensor won't finish the Calibration process if done in clean air
|
||||
**Estado:** abierto
|
||||
|
||||
Se reporta que la calibración se detiene mostrando un mensaje de "Conection issue" cuando se intenta calibrar el sensor MQ-135 en aire limpio. La recomendación del mantenedor es revisar la conexión física y probar el sensor con un programa básico para asegurar su correcto funcionamiento antes de usar la librería. Aún no se ha implementado un cambio en el código.
|
||||
|
||||
|
||||
## Cerrados destacados
|
||||
|
||||
Para obtener más información sobre todos los issues cerrados, consulte la página de [Issues en GitHub](https://github.com/miguel5612/MQSensorsLib/issues?q=is%3Aissue+is%3Aclosed).
|
||||
|
@ -15,7 +15,7 @@
|
||||
|
||||
Wiring:
|
||||
https://github.com/miguel5612/MQSensorsLib_Docs/blob/master/static/img/MQ_Arduino.PNG
|
||||
Please take care, arduino A0 pin represent the analog input configured on #define pin
|
||||
Please make sure arduino A0 pin represents the analog input configured on #define pin
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
@ -42,7 +42,7 @@ void setup() {
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ3.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ3.setA(0.3934); MQ3.setB(-1.504); // Configurate the ecuation values to get Alcohol concentration
|
||||
MQ3.setA(0.3934); MQ3.setB(-1.504); //Configure the equation to calculate Alcohol concentration value
|
||||
/*
|
||||
Exponential regression:
|
||||
Gas | a | b
|
||||
@ -64,30 +64,31 @@ void setup() {
|
||||
MQ3.init();
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
// Explanation:
|
||||
// In this routine the sensor will measure the resistance of the sensor supposing before was pre-heated
|
||||
// and now is on clean air (Calibration conditions), and it will setup R0 value.
|
||||
// We recomend execute this routine only on setup or on the laboratory and save on the eeprom of your arduino
|
||||
// This routine not need to execute to every restart, you can load your R0 if you know the value
|
||||
// In this routine the sensor will measure the resistance of the sensor supposedly before being pre-heated
|
||||
// and on clean air (Calibration conditions), setting up R0 value.
|
||||
// We recommend executing this routine only on setup in laboratory conditions.
|
||||
// This routine does not need to be executed on each restart, you can load your R0 value from eeprom.
|
||||
// Acknowledgements: https://jayconsystems.com/blog/understanding-a-gas-sensor
|
||||
Serial.print("Calibrating please wait.");
|
||||
float calcR0 = 0;
|
||||
for(int i = 1; i<=10; i ++)
|
||||
{
|
||||
MQ3.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ3.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
calcR0 += MQ3.calibrate(RatioMQ3CleanAir);
|
||||
Serial.print(".");
|
||||
}
|
||||
MQ3.setR0(calcR0/10);
|
||||
Serial.println(" done!.");
|
||||
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Conection issue founded, R0 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Conection issue founded, R0 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Connection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
}
|
||||
|
||||
void loop() {
|
||||
MQ3.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
alcoholPPM = MQ3.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
MQ3.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
float correctionFactor = 0; // Optional environmental correction
|
||||
alcoholPPM = MQ3.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
Serial.print("Alcohol now (PPM): ");
|
||||
Serial.println(alcoholPPM);
|
||||
delay(500); //Sampling frequency
|
||||
|
@ -47,7 +47,7 @@ void setup() {
|
||||
//Init serial port
|
||||
Serial.begin(115200);
|
||||
MQ2.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ2.setA(574.25); MQ2.setB(-2.222); // Configurate the ecuation values to get LPG concentration
|
||||
MQ2.setA(574.25); MQ2.setB(-2.222); // Configure the equation to to calculate LPG concentration
|
||||
/*
|
||||
//If the RL value is different from 10K please assign your RL value with the following method:
|
||||
MQ3.setRL(10);
|
||||
|
@ -40,7 +40,7 @@ void setup() {
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ3.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ3.setA(4.8387); MQ3.setB(-2.68); // Configurate the ecuation values to get Benzene concentration
|
||||
MQ3.setA(4.8387); MQ3.setB(-2.68); // Configure the equation to to calculate Benzene concentration
|
||||
/*
|
||||
Exponential regression:
|
||||
Gas | a | b
|
||||
@ -63,31 +63,32 @@ void setup() {
|
||||
*/
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
// Explanation:
|
||||
// In this routine the sensor will measure the resistance of the sensor supposing before was pre-heated
|
||||
// and now is on clean air (Calibration conditions), and it will setup R0 value.
|
||||
// We recomend execute this routine only on setup or on the laboratory and save on the eeprom of your arduino
|
||||
// This routine not need to execute to every restart, you can load your R0 if you know the value
|
||||
// In this routine the sensor will measure the resistance of the sensor supposedly before being pre-heated
|
||||
// and on clean air (Calibration conditions), setting up R0 value.
|
||||
// We recommend executing this routine only on setup in laboratory conditions.
|
||||
// This routine does not need to be executed on each restart, you can load your R0 value from eeprom.
|
||||
// Acknowledgements: https://jayconsystems.com/blog/understanding-a-gas-sensor
|
||||
Serial.print("Calibrating please wait.");
|
||||
float calcR0 = 0;
|
||||
for(int i = 1; i<=10; i ++)
|
||||
{
|
||||
MQ3.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ3.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
calcR0 += MQ3.calibrate(RatioMQ3CleanAir);
|
||||
Serial.print(".");
|
||||
}
|
||||
MQ3.setR0(calcR0/10);
|
||||
Serial.println(" done!.");
|
||||
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Conection issue founded, R0 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Conection issue founded, R0 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Connection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
MQ3.serialDebug(true);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
MQ3.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ3.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
MQ3.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
float correctionFactor = 0; // Optional environmental correction
|
||||
MQ3.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
MQ3.serialDebug(); // Will print the table on the serial port
|
||||
delay(500); //Sampling frequency
|
||||
}
|
137
examples/ESP32/ESP32_WROOM_32D/ESP32_WROOM_32D.ino
Normal file
137
examples/ESP32/ESP32_WROOM_32D/ESP32_WROOM_32D.ino
Normal file
@ -0,0 +1,137 @@
|
||||
/*
|
||||
* This sketch fixes the MQ2 measuring issue when using Wifi.h
|
||||
* Wifi.h deactivates pin 25 analogical input on esp32.ino default sketch.
|
||||
* This is because Wifi.h works only with ADC1 for analogical measurement
|
||||
* Please check the esp32-wroom-32d.jpg image on ESP32 folder
|
||||
*
|
||||
|
||||
* ADC1 GPIO
|
||||
* ADC1_CH0 (GPIO 36) // only tested on this and it works as expected :)
|
||||
* ADC1_CH1 (GPIO 37)
|
||||
* ADC1_CH2 (GPIO 38)
|
||||
* ADC1_CH3 (GPIO 39)
|
||||
* ADC1_CH4 (GPIO 32)
|
||||
* ADC1_CH5 (GPIO 33)
|
||||
* ADC1_CH6 (GPIO 34)
|
||||
* ADC1_CH7 (GPIO 35)
|
||||
*
|
||||
* ADC2 GPIO
|
||||
* ADC2_CH0 (GPIO 4)
|
||||
* ADC2_CH1 (GPIO 0)
|
||||
* ADC2_CH2 (GPIO 2)
|
||||
* ADC2_CH3 (GPIO 15)
|
||||
* ADC2_CH4 (GPIO 13)
|
||||
* ADC2_CH5 (GPIO 12)
|
||||
* ADC2_CH6 (GPIO 14)
|
||||
* ADC2_CH7 (GPIO 27)
|
||||
* ADC2_CH8 (GPIO 25)
|
||||
* ADC2_CH9 (GPIO 26)
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <WiFi.h>
|
||||
|
||||
//Include the library
|
||||
#include <MQUnifiedsensor.h>
|
||||
/************************Hardware Related Macros************************************/
|
||||
#define Board ("ESP-32") // Wemos ESP-32 or other board, whatever have ESP32 core.
|
||||
|
||||
//https://www.amazon.com/HiLetgo-ESP-WROOM-32-Development-Microcontroller-Integrated/dp/B0718T232Z (Although Amazon shows ESP-WROOM-32 ESP32 ESP-32S, the board is the ESP-WROOM-32D)
|
||||
#define Pin (36) //check the esp32-wroom-32d.jpg image on ESP32 folder
|
||||
|
||||
/***********************Software Related Macros************************************/
|
||||
#define Type ("MQ-2") //MQ2 or other MQ Sensor, if change this verify your a and b values.
|
||||
#define Voltage_Resolution (3.3) // 3V3 <- IMPORTANT. Source: https://randomnerdtutorials.com/esp32-adc-analog-read-arduino-ide/
|
||||
#define ADC_Bit_Resolution (12) // ESP-32 bit resolution. Source: https://randomnerdtutorials.com/esp32-adc-analog-read-arduino-ide/
|
||||
#define RatioMQ2CleanAir (9.83) //RS / R0 = 9.83 ppm
|
||||
/*****************************Globals***********************************************/
|
||||
MQUnifiedsensor MQ2(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin, Type);
|
||||
/*****************************Globals***********************************************/
|
||||
|
||||
const char* ssid = "your-ssid";
|
||||
const char* password = "your-password";
|
||||
|
||||
void setup()
|
||||
{
|
||||
|
||||
//Init the serial port communication - to debug the library
|
||||
Serial.begin(115200); //Init serial port
|
||||
delay(10);
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ2.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ2.setA(987.99); MQ2.setB(-2.162); // Configure the equation to to calculate H2 concentration
|
||||
|
||||
/*
|
||||
Exponential regression:
|
||||
Gas | a | b
|
||||
H2 | 987.99 | -2.162
|
||||
LPG | 574.25 | -2.222
|
||||
CO | 36974 | -3.109
|
||||
Alcohol| 3616.1 | -2.675
|
||||
Propane| 658.71 | -2.168
|
||||
*/
|
||||
|
||||
/***************************** MQ Init ********************************************/
|
||||
//Remarks: Configure the pin of arduino as input.
|
||||
/************************************************************************************/
|
||||
MQ2.init();
|
||||
|
||||
/*
|
||||
//If the RL value is different from 10K please assign your RL value with the following method:
|
||||
MQ2.setRL(10);
|
||||
*/
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
// Explanation:
|
||||
// In this routine the sensor will measure the resistance of the sensor supposedly before being pre-heated
|
||||
// and on clean air (Calibration conditions), setting up R0 value.
|
||||
// We recommend executing this routine only on setup in laboratory conditions.
|
||||
// This routine does not need to be executed on each restart, you can load your R0 value from eeprom.
|
||||
// Acknowledgements: https://jayconsystems.com/blog/understanding-a-gas-sensor
|
||||
Serial.print("Calibrating please wait.");
|
||||
float calcR0 = 0;
|
||||
for(int i = 1; i<=10; i ++)
|
||||
{
|
||||
MQ2.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
calcR0 += MQ2.calibrate(RatioMQ2CleanAir);
|
||||
Serial.print(".");
|
||||
}
|
||||
MQ2.setR0(calcR0/10);
|
||||
Serial.println(" done!.");
|
||||
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Connection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
//MQ2.serialDebug(true); uncomment if you want to print the table on the serial port
|
||||
|
||||
// We start by connecting to a WiFi network
|
||||
|
||||
Serial.println();
|
||||
Serial.println();
|
||||
Serial.print("Connecting to ");
|
||||
Serial.println(ssid);
|
||||
|
||||
WiFi.begin(ssid, password);
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
|
||||
Serial.println("");
|
||||
Serial.println("WiFi connected");
|
||||
Serial.println("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
MQ2.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
//MQ2.serialDebug(); // Will print the table on the serial port
|
||||
float correctionFactor = 0; // Optional environmental correction
|
||||
Serial.print(MQ2.readSensor(false, correctionFactor)); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
Serial.println(" PPM");
|
||||
delay(500); //Sampling frequency
|
||||
}
|
||||
|
BIN
examples/ESP32/ESP32_WROOM_32D/images/esp-32-wroom-32d-board.jpg
Normal file
BIN
examples/ESP32/ESP32_WROOM_32D/images/esp-32-wroom-32d-board.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 32 KiB |
BIN
examples/ESP32/ESP32_WROOM_32D/images/esp32-wroom-32d-pinout.jpg
Normal file
BIN
examples/ESP32/ESP32_WROOM_32D/images/esp32-wroom-32d-pinout.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 484 KiB |
@ -40,7 +40,7 @@ void setup() {
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ3.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ3.setA(4.8387); MQ3.setB(-2.68); // Configurate the ecuation values to get Benzene concentration
|
||||
MQ3.setA(4.8387); MQ3.setB(-2.68); // Configure the equation to to calculate Benzene concentration
|
||||
/*
|
||||
Exponential regression:
|
||||
Gas | a | b
|
||||
@ -63,31 +63,32 @@ void setup() {
|
||||
*/
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
// Explanation:
|
||||
// In this routine the sensor will measure the resistance of the sensor supposing before was pre-heated
|
||||
// and now is on clean air (Calibration conditions), and it will setup R0 value.
|
||||
// We recomend execute this routine only on setup or on the laboratory and save on the eeprom of your arduino
|
||||
// This routine not need to execute to every restart, you can load your R0 if you know the value
|
||||
// In this routine the sensor will measure the resistance of the sensor supposedly before being pre-heated
|
||||
// and on clean air (Calibration conditions), setting up R0 value.
|
||||
// We recommend executing this routine only on setup in laboratory conditions.
|
||||
// This routine does not need to be executed on each restart, you can load your R0 value from eeprom.
|
||||
// Acknowledgements: https://jayconsystems.com/blog/understanding-a-gas-sensor
|
||||
Serial.print("Calibrating please wait.");
|
||||
float calcR0 = 0;
|
||||
for(int i = 1; i<=10; i ++)
|
||||
{
|
||||
MQ3.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ3.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
calcR0 += MQ3.calibrate(RatioMQ3CleanAir);
|
||||
Serial.print(".");
|
||||
}
|
||||
MQ3.setR0(calcR0/10);
|
||||
Serial.println(" done!.");
|
||||
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Conection issue founded, R0 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Conection issue founded, R0 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Connection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
MQ3.serialDebug(true);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
MQ3.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ3.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
MQ3.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
float correctionFactor = 0; // Optional environmental correction
|
||||
MQ3.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
MQ3.serialDebug(); // Will print the table on the serial port
|
||||
delay(500); //Sampling frequency
|
||||
}
|
@ -43,24 +43,24 @@ void setup() {
|
||||
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
// Explanation:
|
||||
// In this routine the sensor will measure the resistance of the sensor supposing before was pre-heated
|
||||
// and now is on clean air (Calibration conditions), and it will setup R0 value.
|
||||
// We recomend execute this routine only on setup or on the laboratory and save on the eeprom of your arduino
|
||||
// This routine not need to execute to every restart, you can load your R0 if you know the value
|
||||
// In this routine the sensor will measure the resistance of the sensor supposedly before being pre-heated
|
||||
// and on clean air (Calibration conditions), setting up R0 value.
|
||||
// We recommend executing this routine only on setup in laboratory conditions.
|
||||
// This routine does not need to be executed on each restart, you can load your R0 value from eeprom.
|
||||
// Acknowledgements: https://jayconsystems.com/blog/understanding-a-gas-sensor
|
||||
Serial.print("Calibrating please wait.");
|
||||
float calcR0 = 0;
|
||||
for(int i = 1; i<=10; i ++)
|
||||
{
|
||||
MQ4.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ4.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
calcR0 += MQ4.calibrate(RatioMQ4CleanAir);
|
||||
Serial.print(".");
|
||||
}
|
||||
MQ4.setR0(calcR0/10);
|
||||
Serial.println(" done!.");
|
||||
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Conection issue founded, R0 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Conection issue founded, R0 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Connection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
|
||||
/*
|
||||
@ -75,23 +75,24 @@ void setup() {
|
||||
/************************************************************************************/
|
||||
MQ4.init();
|
||||
|
||||
Serial.println("** Lectures from MQ-4**********");
|
||||
Serial.println("** Values from MQ-4**********");
|
||||
Serial.println("| LPG (Linear EQ) | LPG (Exponential EQ) |");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
MQ4.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ4.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
float correctionFactor = 0; // Optional environmental correction
|
||||
|
||||
//https://jayconsystems.com/blog/understanding-a-gas-sensor
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ4.setRegressionMethod(0); //_PPM = pow(10, (log10(ratio)-b)/a)
|
||||
MQ4.setA(-0.318); MQ4.setB(1.133); // A -> Slope, B -> Intersect with X - Axis
|
||||
float LPG1 = MQ4.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
float LPG1 = MQ4.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ4.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ4.setA(1012.7); MQ4.setB(-2.786); // Configurate the ecuation values to get CH4 concentration
|
||||
float LPG2 = MQ4.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
MQ4.setA(1012.7); MQ4.setB(-2.786); // Configure the equation to to calculate CH4 concentration
|
||||
float LPG2 = MQ4.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
|
||||
// exposure to 2000 ppm of LPG gas is immediately dangerous to life and health. In this section
|
||||
if(LPG1>=2000 || LPG2>=2000) Serial.println("Warning - Very high concentrations detected!");
|
||||
|
@ -15,7 +15,7 @@
|
||||
|
||||
Wiring:
|
||||
https://github.com/miguel5612/MQSensorsLib_Docs/blob/master/static/img/MQ_Arduino.PNG
|
||||
Please take care, arduino A0 pin represent the analog input configured on #define pin
|
||||
Please make sure arduino A0 pin represents the analog input configured on #define pin
|
||||
|
||||
Note: high concentration MQ-131 sensor.
|
||||
|
||||
@ -44,7 +44,7 @@ void setup() {
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ131.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ131.setA(23.943); MQ131.setB(-1.11); // Configurate the ecuation values to get O3 concentration
|
||||
MQ131.setA(23.943); MQ131.setB(-1.11); // Configure the equation to to calculate O3 concentration
|
||||
|
||||
/*
|
||||
Exponential regression:
|
||||
@ -65,32 +65,32 @@ void setup() {
|
||||
*/
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
// Explanation:
|
||||
// In this routine the sensor will measure the resistance of the sensor supposing before was pre-heated
|
||||
// and now is on clean air (Calibration conditions), and it will setup R0 value.
|
||||
// We recomend execute this routine only on setup or on the laboratory and save on the eeprom of your arduino
|
||||
// This routine not need to execute to every restart, you can load your R0 if you know the value
|
||||
// In this routine the sensor will measure the resistance of the sensor supposedly before being pre-heated
|
||||
// and on clean air (Calibration conditions), setting up R0 value.
|
||||
// We recommend executing this routine only on setup in laboratory conditions.
|
||||
// This routine does not need to be executed on each restart, you can load your R0 value from eeprom.
|
||||
// Acknowledgements: https://jayconsystems.com/blog/understanding-a-gas-sensor
|
||||
Serial.print("Calibrating please wait.");
|
||||
float calcR0 = 0;
|
||||
for(int i = 1; i<=10; i ++)
|
||||
{
|
||||
MQ131.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ131.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
calcR0 += MQ131.calibrate(RatioMQ131CleanAir);
|
||||
Serial.print(".");
|
||||
}
|
||||
MQ131.setR0(calcR0/10);
|
||||
Serial.println(" done!.");
|
||||
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Conection issue founded, R0 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Conection issue founded, R0 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Connection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
MQ131.serialDebug(true);
|
||||
Serial.println("Ignore Ratio = RS/R0, for this example we will use readSensorR0Rs, the ratio calculated will be R0/Rs. Thanks :)");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
MQ131.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ131.readSensorR0Rs(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
MQ131.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
MQ131.readSensorR0Rs(); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
MQ131.serialDebug(); // Will print the table on the serial port
|
||||
delay(500); //Sampling frequency
|
||||
}
|
@ -15,7 +15,7 @@
|
||||
|
||||
Wiring:
|
||||
https://github.com/miguel5612/MQSensorsLib_Docs/blob/master/static/img/MQ_Arduino.PNG
|
||||
Please take care, arduino A0 pin represent the analog input configured on #define pin
|
||||
Please make sure arduino A0 pin represents the analog input configured on #define pin
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
@ -53,56 +53,63 @@ void setup() {
|
||||
*/
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
// Explanation:
|
||||
// In this routine the sensor will measure the resistance of the sensor supposing before was pre-heated
|
||||
// and now is on clean air (Calibration conditions), and it will setup R0 value.
|
||||
// We recomend execute this routine only on setup or on the laboratory and save on the eeprom of your arduino
|
||||
// This routine not need to execute to every restart, you can load your R0 if you know the value
|
||||
// In this routine the sensor will measure the resistance of the sensor supposedly before being pre-heated
|
||||
// and on clean air (Calibration conditions), setting up R0 value.
|
||||
// We recommend executing this routine only on setup in laboratory conditions.
|
||||
// This routine does not need to be executed on each restart, you can load your R0 value from eeprom.
|
||||
// Acknowledgements: https://jayconsystems.com/blog/understanding-a-gas-sensor
|
||||
Serial.print("Calibrating please wait.");
|
||||
float calcR0 = 0;
|
||||
for(int i = 1; i<=10; i ++)
|
||||
{
|
||||
MQ135.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ135.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
calcR0 += MQ135.calibrate(RatioMQ135CleanAir);
|
||||
Serial.print(".");
|
||||
}
|
||||
MQ135.setR0(calcR0/10);
|
||||
Serial.println(" done!.");
|
||||
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Conection issue founded, R0 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Conection issue founded, R0 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Connection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
Serial.println("** Lectures from MQ-135 ****");
|
||||
Serial.println("| CO | Alcohol | CO2 | Tolueno | NH4 | Acteona |");
|
||||
Serial.println("** Values from MQ-135 ****");
|
||||
Serial.println("| CO | Alcohol | CO2 | Toluen | NH4 | Aceton |");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
MQ135.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ135.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
float correctionFactor = 0; // Optional environmental correction
|
||||
|
||||
MQ135.setA(605.18); MQ135.setB(-3.937); // Configurate the ecuation values to get CO concentration
|
||||
float CO = MQ135.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
MQ135.setA(605.18); MQ135.setB(-3.937); // Configure the equation to calculate CO concentration value
|
||||
float CO = MQ135.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
|
||||
MQ135.setA(77.255); MQ135.setB(-3.18); // Configurate the ecuation values to get Alcohol concentration
|
||||
float Alcohol = MQ135.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
MQ135.setA(77.255); MQ135.setB(-3.18); //Configure the equation to calculate Alcohol concentration value
|
||||
float Alcohol = MQ135.readSensor(false, correctionFactor); // SSensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
|
||||
MQ135.setA(110.47); MQ135.setB(-2.862); // Configurate the ecuation values to get CO2 concentration
|
||||
float CO2 = MQ135.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
MQ135.setA(110.47); MQ135.setB(-2.862); // Configure the equation to calculate CO2 concentration value
|
||||
float CO2 = MQ135.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
|
||||
MQ135.setA(44.947); MQ135.setB(-3.445); // Configurate the ecuation values to get Tolueno concentration
|
||||
float Tolueno = MQ135.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
|
||||
MQ135.setA(102.2 ); MQ135.setB(-2.473); // Configurate the ecuation values to get NH4 concentration
|
||||
float NH4 = MQ135.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
|
||||
MQ135.setA(34.668); MQ135.setB(-3.369); // Configurate the ecuation values to get Acetona concentration
|
||||
float Acetona = MQ135.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
MQ135.setA(44.947); MQ135.setB(-3.445); // Configure the equation to calculate Toluen concentration value
|
||||
float Toluen = MQ135.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
|
||||
MQ135.setA(102.2 ); MQ135.setB(-2.473); // Configure the equation to calculate NH4 concentration value
|
||||
float NH4 = MQ135.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
|
||||
MQ135.setA(34.668); MQ135.setB(-3.369); // Configure the equation to calculate Aceton concentration value
|
||||
float Aceton = MQ135.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
Serial.print("| "); Serial.print(CO);
|
||||
Serial.print(" | "); Serial.print(Alcohol);
|
||||
Serial.print(" | "); Serial.print(CO2);
|
||||
Serial.print(" | "); Serial.print(Tolueno);
|
||||
Serial.print(" | "); Serial.print(Alcohol);
|
||||
// Note: 400 Offset for CO2 source: https://github.com/miguel5612/MQSensorsLib/issues/29
|
||||
/*
|
||||
Motivation:
|
||||
We have added 400 PPM because when the library is calibrated it assumes the current state of the
|
||||
air as 0 PPM, and it is considered today that the CO2 present in the atmosphere is around 400 PPM.
|
||||
https://www.lavanguardia.com/natural/20190514/462242832581/concentracion-dioxido-cabono-co2-atmosfera-bate-record-historia-humanidad.html
|
||||
*/
|
||||
Serial.print(" | "); Serial.print(CO2 + 400);
|
||||
Serial.print(" | "); Serial.print(Toluen);
|
||||
Serial.print(" | "); Serial.print(NH4);
|
||||
Serial.print(" | "); Serial.print(Acetona);
|
||||
Serial.print(" | "); Serial.print(Aceton);
|
||||
Serial.println(" |");
|
||||
/*
|
||||
Exponential regression:
|
||||
@ -110,10 +117,10 @@ void loop() {
|
||||
CO | 605.18 | -3.937
|
||||
Alcohol | 77.255 | -3.18
|
||||
CO2 | 110.47 | -2.862
|
||||
Tolueno | 44.947 | -3.445
|
||||
Toluen | 44.947 | -3.445
|
||||
NH4 | 102.2 | -2.473
|
||||
Acetona | 34.668 | -3.369
|
||||
Aceton | 34.668 | -3.369
|
||||
*/
|
||||
|
||||
delay(500); //Sampling frequency
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,183 @@
|
||||
/*
|
||||
MQUnifiedsensor Library - reading an MQ135
|
||||
|
||||
Demonstrates the use a MQ135 sensor.
|
||||
Library originally added 01 may 2019
|
||||
by Miguel A Califa, Yersson Carrillo, Ghiordy Contreras, Mario Rodriguez
|
||||
|
||||
Added example
|
||||
modified 23 May 2019
|
||||
by Miguel Califa
|
||||
|
||||
Updated library usage
|
||||
modified 26 March 2020
|
||||
by Miguel Califa
|
||||
|
||||
Wiring:
|
||||
https://github.com/miguel5612/MQSensorsLib_Docs/blob/master/static/img/MQ_Arduino.PNG
|
||||
Please make sure arduino A0 pin represents the analog input configured on #define pin
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
*/
|
||||
|
||||
//Include the library
|
||||
#include <MQUnifiedsensor.h>
|
||||
#include <Adafruit_Sensor.h>
|
||||
#include <DHT.h>
|
||||
#include <DHT_U.h>
|
||||
|
||||
//Definitions
|
||||
#define placa "Arduino UNO"
|
||||
#define Voltage_Resolution 5
|
||||
#define pin A0 //Analog input 0 of your arduino
|
||||
#define type "MQ-135" //MQ135
|
||||
#define ADC_Bit_Resolution 10 // For arduino UNO/MEGA/NANO
|
||||
#define RatioMQ135CleanAir 3.6//RS / R0 = 3.6 ppm
|
||||
//#define calibration_button 13 //Pin to calibrate your sensor
|
||||
|
||||
#define MQ135_DEFAULTPPM 399 //default ppm of CO2 for calibration
|
||||
#define MQ135_DEFAULTRO 68550 //default Ro for MQ135_DEFAULTPPM ppm of CO2
|
||||
#define MQ135_SCALINGFACTOR 116.6020682 //CO2 gas value
|
||||
#define MQ135_EXPONENT -2.769034857 //CO2 gas value
|
||||
#define MQ135_MAXRSRO 2.428 //for CO2
|
||||
#define MQ135_MINRSRO 0.358 //for CO2
|
||||
|
||||
|
||||
/// Parameters for calculating ppm of CO2 from sensor resistance
|
||||
#define PARA 116.6020682
|
||||
#define PARB 2.769034857
|
||||
|
||||
/// Parameters to model temperature and humidity dependence
|
||||
#define CORA 0.00035
|
||||
#define CORB 0.02718
|
||||
#define CORC 1.39538
|
||||
#define CORD 0.0018
|
||||
|
||||
#define DHTPIN 2 // Digital pin connected to the DHT sensor
|
||||
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
|
||||
// Pin 15 can work but DHT must be disconnected during program upload.
|
||||
|
||||
// Uncomment the type of sensor in use:
|
||||
//#define DHTTYPE DHT11 // DHT 11
|
||||
#define DHTTYPE DHT22 // DHT 22 (AM2302)
|
||||
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
|
||||
|
||||
// See guide for details on sensor wiring and usage:
|
||||
// https://learn.adafruit.com/dht/overview
|
||||
|
||||
DHT_Unified dht(DHTPIN, DHTTYPE);
|
||||
|
||||
uint32_t delayMS;
|
||||
|
||||
//Declare Sensor
|
||||
MQUnifiedsensor MQ135(placa, Voltage_Resolution, ADC_Bit_Resolution, pin, type);
|
||||
|
||||
void setup() {
|
||||
//Init the serial port communication - to debug the library
|
||||
Serial.begin(9600); //Init serial port
|
||||
dht.begin();
|
||||
sensor_t sensor;
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ135.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ135.setA(102.2); MQ135.setB(-2.473); // Configure the equation to to calculate NH4 concentration
|
||||
|
||||
/*
|
||||
Exponential regression:
|
||||
GAS | a | b
|
||||
CO | 605.18 | -3.937
|
||||
Alcohol | 77.255 | -3.18
|
||||
CO2 | 110.47 | -2.862
|
||||
Toluen | 44.947 | -3.445
|
||||
NH4 | 102.2 | -2.473
|
||||
Aceton | 34.668 | -3.369
|
||||
*/
|
||||
|
||||
/***************************** MQ Init ********************************************/
|
||||
//Remarks: Configure the pin of arduino as input.
|
||||
/************************************************************************************/
|
||||
MQ135.init();
|
||||
/*
|
||||
//If the RL value is different from 10K please assign your RL value with the following method:
|
||||
MQ135.setRL(10);
|
||||
*/
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
// Explanation:
|
||||
// In this routine the sensor will measure the resistance of the sensor supposedly before being pre-heated
|
||||
// and on clean air (Calibration conditions), setting up R0 value.
|
||||
// We recommend executing this routine only on setup in laboratory conditions.
|
||||
// This routine does not need to be executed on each restart, you can load your R0 value from eeprom.
|
||||
// Acknowledgements: https://jayconsystems.com/blog/understanding-a-gas-sensor
|
||||
Serial.print("Calibrating please wait.");
|
||||
float calcR0 = 0;
|
||||
for(int i = 1; i<=10; i ++)
|
||||
{
|
||||
MQ135.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
calcR0 += MQ135.calibrate(RatioMQ135CleanAir);
|
||||
Serial.print(".");
|
||||
}
|
||||
MQ135.setR0(calcR0/10);
|
||||
Serial.println(" done!.");
|
||||
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Connection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
MQ135.serialDebug(true);
|
||||
// Set delay between sensor readings based on sensor details.
|
||||
delayMS = sensor.min_delay / 1000;
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
delay(delayMS); //Sampling frequency
|
||||
|
||||
// if you want to apply corelation factor, you will add in this program the temperature and humidity sensor
|
||||
sensors_event_t event;
|
||||
dht.temperature().getEvent(&event);
|
||||
float cFactor = 0;
|
||||
if (!isnan(event.temperature) && !isnan(event.relative_humidity)) cFactor = getCorrectionFactor(event.temperature, event.relative_humidity);
|
||||
Serial.print("Correction Factor: "); Serial.println(cFactor);
|
||||
MQ135.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
MQ135.readSensor(false, cFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
MQ135.serialDebug(); // Will print the table on the serial port
|
||||
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@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 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 corrected
|
||||
for temp/hum
|
||||
@param[in] t The ambient air temperature
|
||||
@param[in] h The relative humidity
|
||||
@return The corrected sensor resistance kOhm
|
||||
*/
|
||||
/**************************************************************************/
|
||||
float getCorrectedResistance(long resvalue, float t, float h) {
|
||||
return resvalue/getCorrectionFactor(t, h);
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@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 getCorrectedPPM(long resvalue,float t, float h, long ro) {
|
||||
return PARA * pow((getCorrectedResistance(resvalue, t, h)/ro), -PARB);
|
||||
}
|
@ -15,7 +15,7 @@
|
||||
|
||||
Wiring:
|
||||
https://github.com/miguel5612/MQSensorsLib_Docs/blob/master/static/img/MQ_Arduino.PNG
|
||||
Please take care, arduino A0 pin represent the analog input configured on #define pin
|
||||
Please make sure arduino A0 pin represents the analog input configured on #define pin
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
@ -42,7 +42,7 @@ void setup() {
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ135.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ135.setA(102.2); MQ135.setB(-2.473); // Configurate the ecuation values to get NH4 concentration
|
||||
MQ135.setA(102.2); MQ135.setB(-2.473); // Configure the equation to to calculate NH4 concentration
|
||||
|
||||
/*
|
||||
Exponential regression:
|
||||
@ -50,9 +50,9 @@ void setup() {
|
||||
CO | 605.18 | -3.937
|
||||
Alcohol | 77.255 | -3.18
|
||||
CO2 | 110.47 | -2.862
|
||||
Tolueno | 44.947 | -3.445
|
||||
Toluen | 44.947 | -3.445
|
||||
NH4 | 102.2 | -2.473
|
||||
Acetona | 34.668 | -3.369
|
||||
Aceton | 34.668 | -3.369
|
||||
*/
|
||||
|
||||
/***************************** MQ Init ********************************************/
|
||||
@ -65,31 +65,32 @@ void setup() {
|
||||
*/
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
// Explanation:
|
||||
// In this routine the sensor will measure the resistance of the sensor supposing before was pre-heated
|
||||
// and now is on clean air (Calibration conditions), and it will setup R0 value.
|
||||
// We recomend execute this routine only on setup or on the laboratory and save on the eeprom of your arduino
|
||||
// This routine not need to execute to every restart, you can load your R0 if you know the value
|
||||
// In this routine the sensor will measure the resistance of the sensor supposedly before being pre-heated
|
||||
// and on clean air (Calibration conditions), setting up R0 value.
|
||||
// We recommend executing this routine only on setup in laboratory conditions.
|
||||
// This routine does not need to be executed on each restart, you can load your R0 value from eeprom.
|
||||
// Acknowledgements: https://jayconsystems.com/blog/understanding-a-gas-sensor
|
||||
Serial.print("Calibrating please wait.");
|
||||
float calcR0 = 0;
|
||||
for(int i = 1; i<=10; i ++)
|
||||
{
|
||||
MQ135.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ135.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
calcR0 += MQ135.calibrate(RatioMQ135CleanAir);
|
||||
Serial.print(".");
|
||||
}
|
||||
MQ135.setR0(calcR0/10);
|
||||
Serial.println(" done!.");
|
||||
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Conection issue founded, R0 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Conection issue founded, R0 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Connection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
MQ135.serialDebug(true);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
MQ135.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ135.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
MQ135.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
float correctionFactor = 0; // Optional environmental correction
|
||||
MQ135.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
MQ135.serialDebug(); // Will print the table on the serial port
|
||||
delay(500); //Sampling frequency
|
||||
}
|
89
examples/MQ-136/MQ-136.ino
Normal file
89
examples/MQ-136/MQ-136.ino
Normal file
@ -0,0 +1,89 @@
|
||||
/*
|
||||
MQUnifiedsensor Library - reading an MQ136
|
||||
|
||||
Demonstrates the use a MQ136 sensor.
|
||||
Library originally added 01 may 2019
|
||||
by Miguel A Califa, Yersson Carrillo, Ghiordy Contreras, Mario Rodriguez
|
||||
|
||||
Added example
|
||||
modified 21 March 2022
|
||||
by Miguel Califa
|
||||
|
||||
Wiring:
|
||||
https://github.com/miguel5612/MQSensorsLib_Docs/blob/master/static/img/MQ_Arduino.PNG
|
||||
Please make sure arduino A0 pin represents the analog input configured on #define pin
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
*/
|
||||
|
||||
//Include the library
|
||||
#include <MQUnifiedsensor.h>
|
||||
|
||||
//Definitions
|
||||
#define placa "Arduino UNO"
|
||||
#define Voltage_Resolution 5
|
||||
#define pin A0 //Analog input 0 of your arduino
|
||||
#define type "MQ-136" //MQ136
|
||||
#define ADC_Bit_Resolution 10 // For arduino UNO/MEGA/NANO
|
||||
#define RatioMQ136CleanAir 3.6//RS / R0 = 3.6 ppm
|
||||
//#define calibration_button 13 //Pin to calibrate your sensor
|
||||
|
||||
//Declare Sensor
|
||||
MQUnifiedsensor MQ136(placa, Voltage_Resolution, ADC_Bit_Resolution, pin, type);
|
||||
|
||||
void setup() {
|
||||
//Init the serial port communication - to debug the library
|
||||
Serial.begin(9600); //Init serial port
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ136.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ136.setA(36.737); MQ136.setB(-3.536); // Configure the equation to to calculate H2S Concentration
|
||||
|
||||
/*
|
||||
Exponential regression:
|
||||
GAS | a | b
|
||||
H2S | 36.737 | -3.536
|
||||
NH4 | 98.551 | -2.475
|
||||
CO | 503.34 | -3.774
|
||||
*/
|
||||
|
||||
/***************************** MQ Init ********************************************/
|
||||
//Remarks: Configure the pin of arduino as input.
|
||||
/************************************************************************************/
|
||||
MQ136.init();
|
||||
/*
|
||||
//If the RL value is different from 10K please assign your RL value with the following method:
|
||||
MQ136.setRL(10);
|
||||
*/
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
// Explanation:
|
||||
// In this routine the sensor will measure the resistance of the sensor supposedly before being pre-heated
|
||||
// and on clean air (Calibration conditions), setting up R0 value.
|
||||
// We recommend executing this routine only on setup in laboratory conditions.
|
||||
// This routine does not need to be executed on each restart, you can load your R0 value from eeprom.
|
||||
// Acknowledgements: https://jayconsystems.com/blog/understanding-a-gas-sensor
|
||||
Serial.print("Calibrating please wait.");
|
||||
float calcR0 = 0;
|
||||
for(int i = 1; i<=10; i ++)
|
||||
{
|
||||
MQ136.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
calcR0 += MQ136.calibrate(RatioMQ136CleanAir);
|
||||
Serial.print(".");
|
||||
}
|
||||
MQ136.setR0(calcR0/10);
|
||||
Serial.println(" done!.");
|
||||
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Connection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
MQ136.serialDebug(true);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
MQ136.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
float correctionFactor = 0; // Optional environmental correction
|
||||
MQ136.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
MQ136.serialDebug(); // Will print the table on the serial port
|
||||
delay(500); //Sampling frequency
|
||||
}
|
@ -14,7 +14,7 @@
|
||||
by Miguel Califa
|
||||
Wiring:
|
||||
https://github.com/miguel5612/MQSensorsLib_Docs/blob/master/static/img/MQ_Arduino.PNG
|
||||
Please take care, arduino A0 pin represent the analog input configured on #define pin
|
||||
Please make sure arduino A0 pin represents the analog input configured on #define pin
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
@ -41,7 +41,7 @@ void setup() {
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ2.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ2.setA(574.25); MQ2.setB(-2.222); // Configurate the ecuation values to get LPG concentration
|
||||
MQ2.setA(574.25); MQ2.setB(-2.222); // Configure the equation to to calculate LPG concentration
|
||||
/*
|
||||
Exponential regression:
|
||||
Gas | a | b
|
||||
@ -62,32 +62,33 @@ void setup() {
|
||||
*/
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
// Explanation:
|
||||
// In this routine the sensor will measure the resistance of the sensor supposing before was pre-heated
|
||||
// and now is on clean air (Calibration conditions), and it will setup R0 value.
|
||||
// We recomend execute this routine only on setup or on the laboratory and save on the eeprom of your arduino
|
||||
// This routine not need to execute to every restart, you can load your R0 if you know the value
|
||||
// In this routine the sensor will measure the resistance of the sensor supposedly before being pre-heated
|
||||
// and on clean air (Calibration conditions), setting up R0 value.
|
||||
// We recommend executing this routine only on setup in laboratory conditions.
|
||||
// This routine does not need to be executed on each restart, you can load your R0 value from eeprom.
|
||||
// Acknowledgements: https://jayconsystems.com/blog/understanding-a-gas-sensor
|
||||
Serial.print("Calibrating please wait.");
|
||||
float calcR0 = 0;
|
||||
for(int i = 1; i<=10; i ++)
|
||||
{
|
||||
MQ2.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ2.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
calcR0 += MQ2.calibrate(RatioMQ2CleanAir);
|
||||
Serial.print(".");
|
||||
}
|
||||
MQ2.setR0(calcR0/10);
|
||||
Serial.println(" done!.");
|
||||
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Conection issue founded, R0 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Conection issue founded, R0 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Connection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
|
||||
MQ2.serialDebug(true);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
MQ2.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ2.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
MQ2.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
float correctionFactor = 0; // Optional environmental correction
|
||||
MQ2.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
MQ2.serialDebug(); // Will print the table on the serial port
|
||||
delay(500); //Sampling frequency
|
||||
}
|
@ -15,7 +15,7 @@
|
||||
|
||||
Wiring:
|
||||
https://github.com/miguel5612/MQSensorsLib_Docs/blob/master/static/img/MQ_Arduino.PNG
|
||||
Please take care, arduino A0 pin represent the analog input configured on #define pin
|
||||
Please make sure arduino A0 pin represents the analog input configured on #define pin
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
@ -41,7 +41,7 @@ void setup() {
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ3.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ3.setA(4.8387); MQ3.setB(-2.68); // Configurate the ecuation values to get Benzene concentration
|
||||
MQ3.setA(4.8387); MQ3.setB(-2.68); // Configure the equation to to calculate Benzene concentration
|
||||
/*
|
||||
Exponential regression:
|
||||
Gas | a | b
|
||||
@ -64,31 +64,32 @@ void setup() {
|
||||
*/
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
// Explanation:
|
||||
// In this routine the sensor will measure the resistance of the sensor supposing before was pre-heated
|
||||
// and now is on clean air (Calibration conditions), and it will setup R0 value.
|
||||
// We recomend execute this routine only on setup or on the laboratory and save on the eeprom of your arduino
|
||||
// This routine not need to execute to every restart, you can load your R0 if you know the value
|
||||
// In this routine the sensor will measure the resistance of the sensor supposedly before being pre-heated
|
||||
// and on clean air (Calibration conditions), setting up R0 value.
|
||||
// We recommend executing this routine only on setup in laboratory conditions.
|
||||
// This routine does not need to be executed on each restart, you can load your R0 value from eeprom.
|
||||
// Acknowledgements: https://jayconsystems.com/blog/understanding-a-gas-sensor
|
||||
Serial.print("Calibrating please wait.");
|
||||
float calcR0 = 0;
|
||||
for(int i = 1; i<=10; i ++)
|
||||
{
|
||||
MQ3.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ3.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
calcR0 += MQ3.calibrate(RatioMQ3CleanAir);
|
||||
Serial.print(".");
|
||||
}
|
||||
MQ3.setR0(calcR0/10);
|
||||
Serial.println(" done!.");
|
||||
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Conection issue founded, R0 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Conection issue founded, R0 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Connection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
MQ3.serialDebug(true);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
MQ3.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ3.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
MQ3.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
float correctionFactor = 0; // Optional environmental correction
|
||||
MQ3.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
MQ3.serialDebug(); // Will print the table on the serial port
|
||||
delay(500); //Sampling frequency
|
||||
}
|
@ -15,7 +15,7 @@
|
||||
|
||||
Wiring:
|
||||
https://github.com/miguel5612/MQSensorsLib_Docs/blob/master/static/img/MQ_Arduino.PNG
|
||||
Please take care, arduino A0 pin represent the analog input configured on #define pin
|
||||
Please make sure arduino A0 pin represents the analog input configured on #define pin
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
@ -42,7 +42,7 @@ void setup() {
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ303.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ303.setA(6.2144); MQ303.setB(-2.894); // Configurate the ecuation values to get Isobutano concentration
|
||||
MQ303.setA(6.2144); MQ303.setB(-2.894); // Configure the equation to to calculate Isobutano concentration
|
||||
|
||||
/*
|
||||
Exponential regression:
|
||||
@ -62,31 +62,32 @@ void setup() {
|
||||
*/
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
// Explanation:
|
||||
// In this routine the sensor will measure the resistance of the sensor supposing before was pre-heated
|
||||
// and now is on clean air (Calibration conditions), and it will setup R0 value.
|
||||
// We recomend execute this routine only on setup or on the laboratory and save on the eeprom of your arduino
|
||||
// This routine not need to execute to every restart, you can load your R0 if you know the value
|
||||
// In this routine the sensor will measure the resistance of the sensor supposedly before being pre-heated
|
||||
// and on clean air (Calibration conditions), setting up R0 value.
|
||||
// We recommend executing this routine only on setup in laboratory conditions.
|
||||
// This routine does not need to be executed on each restart, you can load your R0 value from eeprom.
|
||||
// Acknowledgements: https://jayconsystems.com/blog/understanding-a-gas-sensor
|
||||
Serial.print("Calibrating please wait.");
|
||||
float calcR0 = 0;
|
||||
for(int i = 1; i<=10; i ++)
|
||||
{
|
||||
MQ303.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ303.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
calcR0 += MQ303.calibrate(RatioMQ303CleanAir);
|
||||
Serial.print(".");
|
||||
}
|
||||
MQ303.setR0(calcR0/10);
|
||||
Serial.println(" done!.");
|
||||
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Conection issue founded, R0 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Conection issue founded, R0 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Connection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
MQ303.serialDebug(true);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
MQ303.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ303.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
MQ303.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
float correctionFactor = 0; // Optional environmental correction
|
||||
MQ303.readSensor(true, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
MQ303.serialDebug(); // Will print the table on the serial port
|
||||
delay(500); //Sampling frequency
|
||||
}
|
@ -15,7 +15,7 @@
|
||||
|
||||
Wiring:
|
||||
https://github.com/miguel5612/MQSensorsLib_Docs/blob/master/static/img/MQ_Arduino.PNG
|
||||
Please take care, arduino A0 pin represent the analog input configured on #define pin
|
||||
Please make sure arduino A0 pin represents the analog input configured on #define pin
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
@ -44,7 +44,7 @@ void setup() {
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ309.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ309.setA(1000000); MQ309.setB(-4.01); // Configurate the ecuation values to get CO concentration
|
||||
MQ309.setA(1000000); MQ309.setB(-4.01); // Configure the equation to calculate CO concentration value
|
||||
|
||||
/*
|
||||
Exponential regression:
|
||||
@ -65,24 +65,24 @@ void setup() {
|
||||
*/
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
// Explanation:
|
||||
// In this routine the sensor will measure the resistance of the sensor supposing before was pre-heated
|
||||
// and now is on clean air (Calibration conditions), and it will setup R0 value.
|
||||
// We recomend execute this routine only on setup or on the laboratory and save on the eeprom of your arduino
|
||||
// This routine not need to execute to every restart, you can load your R0 if you know the value
|
||||
// In this routine the sensor will measure the resistance of the sensor supposedly before being pre-heated
|
||||
// and on clean air (Calibration conditions), setting up R0 value.
|
||||
// We recommend executing this routine only on setup in laboratory conditions.
|
||||
// This routine does not need to be executed on each restart, you can load your R0 value from eeprom.
|
||||
// Acknowledgements: https://jayconsystems.com/blog/understanding-a-gas-sensor
|
||||
Serial.print("Calibrating please wait.");
|
||||
float calcR0 = 0;
|
||||
for(int i = 1; i<=10; i ++)
|
||||
{
|
||||
MQ309.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ309.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
calcR0 += MQ309.calibrate(RatioMQ309CleanAir);
|
||||
Serial.print(".");
|
||||
}
|
||||
MQ309.setR0(calcR0/10);
|
||||
Serial.println(" done!.");
|
||||
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Conection issue founded, R0 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Conection issue founded, R0 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Connection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
MQ309.serialDebug(true);
|
||||
}
|
||||
@ -94,8 +94,9 @@ void loop() {
|
||||
{
|
||||
// VH 0.9 Volts
|
||||
analogWrite(5, 2); // 255 is 100%, 2.295 is aprox 0.9% of Duty cycle for 60s
|
||||
MQ309.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ309.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
MQ309.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
float correctionFactor = 0; // Optional environmental correction
|
||||
MQ309.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
MQ309.serialDebug(); // Will print the table on the serial port
|
||||
delay(500); //Sampling frequency
|
||||
}
|
||||
@ -106,8 +107,9 @@ void loop() {
|
||||
{
|
||||
// VL 0.2 Volts
|
||||
analogWrite(5, 1); // 255 is 100%, 0.51 is aprox 0.2% of Duty cycle for 120s
|
||||
MQ309.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ309.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
MQ309.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
float correctionFactor2 = 0; // Optional environmental correction
|
||||
MQ309.readSensor(false, correctionFactor2); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
MQ309.serialDebug(); // Will print the table on the serial port
|
||||
delay(500); //Sampling frequency
|
||||
}
|
@ -15,7 +15,7 @@
|
||||
|
||||
Wiring:
|
||||
https://github.com/miguel5612/MQSensorsLib_Docs/blob/master/static/img/MQ_Arduino.PNG
|
||||
Please take care, arduino A0 pin represent the analog input configured on #define pin
|
||||
Please make sure arduino A0 pin represents the analog input configured on #define pin
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
@ -53,32 +53,33 @@ void setup() {
|
||||
*/
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
// Explanation:
|
||||
// In this routine the sensor will measure the resistance of the sensor supposing before was pre-heated
|
||||
// and now is on clean air (Calibration conditions), and it will setup R0 value.
|
||||
// We recomend execute this routine only on setup or on the laboratory and save on the eeprom of your arduino
|
||||
// This routine not need to execute to every restart, you can load your R0 if you know the value
|
||||
// In this routine the sensor will measure the resistance of the sensor supposedly before being pre-heated
|
||||
// and on clean air (Calibration conditions), setting up R0 value.
|
||||
// We recommend executing this routine only on setup in laboratory conditions.
|
||||
// This routine does not need to be executed on each restart, you can load your R0 value from eeprom.
|
||||
// Acknowledgements: https://jayconsystems.com/blog/understanding-a-gas-sensor
|
||||
Serial.print("Calibrating please wait.");
|
||||
float calcR0 = 0;
|
||||
for(int i = 1; i<=10; i ++)
|
||||
{
|
||||
MQ4.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ4.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
calcR0 += MQ4.calibrate(RatioMQ4CleanAir);
|
||||
Serial.print(".");
|
||||
}
|
||||
MQ4.setR0(calcR0/10);
|
||||
Serial.println(" done!.");
|
||||
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Conection issue founded, R0 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Conection issue founded, R0 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Connection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
|
||||
Serial.println("*************** Lectures from MQ-4 **********************");
|
||||
Serial.println("*************** Values from MQ-4 **********************");
|
||||
Serial.println("| LPG | CH4 | CO | Alcohol | Smoke |");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
MQ4.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ4.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
float correctionFactor = 0; // Optional environmental correction
|
||||
|
||||
/*
|
||||
Exponential regression:
|
||||
@ -89,20 +90,20 @@ void loop() {
|
||||
Alcohol| 60000000000 | -14.01
|
||||
smoke | 30000000 | -8.308
|
||||
*/
|
||||
MQ4.setA(3811.9); MQ4.setB(-3.113); // Configurate the ecuation values to get CH4 concentration
|
||||
float LPG = MQ4.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
MQ4.setA(3811.9); MQ4.setB(-3.113); // Configure the equation to to calculate CH4 concentration
|
||||
float LPG = MQ4.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
|
||||
MQ4.setA(1012.7); MQ4.setB(-2.786); // Configurate the ecuation values to get CH4 concentration
|
||||
float CH4 = MQ4.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
MQ4.setA(1012.7); MQ4.setB(-2.786); // Configure the equation to to calculate CH4 concentration
|
||||
float CH4 = MQ4.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
|
||||
MQ4.setA(200000000000000); MQ4.setB(-19.05); // Configurate the ecuation values to get CH4 concentration
|
||||
float CO = MQ4.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
MQ4.setA(200000000000000); MQ4.setB(-19.05); // Configure the equation to to calculate CH4 concentration
|
||||
float CO = MQ4.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
|
||||
MQ4.setA(60000000000); MQ4.setB(-14.01); // Configurate the ecuation values to get CH4 concentration
|
||||
float Alcohol = MQ4.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
MQ4.setA(60000000000); MQ4.setB(-14.01); // Configure the equation to to calculate CH4 concentration
|
||||
float Alcohol = MQ4.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
|
||||
MQ4.setA(30000000); MQ4.setB(-8.308); // Configurate the ecuation values to get CH4 concentration
|
||||
float Smoke = MQ4.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
MQ4.setA(30000000); MQ4.setB(-8.308); // Configure the equation to to calculate CH4 concentration
|
||||
float Smoke = MQ4.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
|
||||
Serial.print("| "); Serial.print(LPG);
|
||||
Serial.print(" | "); Serial.print(CH4);
|
||||
|
@ -15,7 +15,7 @@
|
||||
|
||||
Wiring:
|
||||
https://github.com/miguel5612/MQSensorsLib_Docs/blob/master/static/img/MQ_Arduino.PNG
|
||||
Please take care, arduino A0 pin represent the analog input configured on #define pin
|
||||
Please make sure arduino A0 pin represents the analog input configured on #define pin
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
@ -52,36 +52,37 @@ void setup() {
|
||||
*/
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
// Explanation:
|
||||
// In this routine the sensor will measure the resistance of the sensor supposing before was pre-heated
|
||||
// and now is on clean air (Calibration conditions), and it will setup R0 value.
|
||||
// We recomend execute this routine only on setup or on the laboratory and save on the eeprom of your arduino
|
||||
// This routine not need to execute to every restart, you can load your R0 if you know the value
|
||||
// In this routine the sensor will measure the resistance of the sensor supposedly before being pre-heated
|
||||
// and on clean air (Calibration conditions), setting up R0 value.
|
||||
// We recommend executing this routine only on setup in laboratory conditions.
|
||||
// This routine does not need to be executed on each restart, you can load your R0 value from eeprom.
|
||||
// Acknowledgements: https://jayconsystems.com/blog/understanding-a-gas-sensor
|
||||
Serial.print("Calibrating please wait.");
|
||||
float calcR0 = 0;
|
||||
for(int i = 1; i<=10; i ++)
|
||||
{
|
||||
MQ4.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ4.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
calcR0 += MQ4.calibrate(RatioMQ4CleanAir);
|
||||
Serial.print(".");
|
||||
}
|
||||
MQ4.setR0(calcR0/10);
|
||||
Serial.println(" done!.");
|
||||
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Conection issue founded, R0 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Conection issue founded, R0 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Connection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
|
||||
Serial.println("** Lectures from MQ-4**********");
|
||||
Serial.println("** Values from MQ-4**********");
|
||||
Serial.println("| LPG |");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
MQ4.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ4.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
|
||||
//https://jayconsystems.com/blog/understanding-a-gas-sensor
|
||||
MQ4.setA(-0.318); MQ4.setB(1.133); // A -> Slope, B -> Intersect with X - Axis
|
||||
float LPG = MQ4.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
float correctionFactor = 0; // Optional environmental correction
|
||||
float LPG = MQ4.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
|
||||
// exposure to 2000 ppm of LPG gas is immediately dangerous to life and health. In this section
|
||||
if(LPG>=2000) Serial.println("Warning - Very high concentrations detected!");
|
||||
|
@ -15,7 +15,7 @@
|
||||
|
||||
Wiring:
|
||||
https://github.com/miguel5612/MQSensorsLib_Docs/blob/master/static/img/MQ_Arduino.PNG
|
||||
Please take care, arduino A0 pin represent the analog input configured on #define pin
|
||||
Please make sure arduino A0 pin represents the analog input configured on #define pin
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
@ -41,7 +41,7 @@ void setup() {
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ4.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ4.setA(1012.7); MQ4.setB(-2.786); // Configurate the ecuation values to get CH4 concentration
|
||||
MQ4.setA(1012.7); MQ4.setB(-2.786); // Configure the equation to to calculate CH4 concentration
|
||||
/*
|
||||
Exponential regression:
|
||||
Gas | a | b
|
||||
@ -63,31 +63,32 @@ void setup() {
|
||||
*/
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
// Explanation:
|
||||
// In this routine the sensor will measure the resistance of the sensor supposing before was pre-heated
|
||||
// and now is on clean air (Calibration conditions), and it will setup R0 value.
|
||||
// We recomend execute this routine only on setup or on the laboratory and save on the eeprom of your arduino
|
||||
// This routine not need to execute to every restart, you can load your R0 if you know the value
|
||||
// In this routine the sensor will measure the resistance of the sensor supposedly before being pre-heated
|
||||
// and on clean air (Calibration conditions), setting up R0 value.
|
||||
// We recommend executing this routine only on setup in laboratory conditions.
|
||||
// This routine does not need to be executed on each restart, you can load your R0 value from eeprom.
|
||||
// Acknowledgements: https://jayconsystems.com/blog/understanding-a-gas-sensor
|
||||
Serial.print("Calibrating please wait.");
|
||||
float calcR0 = 0;
|
||||
for(int i = 1; i<=10; i ++)
|
||||
{
|
||||
MQ4.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ4.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
calcR0 += MQ4.calibrate(RatioMQ4CleanAir);
|
||||
Serial.print(".");
|
||||
}
|
||||
MQ4.setR0(calcR0/10);
|
||||
Serial.println(" done!.");
|
||||
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Conection issue founded, R0 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Conection issue founded, R0 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Connection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
MQ4.serialDebug(true);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
MQ4.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ4.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
MQ4.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
float correctionFactor = 0; // Optional environmental correction
|
||||
MQ4.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
MQ4.serialDebug(); // Will print the table on the serial port
|
||||
delay(500); //Sampling frequency
|
||||
}
|
@ -15,7 +15,7 @@
|
||||
|
||||
Wiring:
|
||||
https://github.com/miguel5612/MQSensorsLib_Docs/blob/master/static/img/MQ_Arduino.PNG
|
||||
Please take care, arduino A0 pin represent the analog input configured on #define pin
|
||||
Please make sure arduino A0 pin represents the analog input configured on #define pin
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
@ -42,7 +42,7 @@ void setup() {
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ5.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ5.setA(1163.8); MQ5.setB(-3.874); // Configurate the ecuation values to get H2 concentration
|
||||
MQ5.setA(1163.8); MQ5.setB(-3.874); // Configure the equation to to calculate H2 concentration
|
||||
/*
|
||||
Exponential regression:
|
||||
Gas | a | b
|
||||
@ -63,31 +63,32 @@ void setup() {
|
||||
*/
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
// Explanation:
|
||||
// In this routine the sensor will measure the resistance of the sensor supposing before was pre-heated
|
||||
// and now is on clean air (Calibration conditions), and it will setup R0 value.
|
||||
// We recomend execute this routine only on setup or on the laboratory and save on the eeprom of your arduino
|
||||
// This routine not need to execute to every restart, you can load your R0 if you know the value
|
||||
// In this routine the sensor will measure the resistance of the sensor supposedly before being pre-heated
|
||||
// and on clean air (Calibration conditions), setting up R0 value.
|
||||
// We recommend executing this routine only on setup in laboratory conditions.
|
||||
// This routine does not need to be executed on each restart, you can load your R0 value from eeprom.
|
||||
// Acknowledgements: https://jayconsystems.com/blog/understanding-a-gas-sensor
|
||||
Serial.print("Calibrating please wait.");
|
||||
float calcR0 = 0;
|
||||
for(int i = 1; i<=10; i ++)
|
||||
{
|
||||
MQ5.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ5.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
calcR0 += MQ5.calibrate(RatioMQ5CleanAir);
|
||||
Serial.print(".");
|
||||
}
|
||||
MQ5.setR0(calcR0/10);
|
||||
Serial.println(" done!.");
|
||||
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Conection issue founded, R0 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Conection issue founded, R0 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Connection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
MQ5.serialDebug(true);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
MQ5.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ5.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
MQ5.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
float correctionFactor = 0; // Optional environmental correction
|
||||
MQ5.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
MQ5.serialDebug(); // Will print the table on the serial port
|
||||
delay(500); //Sampling frequency
|
||||
}
|
@ -15,7 +15,7 @@
|
||||
|
||||
Wiring:
|
||||
https://github.com/miguel5612/MQSensorsLib_Docs/blob/master/static/img/MQ_Arduino.PNG
|
||||
Please take care, arduino A0 pin represent the analog input configured on #define pin
|
||||
Please make sure arduino A0 pin represents the analog input configured on #define pin
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
@ -42,7 +42,7 @@ void setup() {
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ6.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ6.setA(2127.2); MQ6.setB(-2.526); // Configurate the ecuation values to get CH4 concentration
|
||||
MQ6.setA(2127.2); MQ6.setB(-2.526); // Configure the equation to to calculate CH4 concentration
|
||||
/*
|
||||
Exponential regression:
|
||||
GAS | a | b
|
||||
@ -63,31 +63,32 @@ void setup() {
|
||||
*/
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
// Explanation:
|
||||
// In this routine the sensor will measure the resistance of the sensor supposing before was pre-heated
|
||||
// and now is on clean air (Calibration conditions), and it will setup R0 value.
|
||||
// We recomend execute this routine only on setup or on the laboratory and save on the eeprom of your arduino
|
||||
// This routine not need to execute to every restart, you can load your R0 if you know the value
|
||||
// In this routine the sensor will measure the resistance of the sensor supposedly before being pre-heated
|
||||
// and on clean air (Calibration conditions), setting up R0 value.
|
||||
// We recommend executing this routine only on setup in laboratory conditions.
|
||||
// This routine does not need to be executed on each restart, you can load your R0 value from eeprom.
|
||||
// Acknowledgements: https://jayconsystems.com/blog/understanding-a-gas-sensor
|
||||
Serial.print("Calibrating please wait.");
|
||||
float calcR0 = 0;
|
||||
for(int i = 1; i<=10; i ++)
|
||||
{
|
||||
MQ6.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ6.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
calcR0 += MQ6.calibrate(RatioMQ6CleanAir);
|
||||
Serial.print(".");
|
||||
}
|
||||
MQ6.setR0(calcR0/10);
|
||||
Serial.println(" done!.");
|
||||
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Conection issue founded, R0 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Conection issue founded, R0 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Connection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
MQ6.serialDebug(true);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
MQ6.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ6.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
MQ6.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
float correctionFactor = 0; // Optional environmental correction
|
||||
MQ6.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
MQ6.serialDebug(); // Will print the table on the serial port
|
||||
delay(500); //Sampling frequency
|
||||
}
|
@ -19,7 +19,7 @@
|
||||
|
||||
Wiring:
|
||||
https://github.com/miguel5612/MQSensorsLib_Docs/blob/master/static/img/MQ_Arduino.PNG
|
||||
Please take care, arduino A0 pin represent the analog input configured on #define pin
|
||||
Please make sure arduino A0 pin represents the analog input configured on #define pin
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
@ -48,7 +48,7 @@ void setup() {
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ7.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ7.setA(99.042); MQ7.setB(-1.518); // Configurate the ecuation values to get CO concentration
|
||||
MQ7.setA(99.042); MQ7.setB(-1.518); // Configure the equation to calculate CO concentration value
|
||||
|
||||
/*
|
||||
Exponential regression:
|
||||
@ -70,24 +70,24 @@ void setup() {
|
||||
*/
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
// Explanation:
|
||||
// In this routine the sensor will measure the resistance of the sensor supposing before was pre-heated
|
||||
// and now is on clean air (Calibration conditions), and it will setup R0 value.
|
||||
// We recomend execute this routine only on setup or on the laboratory and save on the eeprom of your arduino
|
||||
// This routine not need to execute to every restart, you can load your R0 if you know the value
|
||||
// In this routine the sensor will measure the resistance of the sensor supposedly before being pre-heated
|
||||
// and on clean air (Calibration conditions), setting up R0 value.
|
||||
// We recommend executing this routine only on setup in laboratory conditions.
|
||||
// This routine does not need to be executed on each restart, you can load your R0 value from eeprom.
|
||||
// Acknowledgements: https://jayconsystems.com/blog/understanding-a-gas-sensor
|
||||
Serial.print("Calibrating please wait.");
|
||||
float calcR0 = 0;
|
||||
for(int i = 1; i<=10; i ++)
|
||||
{
|
||||
MQ7.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ7.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
calcR0 += MQ7.calibrate(RatioMQ7CleanAir);
|
||||
Serial.print(".");
|
||||
}
|
||||
MQ7.setR0(calcR0/10);
|
||||
Serial.println(" done!.");
|
||||
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Conection issue founded, R0 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Conection issue founded, R0 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Connection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
MQ7.serialDebug(true);
|
||||
}
|
||||
@ -99,8 +99,9 @@ void loop() {
|
||||
{
|
||||
// VH 5 Volts
|
||||
analogWrite(5, 255); // 255 is DC 5V output
|
||||
MQ7.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ7.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
MQ7.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
float correctionFactor = 0; // Optional environmental correction
|
||||
MQ7.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
MQ7.serialDebug(); // Will print the table on the serial port
|
||||
delay(500); //Sampling frequency
|
||||
}
|
||||
@ -111,8 +112,9 @@ void loop() {
|
||||
{
|
||||
// VH 1.4 Volts
|
||||
analogWrite(5, 20); // 255 is 100%, 20.4 is aprox 8% of Duty cycle for 90s
|
||||
MQ7.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ7.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
MQ7.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
float correctionFactor2 = 0; // Optional environmental correction
|
||||
MQ7.readSensor(false, correctionFactor2); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
MQ7.serialDebug(); // Will print the table on the serial port
|
||||
delay(500); //Sampling frequency
|
||||
}
|
||||
|
@ -15,7 +15,7 @@
|
||||
|
||||
Wiring:
|
||||
https://github.com/miguel5612/MQSensorsLib_Docs/blob/master/static/img/MQ_Arduino.PNG
|
||||
Please take care, arduino A0 pin represent the analog input configured on #define pin
|
||||
Please make sure arduino A0 pin represents the analog input configured on #define pin
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
@ -42,7 +42,7 @@ void setup() {
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ8.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ8.setA(976.97); MQ8.setB(-0.688); // Configurate the ecuation values to get H2 concentration
|
||||
MQ8.setA(976.97); MQ8.setB(-0.688); // Configure the equation to to calculate H2 concentration
|
||||
|
||||
/*
|
||||
Exponential regression:
|
||||
@ -64,31 +64,32 @@ void setup() {
|
||||
*/
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
// Explanation:
|
||||
// In this routine the sensor will measure the resistance of the sensor supposing before was pre-heated
|
||||
// and now is on clean air (Calibration conditions), and it will setup R0 value.
|
||||
// We recomend execute this routine only on setup or on the laboratory and save on the eeprom of your arduino
|
||||
// This routine not need to execute to every restart, you can load your R0 if you know the value
|
||||
// In this routine the sensor will measure the resistance of the sensor supposedly before being pre-heated
|
||||
// and on clean air (Calibration conditions), setting up R0 value.
|
||||
// We recommend executing this routine only on setup in laboratory conditions.
|
||||
// This routine does not need to be executed on each restart, you can load your R0 value from eeprom.
|
||||
// Acknowledgements: https://jayconsystems.com/blog/understanding-a-gas-sensor
|
||||
Serial.print("Calibrating please wait.");
|
||||
float calcR0 = 0;
|
||||
for(int i = 1; i<=10; i ++)
|
||||
{
|
||||
MQ8.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ8.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
calcR0 += MQ8.calibrate(RatioMQ8CleanAir);
|
||||
Serial.print(".");
|
||||
}
|
||||
MQ8.setR0(calcR0/10);
|
||||
Serial.println(" done!.");
|
||||
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Conection issue founded, R0 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Conection issue founded, R0 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Connection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
MQ8.serialDebug(true);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
MQ8.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ8.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
MQ8.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
float correctionFactor = 0; // Optional environmental correction
|
||||
MQ8.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
MQ8.serialDebug(); // Will print the table on the serial port
|
||||
delay(500); //Sampling frequency
|
||||
}
|
@ -15,7 +15,7 @@
|
||||
|
||||
Wiring:
|
||||
https://github.com/miguel5612/MQSensorsLib_Docs/blob/master/static/img/MQ_Arduino.PNG
|
||||
Please take care, arduino A0 pin represent the analog input configured on #define pin
|
||||
Please make sure arduino A0 pin represents the analog input configured on #define pin
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
@ -53,31 +53,32 @@ void setup() {
|
||||
*/
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
// Explanation:
|
||||
// In this routine the sensor will measure the resistance of the sensor supposing before was pre-heated
|
||||
// and now is on clean air (Calibration conditions), and it will setup R0 value.
|
||||
// We recomend execute this routine only on setup or on the laboratory and save on the eeprom of your arduino
|
||||
// This routine not need to execute to every restart, you can load your R0 if you know the value
|
||||
// In this routine the sensor will measure the resistance of the sensor supposedly before being pre-heated
|
||||
// and on clean air (Calibration conditions), setting up R0 value.
|
||||
// We recommend executing this routine only on setup in laboratory conditions.
|
||||
// This routine does not need to be executed on each restart, you can load your R0 value from eeprom.
|
||||
// Acknowledgements: https://jayconsystems.com/blog/understanding-a-gas-sensor
|
||||
Serial.print("Calibrating please wait.");
|
||||
float calcR0 = 0;
|
||||
for(int i = 1; i<=10; i ++)
|
||||
{
|
||||
MQ9.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ9.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
calcR0 += MQ9.calibrate(RatioMQ9CleanAir);
|
||||
Serial.print(".");
|
||||
}
|
||||
MQ9.setR0(calcR0/10);
|
||||
Serial.println(" done!.");
|
||||
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Conection issue founded, R0 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Conection issue founded, R0 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Connection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
Serial.println("** Lectures from MQ-9 ****");
|
||||
Serial.println("** Values from MQ-9 ****");
|
||||
Serial.println("| LPG | CH4 | CO |");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
MQ9.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ9.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
float correctionFactor = 0; // Optional environmental correction
|
||||
/*
|
||||
Exponential regression:
|
||||
GAS | a | b
|
||||
@ -86,14 +87,14 @@ void loop() {
|
||||
CO | 599.65 | -2.244
|
||||
*/
|
||||
|
||||
MQ9.setA(1000.5); MQ9.setB(-2.186); // Configurate the ecuation values to get LPG concentration
|
||||
float LPG = MQ9.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
MQ9.setA(1000.5); MQ9.setB(-2.186); // Configure the equation to to calculate LPG concentration
|
||||
float LPG = MQ9.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
|
||||
MQ9.setA(4269.6); MQ9.setB(-2.648); // Configurate the ecuation values to get LPG concentration
|
||||
float CH4 = MQ9.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
MQ9.setA(4269.6); MQ9.setB(-2.648); // Configure the equation to to calculate LPG concentration
|
||||
float CH4 = MQ9.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
|
||||
MQ9.setA(599.65); MQ9.setB(-2.244); // Configurate the ecuation values to get LPG concentration
|
||||
float CO = MQ9.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
MQ9.setA(599.65); MQ9.setB(-2.244); // Configure the equation to to calculate LPG concentration
|
||||
float CO = MQ9.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
|
||||
Serial.print("| "); Serial.print(LPG);
|
||||
Serial.print(" | "); Serial.print(CH4);
|
||||
|
@ -15,7 +15,7 @@
|
||||
|
||||
Wiring:
|
||||
https://github.com/miguel5612/MQSensorsLib_Docs/blob/master/static/img/MQ_Arduino.PNG
|
||||
Please take care, arduino A0 pin represent the analog input configured on #define pin
|
||||
Please make sure arduino A0 pin represents the analog input configured on #define pin
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
@ -25,12 +25,14 @@
|
||||
#include <MQUnifiedsensor.h>
|
||||
/************************Hardware Related Macros************************************/
|
||||
#define Board ("Arduino UNO")
|
||||
#define Pin (A9) //Analog input 4 of your arduino
|
||||
#define Pin (A0) //Analog input 4 of your arduino
|
||||
/***********************Software Related Macros************************************/
|
||||
#define Type ("MQ-9") //MQ9
|
||||
#define Voltage_Resolution (5)
|
||||
#define ADC_Bit_Resolution (10) // For arduino UNO/MEGA/NANO
|
||||
#define RatioMQ9CleanAir (9.6) //RS / R0 = 60 ppm
|
||||
#define PreaheatControlPin5 (3) // Preaheat pin to control with 5 volts
|
||||
#define PreaheatControlPin14 (4) // Preaheat pin to control with 1.4 volts
|
||||
/*****************************Globals***********************************************/
|
||||
//Declare Sensor
|
||||
MQUnifiedsensor MQ9(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin, Type);
|
||||
@ -38,10 +40,12 @@ MQUnifiedsensor MQ9(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin, Type);
|
||||
void setup() {
|
||||
//Init the serial port communication - to debug the library
|
||||
Serial.begin(9600); //Init serial port
|
||||
pinMode(PreaheatControlPin5, OUTPUT);
|
||||
pinMode(PreaheatControlPin14, OUTPUT);
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ9.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ9.setA(1000.5); MQ9.setB(-2.186); // Configurate the ecuation values to get LPG concentration
|
||||
MQ9.setA(1000.5); MQ9.setB(-2.186); // Configure the equation to to calculate LPG concentration
|
||||
|
||||
/*
|
||||
Exponential regression:
|
||||
@ -61,31 +65,58 @@ void setup() {
|
||||
*/
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
// Explanation:
|
||||
// In this routine the sensor will measure the resistance of the sensor supposing before was pre-heated
|
||||
// and now is on clean air (Calibration conditions), and it will setup R0 value.
|
||||
// We recomend execute this routine only on setup or on the laboratory and save on the eeprom of your arduino
|
||||
// This routine not need to execute to every restart, you can load your R0 if you know the value
|
||||
// In this routine the sensor will measure the resistance of the sensor supposedly before being pre-heated
|
||||
// and on clean air (Calibration conditions), setting up R0 value.
|
||||
// We recommend executing this routine only on setup in laboratory conditions.
|
||||
// This routine does not need to be executed on each restart, you can load your R0 value from eeprom.
|
||||
// Acknowledgements: https://jayconsystems.com/blog/understanding-a-gas-sensor
|
||||
// ISSUE 44 - MQ9 needs a low/high temperature cycle like MQ7 #44
|
||||
|
||||
Serial.println("Preheating, please wait 2.5 minutes");
|
||||
digitalWrite(PreaheatControlPin5, HIGH);
|
||||
digitalWrite(PreaheatControlPin14, LOW);
|
||||
delay(60000);
|
||||
digitalWrite(PreaheatControlPin5, LOW);
|
||||
digitalWrite(PreaheatControlPin14, HIGH);
|
||||
delay(90000);
|
||||
digitalWrite(PreaheatControlPin5, HIGH);
|
||||
digitalWrite(PreaheatControlPin14, LOW);
|
||||
delay(60000);
|
||||
digitalWrite(PreaheatControlPin5, LOW);
|
||||
digitalWrite(PreaheatControlPin14, HIGH);
|
||||
delay(90000);
|
||||
digitalWrite(PreaheatControlPin5, HIGH);
|
||||
digitalWrite(PreaheatControlPin14, LOW);
|
||||
|
||||
// End pre-heat cycle
|
||||
|
||||
Serial.print("Calibrating please wait.");
|
||||
float calcR0 = 0;
|
||||
for(int i = 1; i<=10; i ++)
|
||||
{
|
||||
MQ9.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ9.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
calcR0 += MQ9.calibrate(RatioMQ9CleanAir);
|
||||
Serial.print(".");
|
||||
}
|
||||
MQ9.setR0(calcR0/10);
|
||||
Serial.println(" done!.");
|
||||
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Conection issue founded, R0 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Conection issue founded, R0 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Connection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
MQ9.serialDebug(true);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
MQ9.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ9.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
digitalWrite(PreaheatControlPin5, LOW);
|
||||
digitalWrite(PreaheatControlPin14, HIGH);
|
||||
delay(90000);
|
||||
digitalWrite(PreaheatControlPin5, HIGH);
|
||||
digitalWrite(PreaheatControlPin14, LOW);
|
||||
|
||||
MQ9.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
float correctionFactor = 0; // Optional environmental correction
|
||||
MQ9.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
MQ9.serialDebug(); // Will print the table on the serial port
|
||||
delay(500); //Sampling frequency
|
||||
|
||||
}
|
@ -11,7 +11,7 @@
|
||||
|
||||
Wiring:
|
||||
https://github.com/miguel5612/MQSensorsLib_Docs/blob/master/static/img/MQ_Arduino.PNG
|
||||
Please take care, arduino A0 pin represent the analog input configured on #define pin
|
||||
Please make sure arduino A0 pin represents the analog input configured on #define pin
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
@ -61,50 +61,50 @@ void setup() {
|
||||
//init the sensor
|
||||
MQ2.init();
|
||||
MQ2.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ2.setA(574.25); MQ2.setB(-2.222); // Configurate the ecuation values to get LPG concentration
|
||||
MQ2.setA(574.25); MQ2.setB(-2.222); // Configure the equation to to calculate LPG concentration
|
||||
MQ2.setR0(9.659574468);
|
||||
|
||||
MQ3.init();
|
||||
MQ3.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ3.setA(0.3934); MQ3.setB(-1.504); // Configurate the ecuation values to get Alcohol concentration
|
||||
MQ3.setA(0.3934); MQ3.setB(-1.504); //Configure the equation to calculate Alcohol concentration value
|
||||
MQ3.setR0(3.86018237);
|
||||
|
||||
MQ4.init();
|
||||
MQ4.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ4.setA(1012.7); MQ4.setB(-2.786); // Configurate the ecuation values to get CH4 concentration
|
||||
MQ4.setA(1012.7); MQ4.setB(-2.786); // Configure the equation to to calculate CH4 concentration
|
||||
MQ4.setR0(3.86018237);
|
||||
|
||||
MQ5.init();
|
||||
MQ5.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ5.setA(97124); MQ5.setB(-4.918); // Configurate the ecuation values to get Alcohol concentration
|
||||
MQ5.setA(97124); MQ5.setB(-4.918); //Configure the equation to calculate Alcohol concentration value
|
||||
MQ5.setR0(71.100304);
|
||||
|
||||
MQ6.init();
|
||||
MQ6.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ6.setA(2127.2); MQ6.setB(-2.526); // Configurate the ecuation values to get CH4 concentration
|
||||
MQ6.setA(2127.2); MQ6.setB(-2.526); // Configure the equation to to calculate CH4 concentration
|
||||
MQ6.setR0(13.4285714);
|
||||
|
||||
MQ7.init();
|
||||
MQ7.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ7.setA(99.042); MQ7.setB(-1.518); // Configurate the ecuation values to get CO concentration
|
||||
MQ7.setA(99.042); MQ7.setB(-1.518); // Configure the equation to calculate CO concentration value
|
||||
MQ7.setR0(4);
|
||||
|
||||
MQ8.init();
|
||||
MQ8.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ8.setA(976.97); MQ8.setB(-0.688); // Configurate the ecuation values to get H2 concentration
|
||||
MQ8.setA(976.97); MQ8.setB(-0.688); // Configure the equation to to calculate H2 concentration
|
||||
MQ8.setR0(1);
|
||||
|
||||
MQ9.init();
|
||||
MQ9.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ9.setA(1000.5); MQ9.setB(-2.186); // Configurate the ecuation values to get LPG concentration
|
||||
MQ9.setA(1000.5); MQ9.setB(-2.186); // Configure the equation to to calculate LPG concentration
|
||||
MQ9.setR0(9.42857143);
|
||||
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
// Explanation:
|
||||
// In this routine the sensor will measure the resistance of the sensor supposing before was pre-heated
|
||||
// and now is on clean air (Calibration conditions), and it will setup R0 value.
|
||||
// We recomend execute this routine only on setup or on the laboratory and save on the eeprom of your arduino
|
||||
// This routine not need to execute to every restart, you can load your R0 if you know the value
|
||||
// In this routine the sensor will measure the resistance of the sensor supposedly before being pre-heated
|
||||
// and on clean air (Calibration conditions), setting up R0 value.
|
||||
// We recommend executing this routine only on setup in laboratory conditions.
|
||||
// This routine does not need to be executed on each restart, you can load your R0 value from eeprom.
|
||||
// Acknowledgements: https://jayconsystems.com/blog/understanding-a-gas-sensor
|
||||
Serial.print("Calibrating please wait.");
|
||||
float MQ2calcR0 = 0,
|
||||
@ -117,7 +117,7 @@ void setup() {
|
||||
MQ9calcR0 = 0;
|
||||
for(int i = 1; i<=10; i ++)
|
||||
{
|
||||
//Update the voltage lectures
|
||||
//Update the voltage Values
|
||||
MQ2.update();
|
||||
MQ3.update();
|
||||
MQ4.update();
|
||||
@ -158,13 +158,13 @@ void setup() {
|
||||
Serial.print(MQ8calcR0/10); Serial.print(" | ");
|
||||
Serial.print(MQ9calcR0/10); Serial.println(" |");
|
||||
|
||||
if(isinf(MQ2calcR0) || isinf(MQ3calcR0) || isinf(MQ4calcR0) || isinf(MQ5calcR0) || isinf(MQ6calcR0) || isinf(MQ7calcR0) || isinf(MQ8calcR0) || isinf(MQ9calcR0)) {Serial.println("Warning: Conection issue founded, R0 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(MQ2calcR0 == 0 || MQ3calcR0 == 0 || MQ4calcR0 == 0 || MQ5calcR0 == 0 || MQ6calcR0 == 0 || MQ7calcR0 == 0 || MQ8calcR0 == 0 || MQ9calcR0 == 0){Serial.println("Warning: Conection issue founded, R0 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
|
||||
if(isinf(MQ2calcR0) || isinf(MQ3calcR0) || isinf(MQ4calcR0) || isinf(MQ5calcR0) || isinf(MQ6calcR0) || isinf(MQ7calcR0) || isinf(MQ8calcR0) || isinf(MQ9calcR0)) {Serial.println("Warning: Connection issue found, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(MQ2calcR0 == 0 || MQ3calcR0 == 0 || MQ4calcR0 == 0 || MQ5calcR0 == 0 || MQ6calcR0 == 0 || MQ7calcR0 == 0 || MQ8calcR0 == 0 || MQ9calcR0 == 0){Serial.println("Warning: Connection issue found, R0 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
|
||||
//Print in serial monitor
|
||||
Serial.println("MQ2 to MQ9 - lecture");
|
||||
Serial.println("*************************** Lectures from MQ-board ***************************");
|
||||
Serial.println("*************************** Values from MQ-board ***************************");
|
||||
Serial.println("| LPG | Alcohol | CH4 | Alcohol | CH4 | CO | H2 | LPG |");
|
||||
Serial.println("| MQ-2 | MQ-3 | MQ-4 | MQ-5 | MQ-6 | MQ-7 | MQ-8 | MQ-9 |");
|
||||
//pinMode(calibration_button, INPUT);
|
||||
@ -192,7 +192,7 @@ void loop() {
|
||||
|
||||
void readAllSensors()
|
||||
{
|
||||
//Update the voltage lectures
|
||||
//Update the voltage Values
|
||||
MQ2.update();
|
||||
MQ3.update();
|
||||
MQ4.update();
|
||||
@ -201,15 +201,16 @@ void readAllSensors()
|
||||
MQ7.update();
|
||||
MQ8.update();
|
||||
MQ9.update();
|
||||
float correctionFactor = 0; // Optional environmental correction
|
||||
//Read the sensor and print in serial port
|
||||
float MQ2Lecture = MQ2.readSensor();
|
||||
float MQ3Lecture = MQ3.readSensor();
|
||||
float MQ4Lecture = MQ4.readSensor();
|
||||
float MQ5Lecture = MQ5.readSensor();
|
||||
float MQ6Lecture = MQ6.readSensor();
|
||||
float MQ7Lecture = MQ7.readSensor();
|
||||
float MQ8Lecture = MQ8.readSensor();
|
||||
float MQ9Lecture = MQ9.readSensor();
|
||||
float MQ2Lecture = MQ2.readSensor(false, correctionFactor);
|
||||
float MQ3Lecture = MQ3.readSensor(false, correctionFactor);
|
||||
float MQ4Lecture = MQ4.readSensor(false, correctionFactor);
|
||||
float MQ5Lecture = MQ5.readSensor(false, correctionFactor);
|
||||
float MQ6Lecture = MQ6.readSensor(false, correctionFactor);
|
||||
float MQ7Lecture = MQ7.readSensor(false, correctionFactor);
|
||||
float MQ8Lecture = MQ8.readSensor(false, correctionFactor);
|
||||
float MQ9Lecture = MQ9.readSensor(false, correctionFactor);
|
||||
|
||||
Serial.print("| "); Serial.print(MQ2Lecture);
|
||||
Serial.print(" | "); Serial.print(MQ3Lecture);
|
||||
|
125
examples/MQ135-ADS1115/MQ135-ADS1115.ino
Normal file
125
examples/MQ135-ADS1115/MQ135-ADS1115.ino
Normal file
@ -0,0 +1,125 @@
|
||||
/*
|
||||
MQUnifiedsensor Library - reading an MQ135
|
||||
Demonstrates the use a MQ135 sensor.
|
||||
Library originally added 01 may 2019
|
||||
by Miguel A Califa, Yersson Carrillo, Ghiordy Contreras, Mario Rodriguez
|
||||
|
||||
Added example
|
||||
modified 20 March 2022
|
||||
by Miguel Califa
|
||||
|
||||
Wiring:
|
||||
https://github.com/miguel5612/MQSensorsLib_Docs/blob/master/static/img/MQ_Arduino.PNG
|
||||
Please make sure arduino A0 pin represents the analog input configured on #define pin
|
||||
This example code is in the public domain.
|
||||
|
||||
Library link https://github.com/adafruit/Adafruit_ADS1X15
|
||||
*/
|
||||
#include <Wire.h>
|
||||
#include <Adafruit_ADS1X15.h>
|
||||
//Include the library
|
||||
#include <MQUnifiedsensor.h>
|
||||
//Definitions
|
||||
#define placa "Arduino UNO"
|
||||
#define type "MQ-135" //MQ135
|
||||
#define RatioMQ135CleanAir 3.6//RS / R0 = 3.6 ppm
|
||||
float factorEscala = 0.1875F;
|
||||
//#define calibration_button 13 //Pin to calibrate your sensor
|
||||
//Declare Sensor
|
||||
MQUnifiedsensor MQ135(placa, type);
|
||||
|
||||
|
||||
// Crear objeto de la clase
|
||||
Adafruit_ADS1115 ads;
|
||||
|
||||
void setup(void)
|
||||
{
|
||||
//Init the serial port communication - to debug the library
|
||||
Serial.begin(9600); //Init serial port
|
||||
delay(200);
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ135.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
|
||||
/************************************************************************************/
|
||||
MQ135.init();
|
||||
|
||||
// Iniciar el ADS1115
|
||||
ads.begin();
|
||||
|
||||
|
||||
Serial.print("Calibrating please wait.");
|
||||
float calcR0 = 0;
|
||||
for(int i = 1; i<=10; i ++)
|
||||
{
|
||||
// Obtener datos del A0 del ADS1115
|
||||
short adc0 = ads.readADC_SingleEnded(0);
|
||||
float voltios = (adc0 * factorEscala)/1000.0;
|
||||
MQ135.externalADCUpdate(voltios); // Update data, the arduino will read the voltage from the analog pin
|
||||
calcR0 += MQ135.calibrate(RatioMQ135CleanAir);
|
||||
Serial.print(".");
|
||||
}
|
||||
MQ135.setR0(calcR0/10);
|
||||
Serial.println(" done!.");
|
||||
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Connection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
Serial.println("** Values from MQ-135 ****");
|
||||
Serial.println("| CO | Alcohol | CO2 | Toluen | NH4 | Aceton |");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Obtener datos del A0 del ADS1115
|
||||
short adc0 = ads.readADC_SingleEnded(0);
|
||||
float voltios = (adc0 * factorEscala)/1000.0;
|
||||
MQ135.externalADCUpdate(voltios); // Update data, the arduino will read the voltage from the analog pin
|
||||
float correctionFactor = 0; // Optional environmental correction
|
||||
|
||||
MQ135.setA(605.18); MQ135.setB(-3.937); // Configure the equation to calculate CO concentration value
|
||||
float CO = MQ135.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
|
||||
MQ135.setA(77.255); MQ135.setB(-3.18); //Configure the equation to calculate Alcohol concentration value
|
||||
float Alcohol = MQ135.readSensor(false, correctionFactor); // SSensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
|
||||
MQ135.setA(110.47); MQ135.setB(-2.862); // Configure the equation to calculate CO2 concentration value
|
||||
float CO2 = MQ135.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
|
||||
MQ135.setA(44.947); MQ135.setB(-3.445); // Configure the equation to calculate Toluen concentration value
|
||||
float Toluen = MQ135.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
|
||||
MQ135.setA(102.2 ); MQ135.setB(-2.473); // Configure the equation to calculate NH4 concentration value
|
||||
float NH4 = MQ135.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
|
||||
MQ135.setA(34.668); MQ135.setB(-3.369); // Configure the equation to calculate Aceton concentration value
|
||||
float Aceton = MQ135.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
Serial.print("| "); Serial.print(CO);
|
||||
Serial.print(" | "); Serial.print(Alcohol);
|
||||
// Note: 200 Offset for CO2 source: https://github.com/miguel5612/MQSensorsLib/issues/29
|
||||
|
||||
|
||||
|
||||
/*
|
||||
Motivation:
|
||||
We have added 200 PPM because when the library is calibrated it assumes the current state of the
|
||||
air as 0 PPM, and it is considered today that the CO2 present in the atmosphere is around 400 PPM.
|
||||
https://www.lavanguardia.com/natural/20190514/462242832581/concentracion-dioxido-cabono-co2-atmosfera-bate-record-historia-humanidad.html
|
||||
*/
|
||||
Serial.print(" | "); Serial.print(CO2 + 400);
|
||||
Serial.print(" | "); Serial.print(Toluen);
|
||||
Serial.print(" | "); Serial.print(NH4);
|
||||
Serial.print(" | "); Serial.print(Aceton);
|
||||
Serial.println(" |");
|
||||
/*
|
||||
Exponential regression:
|
||||
GAS | a | b
|
||||
CO | 605.18 | -3.937
|
||||
Alcohol | 77.255 | -3.18
|
||||
CO2 | 110.47 | -2.862
|
||||
Toluen | 44.947 | -3.445
|
||||
NH4 | 102.2 | -2.473
|
||||
Aceton | 34.668 | -3.369
|
||||
*/
|
||||
|
||||
delay(500); //Sampling frequency
|
||||
}
|
282
examples/UnitTesting/UnitTesting.ino
Normal file
282
examples/UnitTesting/UnitTesting.ino
Normal file
@ -0,0 +1,282 @@
|
||||
/*
|
||||
MQUnifiedsensor Library - testing library
|
||||
|
||||
Library originally added 01 may 2019
|
||||
by Miguel A Califa, Yersson Carrillo, Ghiordy Contreras, Mario Rodriguez
|
||||
|
||||
Added example
|
||||
modified 21 March 2022
|
||||
by Miguel Califa
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
*/
|
||||
|
||||
//Include the library
|
||||
#include <MQUnifiedsensor.h>
|
||||
#line 2 "basic.ino"
|
||||
#include <ArduinoUnit.h>
|
||||
|
||||
/************************Hardware Related Macros************************************/
|
||||
#define Board ("Arduino Mega")
|
||||
#define Pin2 (A2) //Analog input 2 of your arduino
|
||||
#define Pin3 (A3) //Analog input 3 of your arduino
|
||||
#define Pin4 (A4) //Analog input 4 of your arduino
|
||||
#define Pin5 (A5) //Analog input 5 of your arduino
|
||||
#define Pin6 (A6) //Analog input 6 of your arduino
|
||||
#define Pin7 (A7) //Analog input 7 of your arduino
|
||||
#define Pin8 (A8) //Analog input 8 of your arduino
|
||||
#define Pin9 (A9) //Analog input 9 of your arduino
|
||||
#define Pin10 (A10) //Analog input 10 of your arduino
|
||||
#define Pin11 (A11) //Analog input 11 of your arduino
|
||||
#define Pin12 (A12) //Analog input 12 of your arduino
|
||||
#define Pin13 (A13) //Analog input 13 of your arduino
|
||||
#define Pin14 (A14) //Analog input 14 of your arduino
|
||||
#define PWMPin (5) // Pin connected to mosfet
|
||||
/***********************Software Related Macros************************************/
|
||||
#define RatioMQ2CleanAir (9.83) //RS / R0 = 9.83 ppm
|
||||
#define RatioMQ3CleanAir (60) //RS / R0 = 60 ppm
|
||||
#define RatioMQ4CleanAir (4.4) //RS / R0 = 4.4 ppm
|
||||
#define RatioMQ5CleanAir (6.5) //RS / R0 = 6.5 ppm
|
||||
#define RatioMQ6CleanAir (10) //RS / R0 = 10 ppm
|
||||
#define RatioMQ7CleanAir (27.5) //RS / R0 = 27.5 ppm
|
||||
#define RatioMQ8CleanAir (70) //RS / R0 = 70 ppm
|
||||
#define RatioMQ9CleanAir (9.6) //RS / R0 = 9.6 ppm
|
||||
#define RatioMQ136CleanAir (3.6) //RS / R0 = 9.6 ppm
|
||||
#define ADC_Bit_Resolution (10) // 10 bit ADC
|
||||
#define Voltage_Resolution (5) // Volt resolution to calc the voltage
|
||||
#define Type ("Arduino Mega 2560") //Board used
|
||||
/*****************************Globals***********************************************/
|
||||
//Declare Sensor
|
||||
MQUnifiedsensor MQ2(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin2, Type);
|
||||
MQUnifiedsensor MQ3(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin3, Type);
|
||||
MQUnifiedsensor MQ4(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin4, Type);
|
||||
MQUnifiedsensor MQ5(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin5, Type);
|
||||
MQUnifiedsensor MQ6(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin6, Type);
|
||||
MQUnifiedsensor MQ7(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin7, Type);
|
||||
MQUnifiedsensor MQ8(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin8, Type);
|
||||
MQUnifiedsensor MQ9(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin9, Type);
|
||||
MQUnifiedsensor MQ131(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin10, Type); //mq131
|
||||
MQUnifiedsensor MQ135(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin11, Type); //mq135
|
||||
MQUnifiedsensor MQ136(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin12, Type); //mq136
|
||||
MQUnifiedsensor MQ303A(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin13, Type); //mq303
|
||||
MQUnifiedsensor MQ309A(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin14, Type); //mq309
|
||||
|
||||
|
||||
test(adcMaxValue)
|
||||
{
|
||||
int adcVal=1023;
|
||||
int volt = MQ2.getVoltage(false, true, adcVal);
|
||||
int expVolt = 5;
|
||||
assertEqual(volt,expVolt);
|
||||
}
|
||||
|
||||
test(adcMinValue)
|
||||
{
|
||||
int adcVal=0;
|
||||
int volt = MQ2.getVoltage(false, true, adcVal);
|
||||
int expVolt = 0;
|
||||
assertEqual(volt,expVolt);
|
||||
}
|
||||
|
||||
test(adcMidValue)
|
||||
{
|
||||
int adcVal=410;
|
||||
int volt = MQ2.getVoltage(false, true, adcVal);
|
||||
int expVolt = 2;
|
||||
assertEqual(volt,expVolt);
|
||||
}
|
||||
|
||||
test(MQ2_LPG)
|
||||
{
|
||||
MQ2.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ2.setA(574.25); MQ2.setB(-2.222); // Configure the equation to to calculate LPG concentration
|
||||
MQ2.init();
|
||||
MQ2.setR0(10);
|
||||
|
||||
int ppmExp=464;
|
||||
int PPM = MQ2.setRsR0RatioGetPPM(1.1); // Send and Rs/R0 and return PPM (Using datasheet)
|
||||
|
||||
assertEqual(PPM,ppmExp);
|
||||
}
|
||||
|
||||
test(MQ3_Alcohol)
|
||||
{
|
||||
MQ3.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ3.setA(0.3934); MQ3.setB(-1.504); // Configure the equation to to calculate Alcohol concentration
|
||||
MQ3.init();
|
||||
MQ3.setR0(10);
|
||||
|
||||
int ppmExp=4;
|
||||
int PPM = MQ3.setRsR0RatioGetPPM(0.2); // Send and Rs/R0 and return PPM (Using datasheet)
|
||||
|
||||
assertEqual(PPM,ppmExp);
|
||||
}
|
||||
|
||||
|
||||
test(MQ4_Smoke)
|
||||
{
|
||||
MQ4.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ4.setA(30000000); MQ4.setB(-8.31); // Configure the equation to to calculate Smoke concentration
|
||||
MQ4.init();
|
||||
MQ4.setR0(10);
|
||||
|
||||
int ppmExp=297;
|
||||
int PPM = MQ4.setRsR0RatioGetPPM(4); // Send and Rs/R0 and return PPM (Using datasheet)
|
||||
|
||||
assertEqual(PPM,ppmExp);
|
||||
}
|
||||
|
||||
test(MQ5_LPG)
|
||||
{
|
||||
MQ5.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ5.setA(80.897); MQ5.setB(-2.431); // Configure the equation to to calculate LPG concentration
|
||||
MQ5.init();
|
||||
MQ5.setR0(10);
|
||||
|
||||
int ppmExp=192;
|
||||
int PPM = MQ5.setRsR0RatioGetPPM(0.7); // Send and Rs/R0 and return PPM (Using datasheet)
|
||||
|
||||
assertEqual(PPM,ppmExp);
|
||||
}
|
||||
|
||||
test(MQ6_CH4)
|
||||
{
|
||||
MQ6.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ6.setA(2127.2); MQ6.setB(-2.526); // Configure the equation to to calculate CH4 concentration
|
||||
MQ6.init();
|
||||
MQ6.setR0(10);
|
||||
|
||||
int ppmExp=2127;
|
||||
int PPM = MQ6.setRsR0RatioGetPPM(1); // Send and Rs/R0 and return PPM (Using datasheet)
|
||||
|
||||
assertEqual(PPM,ppmExp);
|
||||
}
|
||||
|
||||
test(MQ7_CH4)
|
||||
{
|
||||
MQ7.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ7.setA(99.042); MQ7.setB(-1.518); // Configure the equation to to calculate CH4 concentration
|
||||
MQ7.init();
|
||||
MQ7.setR0(10);
|
||||
|
||||
int ppmExp=1139;
|
||||
int PPM = MQ7.setRsR0RatioGetPPM(0.2); // Send and Rs/R0 and return PPM (Using datasheet)
|
||||
|
||||
assertEqual(PPM,ppmExp);
|
||||
}
|
||||
|
||||
test(MQ8_H2)
|
||||
{
|
||||
MQ8.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ8.setA(976.97); MQ8.setB(-0.688); // Configure the equation to to calculate H2 concentration
|
||||
MQ8.init();
|
||||
MQ8.setR0(10);
|
||||
|
||||
int ppmExp=2956;
|
||||
int PPM = MQ8.setRsR0RatioGetPPM(0.2); // Send and Rs/R0 and return PPM (Using datasheet)
|
||||
|
||||
assertEqual(PPM,ppmExp);
|
||||
}
|
||||
|
||||
|
||||
test(MQ9_CO)
|
||||
{
|
||||
MQ9.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ9.setA(599.65); MQ9.setB(-2.244); // Configure the equation to to calculate CO concentration
|
||||
MQ9.init();
|
||||
MQ9.setR0(10);
|
||||
|
||||
int ppmExp=989;
|
||||
int PPM = MQ9.setRsR0RatioGetPPM(0.8); // Send and Rs/R0 and return PPM (Using datasheet)
|
||||
|
||||
assertEqual(PPM,ppmExp);
|
||||
}
|
||||
|
||||
test(MQ131_O3)
|
||||
{
|
||||
MQ131.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ131.setA(23.943); MQ131.setB(-1.11); // Configure the equation to to calculate O3 concentration
|
||||
MQ131.init();
|
||||
MQ131.setR0(10);
|
||||
|
||||
int ppmExp=11;
|
||||
int PPM = MQ131.setRsR0RatioGetPPM(2); // Send and Rs/R0 and return PPM (Using datasheet)
|
||||
|
||||
assertEqual(PPM,ppmExp);
|
||||
}
|
||||
|
||||
|
||||
test(MQ135_CO2)
|
||||
{
|
||||
MQ135.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ135.setA(110.47); MQ135.setB(-2.862); // Configure the equation to to calculate CO2 concentration
|
||||
MQ135.init();
|
||||
MQ135.setR0(10);
|
||||
|
||||
int ppmExp=110;
|
||||
int PPM = MQ135.setRsR0RatioGetPPM(1); // Send and Rs/R0 and return PPM (Using datasheet)
|
||||
|
||||
assertEqual(PPM,ppmExp);
|
||||
}
|
||||
|
||||
test(MQ136_H2S)
|
||||
{
|
||||
MQ136.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ136.setA(36.737); MQ136.setB(-3.536); // Configure the equation to to calculate H2S concentration
|
||||
MQ136.init();
|
||||
MQ136.setR0(10);
|
||||
|
||||
int ppmExp=223;
|
||||
int PPM = MQ136.setRsR0RatioGetPPM(0.6); // Send and Rs/R0 and return PPM (Using datasheet)
|
||||
|
||||
assertEqual(PPM,ppmExp);
|
||||
}
|
||||
|
||||
test(MQ303A_Ethanol)
|
||||
{
|
||||
MQ303A.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ303A.setA(3.4916); MQ303A.setB(-2.432); // Configure the equation to to calculate Ethanol concentration
|
||||
MQ303A.init();
|
||||
MQ303A.setR0(10);
|
||||
|
||||
int ppmExp=944;
|
||||
int PPM = MQ303A.setRsR0RatioGetPPM(0.1); // Send and Rs/R0 and return PPM (Using datasheet)
|
||||
|
||||
assertEqual(PPM,ppmExp);
|
||||
}
|
||||
|
||||
test(MQ303A_VoltResolution)
|
||||
{
|
||||
float vRes = 4.7;
|
||||
MQ303A.setVoltResolution(vRes);
|
||||
MQ303A.setADC(100); // provide dummy ADC value
|
||||
float correctionFactor = 0; // Optional environmental correction
|
||||
MQ303A.readSensor(true, correctionFactor); // dummy read for MQ303A
|
||||
assertEqualFloat(MQ303A.getVoltResolution(), vRes);
|
||||
}
|
||||
|
||||
test(MQ309A_CO)
|
||||
{
|
||||
MQ309A.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ309A.setA(1000000); MQ309A.setB(-4.01); // Configure the equation to to calculate CO concentration
|
||||
MQ309A.init();
|
||||
MQ309A.setR0(10);
|
||||
|
||||
int ppmExp=757;
|
||||
int PPM = MQ309A.setRsR0RatioGetPPM(6); // Send and Rs/R0 and return PPM (Using datasheet)
|
||||
|
||||
assertEqual(PPM,ppmExp);
|
||||
}
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
while(!Serial) {} // Portability for Leonardo/Micro
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
Test::run();
|
||||
}
|
@ -15,7 +15,7 @@
|
||||
|
||||
Wiring:
|
||||
https://github.com/miguel5612/MQSensorsLib_Docs/blob/master/static/img/MQ_Arduino.PNG
|
||||
Please take care, arduino A0 pin represent the analog input configured on #define pin - For this example this doesn't matter
|
||||
Please make sure arduino A0 pin represents the analog input configured on #define pin - For this example this doesn't matter
|
||||
You will connect your sensor to your external A2D Sensor
|
||||
|
||||
This example code is in the public domain.
|
||||
@ -37,7 +37,7 @@
|
||||
//Definitions
|
||||
#define placa "Arduino UNO"
|
||||
#define Voltage_Resolution 5
|
||||
#define pin A0 //Analog input 0 -> This value can have any number, we recomend use A0
|
||||
#define pin A0 //Analog input 0 -> This value can have any number, we recommend use A0
|
||||
#define type "MQ-3 With A2D External connected" //MQ3
|
||||
#define ADC_Bit_Resolution 10 // Resolution of your external A2D
|
||||
#define RatioMQ3CleanAir 60 //RS / R0 = 60 ppm
|
||||
@ -52,7 +52,7 @@ void setup() {
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ3.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ3.setA(4.8387); MQ3.setB(-2.68); // Configurate the ecuation values to get Benzene concentration
|
||||
MQ3.setA(4.8387); MQ3.setB(-2.68); // Configure the equation to to calculate Benzene concentration
|
||||
/*
|
||||
Exponential regression:
|
||||
Gas | a | b
|
||||
@ -72,25 +72,25 @@ void setup() {
|
||||
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
// Explanation:
|
||||
// In this routine the sensor will measure the resistance of the sensor supposing before was pre-heated
|
||||
// and now is on clean air (Calibration conditions), and it will setup R0 value.
|
||||
// We recomend execute this routine only on setup or on the laboratory and save on the eeprom of your arduino
|
||||
// This routine not need to execute to every restart, you can load your R0 if you know the value
|
||||
// In this routine the sensor will measure the resistance of the sensor supposedly before being pre-heated
|
||||
// and on clean air (Calibration conditions), setting up R0 value.
|
||||
// We recommend executing this routine only on setup in laboratory conditions.
|
||||
// This routine does not need to be executed on each restart, you can load your R0 value from eeprom.
|
||||
// Acknowledgements: https://jayconsystems.com/blog/understanding-a-gas-sensor
|
||||
Serial.print("Calibrating please wait.");
|
||||
float calcR0 = 0;
|
||||
for(int i = 1; i<=10; i ++)
|
||||
{
|
||||
int yourA2DValue = random(0, 1024); // 10-bit emulation
|
||||
MQ3.setADC(yourA2DValue);// Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ3.setADC(yourA2DValue);// Update data, the arduino will read the voltage from the analog pin
|
||||
calcR0 += MQ3.calibrate(RatioMQ3CleanAir);
|
||||
Serial.print(".");
|
||||
}
|
||||
MQ3.setR0(calcR0/10);
|
||||
Serial.println(" done!.");
|
||||
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Conection issue founded, R0 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Conection issue founded, R0 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Connection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
|
||||
MQ3.serialDebug(true);
|
||||
@ -98,8 +98,9 @@ void setup() {
|
||||
|
||||
void loop() {
|
||||
int yourA2DValue = random(0, 1024); // 10-bit emulation
|
||||
MQ3.setADC(yourA2DValue); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ3.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
MQ3.setADC(yourA2DValue); // Update data, the arduino will read the voltage from the analog pin
|
||||
float correctionFactor = 0; // Optional environmental correction
|
||||
MQ3.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
MQ3.serialDebug(); // Will print the table on the serial port
|
||||
delay(500); //Sampling frequency
|
||||
}
|
@ -11,7 +11,7 @@
|
||||
|
||||
Wiring:
|
||||
https://github.com/miguel5612/MQSensorsLib_Docs/blob/master/static/img/MQ_Arduino.PNG
|
||||
Please take care, arduino A0 pin represent the analog input configured on #define pin
|
||||
Please make sure arduino A0 pin represents the analog input configured on #define pin
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
@ -37,7 +37,7 @@ void setup() {
|
||||
Serial.begin(115200);
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ4.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ4.setA(30000000); MQ4.setB(-8.308); // Configurate the ecuation values to get CH4 concentration
|
||||
MQ4.setA(30000000); MQ4.setB(-8.308); // Configure the equation to to calculate CH4 concentration
|
||||
/*
|
||||
Exponential regression:
|
||||
Gas | a | b
|
||||
@ -57,24 +57,24 @@ void setup() {
|
||||
*/
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
// Explanation:
|
||||
// In this routine the sensor will measure the resistance of the sensor supposing before was pre-heated
|
||||
// and now is on clean air (Calibration conditions), and it will setup R0 value.
|
||||
// We recomend execute this routine only on setup or on the laboratory and save on the eeprom of your arduino
|
||||
// This routine not need to execute to every restart, you can load your R0 if you know the value
|
||||
// In this routine the sensor will measure the resistance of the sensor supposedly before being pre-heated
|
||||
// and on clean air (Calibration conditions), setting up R0 value.
|
||||
// We recommend executing this routine only on setup in laboratory conditions.
|
||||
// This routine does not need to be executed on each restart, you can load your R0 value from eeprom.
|
||||
// Acknowledgements: https://jayconsystems.com/blog/understanding-a-gas-sensor
|
||||
Serial.print("Calibrating please wait.");
|
||||
float calcR0 = 0;
|
||||
for(int i = 1; i<=10; i ++)
|
||||
{
|
||||
MQ4.update(); // Update data, the arduino will be read the voltage on the analog pin
|
||||
MQ4.update(); // Update data, the arduino will read the voltage from the analog pin
|
||||
calcR0 += MQ4.calibrate(RatioMQ4CleanAir);
|
||||
Serial.print(".");
|
||||
}
|
||||
MQ4.setR0(calcR0/10);
|
||||
Serial.println(" done!.");
|
||||
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Conection issue founded, R0 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Conection issue founded, R0 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
|
||||
if(isinf(calcR0)) {Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
|
||||
if(calcR0 == 0){Serial.println("Warning: Connection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
|
||||
/***************************** MQ CAlibration ********************************************/
|
||||
MQ4.serialDebug(true);
|
||||
}
|
||||
@ -88,8 +88,9 @@ void setup() {
|
||||
//Read the sensor and print in serial port
|
||||
//Lecture will be saved in lecture variable
|
||||
MQ4.update();
|
||||
float smokePPM = MQ4.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
|
||||
if(smokePPM > 1000) {Serial.println("Warning: High concentrations of smoke detected")};
|
||||
float correctionFactor = 0; // Optional environmental correction
|
||||
float smokePPM = MQ4.readSensor(false, correctionFactor); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
|
||||
if(smokePPM > 1000) {Serial.println("Warning: High concentrations of smoke detected");}
|
||||
MQ4.serialDebug(); // Will print the table on the serial port
|
||||
delay(400);
|
||||
}
|
||||
|
29
keywords.txt
Normal file
29
keywords.txt
Normal file
@ -0,0 +1,29 @@
|
||||
MQUnifiedsensor KEYWORD1
|
||||
init KEYWORD2
|
||||
update KEYWORD2
|
||||
externalADCUpdate KEYWORD2
|
||||
setR0 KEYWORD2
|
||||
setRL KEYWORD2
|
||||
setA KEYWORD2
|
||||
setB KEYWORD2
|
||||
setRegressionMethod KEYWORD2
|
||||
setVoltResolution KEYWORD2
|
||||
setVCC KEYWORD2
|
||||
setPin KEYWORD2
|
||||
setADC KEYWORD2
|
||||
serialDebug KEYWORD2
|
||||
calibrate KEYWORD2
|
||||
readSensor KEYWORD2
|
||||
readSensorR0Rs KEYWORD2
|
||||
validateEcuation KEYWORD2
|
||||
getVoltage KEYWORD2
|
||||
setRsR0RatioGetPPM KEYWORD2
|
||||
getRS KEYWORD2
|
||||
stringTofloat KEYWORD2
|
||||
getA KEYWORD2
|
||||
getB KEYWORD2
|
||||
getR0 KEYWORD2
|
||||
getRL KEYWORD2
|
||||
getVoltResolution KEYWORD2
|
||||
getVCC KEYWORD2
|
||||
getRegressionMethod KEYWORD2
|
@ -1,10 +1,10 @@
|
||||
name=MQUnifiedsensor
|
||||
version=2.0.1
|
||||
version=3.0.5
|
||||
author= Miguel Califa <miguelangel5612@gmail.com>, Yersson Carrillo<miguelangel5612@gmail.com>, Ghiordy Contreras<miguelangel5612@gmail.com>
|
||||
maintainer= Miguel Califa <miguelangel5612@gmail.com>
|
||||
sentence= This library allows you to read the MQ sensors very easily.
|
||||
paragraph= This library allows an Arduino/Genuino/ESP8266 board to read MQ Sensors (AIr quality meter) references: MQ2, MQ3, MQ4, MQ5, MQ6, MQ7, MQ8, MQ9, MQ131, MQ135, MQ303A, MQ309A.
|
||||
category= Sensors
|
||||
url=https://github.com/miguel5612/MQSensorsLib
|
||||
architectures=avr
|
||||
architectures=avr,esp8266,esp32
|
||||
license=MIT
|
||||
|
@ -1,23 +1,61 @@
|
||||
#include "MQUnifiedsensor.h"
|
||||
#include <float.h>
|
||||
#include <math.h>
|
||||
|
||||
#define retries 2
|
||||
#define retry_interval 20
|
||||
|
||||
MQUnifiedsensor::MQUnifiedsensor(String Placa, float Voltage_Resolution, int ADC_Bit_Resolution, int pin, String type) {
|
||||
this->_pin = pin;
|
||||
Placa.toCharArray(this->_placa, 20);
|
||||
type.toCharArray(this->_type, 6);
|
||||
type.toCharArray(this->_type, 7);
|
||||
//this->_type = type; //MQ-2, MQ-3 ... MQ-309A
|
||||
//this->_placa = Placa;
|
||||
this-> _VOLT_RESOLUTION = Voltage_Resolution;
|
||||
this-> _VCC = Voltage_Resolution;
|
||||
this-> _ADC_Bit_Resolution = ADC_Bit_Resolution;
|
||||
}
|
||||
MQUnifiedsensor::MQUnifiedsensor(String Placa, String type) {
|
||||
Placa.toCharArray(this->_placa, 20);
|
||||
type.toCharArray(this->_type, 7);
|
||||
}
|
||||
void MQUnifiedsensor::init()
|
||||
{
|
||||
pinMode(_pin, INPUT);
|
||||
}
|
||||
static inline double safePow(double base, double exp){
|
||||
if(exp == 0.0) return 1.0;
|
||||
if(exp == 1.0) return base;
|
||||
if(exp == 2.0) return base * base;
|
||||
return pow(base, exp);
|
||||
}
|
||||
static inline bool willOverflow(double log_ppm){
|
||||
static const double maxLog = log10((double)FLT_MAX);
|
||||
static const double minLog = log10((double)FLT_MIN);
|
||||
return (log_ppm > maxLog || log_ppm < minLog);
|
||||
}
|
||||
|
||||
void MQUnifiedsensor::setA(float a) {
|
||||
this->_a = a;
|
||||
if(isinf(a) || isnan(a)) {
|
||||
this->_a = 0;
|
||||
} else if(a > MQ_MAX_A) {
|
||||
this->_a = MQ_MAX_A;
|
||||
} else if(a < -MQ_MAX_A) {
|
||||
this->_a = -MQ_MAX_A;
|
||||
} else {
|
||||
this->_a = a;
|
||||
}
|
||||
}
|
||||
void MQUnifiedsensor::setB(float b) {
|
||||
this->_b = b;
|
||||
if(isinf(b) || isnan(b)) {
|
||||
this->_b = 0;
|
||||
} else if(b > MQ_MAX_B) {
|
||||
this->_b = MQ_MAX_B;
|
||||
} else if(b < -MQ_MAX_B) {
|
||||
this->_b = -MQ_MAX_B;
|
||||
} else {
|
||||
this->_b = b;
|
||||
}
|
||||
}
|
||||
void MQUnifiedsensor::setR0(float R0) {
|
||||
this->_R0 = R0;
|
||||
@ -34,6 +72,13 @@ void MQUnifiedsensor::setVoltResolution(float voltage_resolution)
|
||||
{
|
||||
_VOLT_RESOLUTION = voltage_resolution;
|
||||
}
|
||||
void MQUnifiedsensor::setVCC(float vcc)
|
||||
{
|
||||
_VCC = vcc;
|
||||
}
|
||||
void MQUnifiedsensor::setPin(int pin) {
|
||||
this->_pin = pin;
|
||||
}
|
||||
void MQUnifiedsensor::setRegressionMethod(int regressionMethod)
|
||||
{
|
||||
//this->_regressionMethod = regressionMethod;
|
||||
@ -49,6 +94,10 @@ float MQUnifiedsensor::getVoltResolution()
|
||||
{
|
||||
return _VOLT_RESOLUTION;
|
||||
}
|
||||
float MQUnifiedsensor::getVCC()
|
||||
{
|
||||
return _VCC;
|
||||
}
|
||||
String MQUnifiedsensor::getRegressionMethod()
|
||||
{
|
||||
if(_regressionMethod == 1) return "Exponential";
|
||||
@ -75,7 +124,8 @@ void MQUnifiedsensor::serialDebug(bool onSetup)
|
||||
Serial.println("Contributors: Andres A. Martinez - Juan A. Rodríguez - Mario A. Rodríguez O ");
|
||||
|
||||
Serial.print("Sensor: "); Serial.println(_type);
|
||||
Serial.print("Supply voltage: "); Serial.print(_VOLT_RESOLUTION); Serial.println(" VDC");
|
||||
Serial.print("ADC voltage: "); Serial.print(_VOLT_RESOLUTION); Serial.println(" VDC");
|
||||
Serial.print("Sensor supply (VCC): "); Serial.print(_VCC); 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Ω");
|
||||
@ -96,7 +146,7 @@ void MQUnifiedsensor::serialDebug(bool onSetup)
|
||||
else
|
||||
{
|
||||
Serial.print("|"); Serial.print(_adc); Serial.print("| v = ADC*"); Serial.print(_VOLT_RESOLUTION); Serial.print("/"); Serial.print((pow(2, _ADC_Bit_Resolution)) - 1); 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(" | RS = ((" ); Serial.print(_VCC ); Serial.print("*RL)/Voltage) - RL| "); Serial.print(_RS_Calc); Serial.print(" | Ratio = RS/R0| ");
|
||||
Serial.print(_ratio); Serial.print( " | ");
|
||||
if(_regressionMethod == 1) Serial.print("ratio*a + b");
|
||||
else Serial.print("pow(10, (log10(ratio)-b)/a)");
|
||||
@ -108,60 +158,92 @@ void MQUnifiedsensor::update()
|
||||
{
|
||||
_sensor_volt = this->getVoltage();
|
||||
}
|
||||
void MQUnifiedsensor::externalADCUpdate(float volt)
|
||||
{
|
||||
_sensor_volt = volt;
|
||||
}
|
||||
float MQUnifiedsensor::validateEcuation(float ratioInput)
|
||||
{
|
||||
//Serial.print("Ratio input: "); Serial.println(ratioInput);
|
||||
//Serial.print("a: "); Serial.println(_a);
|
||||
//Serial.print("b: "); Serial.println(_b);
|
||||
//Usage of this function: Unit test on ALgorithmTester example;
|
||||
if(_regressionMethod == 1) _PPM= _a*pow(ratioInput, _b);
|
||||
else
|
||||
{
|
||||
// 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
|
||||
double ppm;
|
||||
if(_regressionMethod == 1){
|
||||
if(ratioInput <= 0 || _a == 0) return 0;
|
||||
double logppm = log10((double)_a) + (double)_b * log10((double)ratioInput);
|
||||
if(willOverflow(logppm)) ppm = (logppm > 0) ? FLT_MAX : 0.0;
|
||||
else ppm = safePow(10.0, logppm);
|
||||
}
|
||||
//Serial.println("Regression Method: "); Serial.println(_regressionMethod);
|
||||
//Serial.println("Result: "); Serial.println(_PPM);
|
||||
return _PPM;
|
||||
}
|
||||
float MQUnifiedsensor::readSensor()
|
||||
{
|
||||
//More explained in: https://jayconsystems.com/blog/understanding-a-gas-sensor
|
||||
_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 = 0; //No negative values accepted or upper datasheet recomendation.
|
||||
if(_regressionMethod == 1) _PPM= _a*pow(_ratio, _b); // <- Source excel analisis https://github.com/miguel5612/MQSensorsLib_Docs/tree/master/Internal_design_documents
|
||||
else
|
||||
else
|
||||
{
|
||||
// https://jayconsystems.com/blog/understanding-a-gas-sensor <- Source of linear ecuation
|
||||
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
|
||||
if(ratioInput <= 0 || _a == 0) return 0;
|
||||
double logppm = (log10((double)ratioInput)-(double)_b)/(double)_a;
|
||||
if(willOverflow(logppm)) ppm = (logppm > 0) ? FLT_MAX : 0.0;
|
||||
else ppm = safePow(10.0, logppm);
|
||||
}
|
||||
if(_PPM < 0) _PPM = 0; //No negative values accepted or upper datasheet recomendation.
|
||||
//if(_PPM > 10000) _PPM = 99999999; //No negative values accepted or upper datasheet recomendation.
|
||||
if(isinf(ppm) || isnan(ppm)) ppm = FLT_MAX;
|
||||
_PPM = (float)ppm;
|
||||
return _PPM;
|
||||
}
|
||||
float MQUnifiedsensor::readSensorR0Rs()
|
||||
float MQUnifiedsensor::readSensor(bool isMQ303A, float correctionFactor, bool injected)
|
||||
{
|
||||
//More explained in: https://jayconsystems.com/blog/understanding-a-gas-sensor
|
||||
_RS_Calc = ((_VOLT_RESOLUTION*_RL)/_sensor_volt)-_RL; //Get value of RS in a gas
|
||||
|
||||
float voltRes = _VOLT_RESOLUTION; // preserve global resolution
|
||||
if(isMQ303A) {
|
||||
voltRes = voltRes - 0.45; //Calculations for RS using mq303a sensor look wrong #42
|
||||
}
|
||||
_RS_Calc = ((_VCC*_RL)/_sensor_volt)-_RL; //Get value of RS in a gas
|
||||
if(_RS_Calc < 0) _RS_Calc = 0; //No negative values accepted.
|
||||
if(!injected) _ratio = _RS_Calc / this->_R0; // Get ratio RS_gas/RS_air
|
||||
_ratio += correctionFactor;
|
||||
if(_ratio <= 0) _ratio = 0; //No negative values accepted or upper datasheet recommendation.
|
||||
double ppm;
|
||||
if(_regressionMethod == 1){
|
||||
if(_ratio <= 0 || _a == 0) return 0;
|
||||
double logppm = log10((double)_a) + (double)_b * log10((double)_ratio);
|
||||
if(willOverflow(logppm)) ppm = (logppm > 0) ? FLT_MAX : 0.0;
|
||||
else ppm = safePow(10.0, logppm);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(_ratio <= 0 || _a == 0) return 0;
|
||||
double logppm = (log10((double)_ratio)-(double)_b)/(double)_a;
|
||||
if(willOverflow(logppm)) ppm = (logppm > 0) ? FLT_MAX : 0.0;
|
||||
else ppm = safePow(10.0, logppm);
|
||||
}
|
||||
if(ppm < 0) ppm = 0; //No negative values accepted or upper datasheet recommendation.
|
||||
if(isinf(ppm) || isnan(ppm)) ppm = FLT_MAX;
|
||||
_PPM = (float)ppm;
|
||||
//if(_PPM > 10000) _PPM = 99999999; //No negative values accepted or upper datasheet recommendation.
|
||||
return _PPM;
|
||||
}
|
||||
float MQUnifiedsensor::readSensorR0Rs(float correctionFactor)
|
||||
{
|
||||
//More explained in: https://jayconsystems.com/blog/understanding-a-gas-sensor
|
||||
_RS_Calc = ((_VCC*_RL)/_sensor_volt)-_RL; //Get value of RS in a gas
|
||||
if(_RS_Calc < 0) _RS_Calc = 0; //No negative values accepted.
|
||||
_ratio = this->_R0/_RS_Calc; // Get ratio RS_air/RS_gas <- INVERTED for MQ-131 issue 28 https://github.com/miguel5612/MQSensorsLib/issues/28
|
||||
if(_ratio <= 0) _ratio = 0; //No negative values accepted or upper datasheet recomendation.
|
||||
if(_regressionMethod == 1) _PPM= _a*pow(_ratio, _b); // <- Source excel analisis https://github.com/miguel5612/MQSensorsLib_Docs/tree/master/Internal_design_documents
|
||||
else
|
||||
{
|
||||
// https://jayconsystems.com/blog/understanding-a-gas-sensor <- Source of linear ecuation
|
||||
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
|
||||
_ratio += correctionFactor;
|
||||
if(_ratio <= 0) _ratio = 0; //No negative values accepted or upper datasheet recommendation.
|
||||
double ppm;
|
||||
if(_regressionMethod == 1){
|
||||
if(_ratio <= 0 || _a == 0) return 0;
|
||||
double logppm = log10((double)_a) + (double)_b * log10((double)_ratio);
|
||||
if(willOverflow(logppm)) ppm = (logppm > 0) ? FLT_MAX : 0.0;
|
||||
else ppm = safePow(10.0, logppm);
|
||||
}
|
||||
if(_PPM < 0) _PPM = 0; //No negative values accepted or upper datasheet recomendation.
|
||||
//if(_PPM > 10000) _PPM = 99999999; //No negative values accepted or upper datasheet recomendation.
|
||||
else
|
||||
{
|
||||
if(_ratio <= 0 || _a == 0) return 0;
|
||||
double logppm = (log10((double)_ratio)-(double)_b)/(double)_a;
|
||||
if(willOverflow(logppm)) ppm = (logppm > 0) ? FLT_MAX : 0.0;
|
||||
else ppm = safePow(10.0, logppm);
|
||||
}
|
||||
if(ppm < 0) ppm = 0; //No negative values accepted or upper datasheet recommendation.
|
||||
if(isinf(ppm) || isnan(ppm)) ppm = FLT_MAX;
|
||||
_PPM = (float)ppm;
|
||||
//if(_PPM > 10000) _PPM = 99999999; //No negative values accepted or upper datasheet recommendation.
|
||||
return _PPM;
|
||||
}
|
||||
float MQUnifiedsensor::calibrate(float ratioInCleanAir) {
|
||||
float MQUnifiedsensor::calibrate(float ratioInCleanAir, float correctionFactor) {
|
||||
//More explained in: https://jayconsystems.com/blog/understanding-a-gas-sensor
|
||||
/*
|
||||
V = I x R
|
||||
@ -176,13 +258,14 @@ float MQUnifiedsensor::calibrate(float ratioInCleanAir) {
|
||||
*/
|
||||
float RS_air; //Define variable for sensor resistance
|
||||
float R0; //Define variable for R0
|
||||
RS_air = ((_VOLT_RESOLUTION*_RL)/_sensor_volt)-_RL; //Calculate RS in fresh air
|
||||
RS_air = ((_VCC*_RL)/_sensor_volt)-_RL; //Calculate RS in fresh air
|
||||
if(RS_air < 0) RS_air = 0; //No negative values accepted.
|
||||
R0 = RS_air/ratioInCleanAir; //Calculate R0
|
||||
R0 = RS_air/ratioInCleanAir; //Calculate R0
|
||||
R0 += correctionFactor;
|
||||
if(R0 < 0) R0 = 0; //No negative values accepted.
|
||||
return R0;
|
||||
}
|
||||
float MQUnifiedsensor::getVoltage(int read) {
|
||||
float MQUnifiedsensor::getVoltage(bool read, bool injected, int value) {
|
||||
float voltage;
|
||||
if(read)
|
||||
{
|
||||
@ -194,13 +277,31 @@ float MQUnifiedsensor::getVoltage(int read) {
|
||||
}
|
||||
voltage = (avg/ retries) * _VOLT_RESOLUTION / ((pow(2, _ADC_Bit_Resolution)) - 1);
|
||||
}
|
||||
else
|
||||
else if(!injected)
|
||||
{
|
||||
voltage = _sensor_volt;
|
||||
}
|
||||
else
|
||||
{
|
||||
voltage = (value) * _VOLT_RESOLUTION / ((pow(2, _ADC_Bit_Resolution)) - 1);
|
||||
_sensor_volt = voltage; //to work on testing
|
||||
}
|
||||
return voltage;
|
||||
}
|
||||
float MQUnifiedsensor:: setRsR0RatioGetPPM(float value)
|
||||
{
|
||||
_ratio = value;
|
||||
return readSensor(false, 0, true);
|
||||
}
|
||||
float MQUnifiedsensor::getRS()
|
||||
{
|
||||
//More explained in: https://jayconsystems.com/blog/understanding-a-gas-sensor
|
||||
_RS_Calc = ((_VCC*_RL)/_sensor_volt)-_RL; //Get value of RS in a gas
|
||||
if(_RS_Calc < 0) _RS_Calc = 0; //No negative values accepted.
|
||||
return _RS_Calc;
|
||||
}
|
||||
|
||||
float MQUnifiedsensor::stringTofloat(String & str)
|
||||
{
|
||||
return atof( str.c_str() );
|
||||
}
|
||||
}
|
||||
|
@ -3,34 +3,40 @@
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <stdint.h>
|
||||
#include <float.h>
|
||||
#include <math.h>
|
||||
|
||||
// Maximum coefficients allowed for the regression model
|
||||
#define MQ_MAX_A 1e30
|
||||
#define MQ_MAX_B 100.0
|
||||
|
||||
/***********************Software Related Macros************************************/
|
||||
|
||||
#define ADC_RESOLUTION 10 // for 10bit analog to digital converter.
|
||||
#define retries 2
|
||||
#define retry_interval 20
|
||||
|
||||
class MQUnifiedsensor
|
||||
{
|
||||
public:
|
||||
MQUnifiedsensor(String Placa = "Arduino", float Voltage_Resolution = 5, int ADC_Bit_Resolution = 10, int pin = 1, String type = "CUSTOM MQ");
|
||||
MQUnifiedsensor(String Placa = "Arduino", String type = "CUSTOM MQ");
|
||||
|
||||
//Functions to set values
|
||||
void init();
|
||||
void update();
|
||||
void externalADCUpdate(float volt);
|
||||
void setR0(float R0 = 10);
|
||||
void setRL(float RL = 10);
|
||||
void setA(float a);
|
||||
void setB(float b);
|
||||
void setRegressionMethod(int regressionMethod);
|
||||
void setVoltResolution(float voltage_resolution = 5);
|
||||
void setVCC(float vcc = 5);
|
||||
void setPin(int pin = 1);
|
||||
void serialDebug(bool onSetup = false); //Show on serial port information about sensor
|
||||
void setADC(int value); //For external ADC Usage
|
||||
|
||||
//user functions
|
||||
float calibrate(float ratioInCleanAir);
|
||||
float readSensor();
|
||||
float readSensorR0Rs();
|
||||
float calibrate(float ratioInCleanAir, float correctionFactor = 0.0);
|
||||
float readSensor(bool isMQ303A = false, float correctionFactor = 0.0, bool injected=false);
|
||||
float readSensorR0Rs(float correctionFactor = 0.0);
|
||||
float validateEcuation(float ratioInput = 0);
|
||||
|
||||
//get function for info
|
||||
@ -39,24 +45,29 @@ class MQUnifiedsensor
|
||||
float getR0();
|
||||
float getRL();
|
||||
float getVoltResolution();
|
||||
float getVCC();
|
||||
String getRegressionMethod();
|
||||
float getVoltage(int read = true);
|
||||
|
||||
float stringTofloat(String & str);
|
||||
float getVoltage(bool read = true, bool injected = false, int value = 0);
|
||||
float stringTofloat(String & str);
|
||||
|
||||
// functions for testing
|
||||
float getRS();
|
||||
float setRsR0RatioGetPPM(float value);
|
||||
|
||||
private:
|
||||
/************************Private vars************************************/
|
||||
byte _pin;
|
||||
byte _pin = 1;
|
||||
byte _firstFlag = false;
|
||||
byte _VOLT_RESOLUTION = 5.0; // if 3.3v use 3.3
|
||||
byte _RL = 10; //Value in KiloOhms
|
||||
float _VOLT_RESOLUTION = 5.0; // if 3.3v use 3.3
|
||||
float _VCC = 5.0; // Sensor supply voltage
|
||||
float _RL = 10; //Value in KiloOhms
|
||||
byte _ADC_Bit_Resolution = 10;
|
||||
byte _regressionMethod = 1; // 1 -> Exponential || 2 -> Linear
|
||||
|
||||
float _adc, _a, _b, _sensor_volt;
|
||||
float _R0, RS_air, _ratio, _PPM, _RS_Calc;
|
||||
|
||||
char _type[6];
|
||||
char _type[7];
|
||||
char _placa[20];
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user