Compare commits

...

71 Commits

Author SHA1 Message Date
Miguel Angel Califa Urquiza
c927504bf5 Update readme and contributing files 2023-10-04 16:21:25 -05:00
Miguel Angel Califa Urquiza
337d962862
Merge pull request #64 from adrcunha/master
Allow setting the ADC pin of a MQUnifiedsensor object.
2023-05-16 11:21:45 -05:00
Adriano Cunha
f7f2c1f3f4 Allow setting the ADC pin of a MQUnifiedsensor object.
The ADC pin can only be set when using the full/long constructor, causing the short constructor (name/type only) useless because the pin is unset.

Also default the ADC pin to 1 for consistency sake.
2023-04-11 14:30:49 -07:00
Miguel Angel Califa Urquiza
1c131eed65
Merge pull request #61 from adrcunha/master
Cleanup general definitions in MQUnifiedSensor.h
2023-04-11 07:13:18 -05:00
Adriano Cunha
b6bff185f3 Cleanup general definitions in MQUnifiedSensor.h
ADC_RESOLUTION is unused, and retries and retry_interval are only used by getVoltage().
Thus, remove ADC_RESOLUTION and move retries and retry_interval to MQUnifiedsensor.cpp.
Furthermore, these three definitions have generic names so it's easy to have them
conflict with a user's sketch that imports MQUnifiedSensor.h.
2023-04-03 08:53:13 -07:00
Miguel Angel Califa Urquiza
a7e89570fd
Merge pull request #53 from SMH17/patch-1
Update README.md
2023-03-15 16:35:38 -05:00
Miguel Angel Califa Urquiza
35180270ac
Merge pull request #56 from PsySc0rpi0n/patch-2
Update MQUnifiedsensor.cpp
2023-03-15 16:35:26 -05:00
Miguel Angel Califa Urquiza
52bccfbff1
Merge pull request #57 from PsySc0rpi0n/patch-3
Update MQUnifiedsensor.h
2023-03-15 16:35:12 -05:00
PsySc0rpi0n
3368613dd7
Update MQUnifiedsensor.h 2022-09-07 22:24:54 +01:00
PsySc0rpi0n
9f41530cda
Update MQUnifiedsensor.cpp
Change the `_type` array size from 6 to 7 so that it can display sensor name's with 6 characters or more.
2022-09-07 22:23:27 +01:00
Silvio Marano
e862774fe0
Update README.md
These sensors are all made by HANWEI Electronics, Sparkfun etc. are just resellers.
2022-08-03 01:53:49 +02:00
miguel5612
27465dfb99 Launched 3.0 Official! 2022-03-21 14:36:26 -05:00
miguel5612
e3f5e2ec0c Added unit tests! to validate library calculations 2022-03-21 14:32:37 -05:00
miguel5612
6a035cc5d0 Fix in regression of MQ136 2022-03-21 14:19:48 -05:00
miguel5612
80025e8398 Little changes 2022-03-21 08:18:41 -05:00
miguel5612
d3241a1e2f Added MQ136 Example 2022-03-21 08:17:54 -05:00
miguel5612
4b71c626b0 Fixed comment mentioned #29 2022-03-20 12:59:19 -05:00
miguel5612
07b6948f76 Updated and fixed typo examples 2022-03-20 12:53:36 -05:00
Miguel Angel Califa Urquiza
990e6878ef
Merge pull request #51 from fredolaredo/patch-1
some typo recommendations
2022-03-20 12:44:45 -05:00
miguel5612
6322e9c03d Following comment in #31 we added example 2022-03-20 12:27:54 -05:00
miguel5612
964204706f Added correction factor for Rs/R0 (Ratio) 2022-03-20 09:48:02 -05:00
miguel5612
1f8310a6e7 Changed type of RL internally to fix bug #37 2022-03-20 09:44:30 -05:00
miguel5612
700d367d93 Fix Rs calculation for MQ303A 2022-03-20 09:32:31 -05:00
miguel5612
4be0a40997 Remove delay 2022-03-20 08:50:06 -05:00
miguel5612
6aa78fd575 Bug 44 added heating flags 2022-03-20 08:48:12 -05:00
miguel5612
c934152cd7 Added MQ135-ADS1115 example and feature 2022-03-20 08:28:45 -05:00
fredolaredo
525a09644b
some typo recommendations
Hi Miguel, thanks you for the Library, here you will find some typo recommendations. not an expert in english myself .
2022-03-19 15:25:57 +01:00
Miguel Angel Califa Urquiza
16affe479f
Merge pull request #49 from vincenzomanzoni/patch-1
_VOLT_RESOLUTION data type should be float
2022-02-06 17:17:59 -05:00
Miguel Angel Califa Urquiza
4a3d46f47e
Merge pull request #46 from Prabhuelectro/patch-1
Update smokeDetector.ino
2022-02-06 17:17:48 -05:00
Vincenzo Manzoni
bf6d90db69
_VOLT_RESOLUTION data type changed to float
_VOLT_RESOLUTION can be either 5 or 3.3 V. However, 3.3 V cannot be stored properly in a byte data type and it requires a float data type.
2021-11-03 00:31:39 +01:00
Prabhu
1818c3cf1a
Update smokeDetector.ino
Semi colon before the curly bracket.
2021-05-10 13:27:40 +05:30
Miguel Angel Califa Urquiza
874be50106
Merge pull request #33 from Yoimer/fix-esp32-measuring-issue-when-connected-to-wifi
added esp32-wroom-32d board, fixed the measuring issue when connected…
2020-08-31 19:29:35 -05:00
Yoimer Román
71c0d82314 added esp32-wroom-32d board, fixed the measuring issue when connected to wifi. added esp8266 and esp32 and library.properties to avoid avr warning on arduino ide when compiling 2020-08-23 21:19:35 -04:00
Miguel Angel Califa Urquiza
11bef492e5
Update README.md 2020-07-09 22:01:09 -05:00
Miguel Angel Califa Urquiza
728450025a
Update README.md 2020-07-09 21:56:49 -05:00
Miguel Angel Califa Urquiza
a059971123
Update README.md 2020-07-09 21:55:09 -05:00
Miguel Angel Califa Urquiza
e9425f7047
Update README.md 2020-07-09 21:54:03 -05:00
miguel5612
beab80c403 Added offset of 400 ppm of CO2 on example MQ135 2020-05-31 10:58:00 -05:00
miguel5612
688827ffb4 Fixed MQ-131 Issue 2020-04-20 19:40:27 -05:00
miguel5612
b9a5e62899 Updated examples (Issue 26) 2020-04-20 19:23:45 -05:00
Miguel Angel Califa Urquiza
a906388c5c
Update README.md 2020-04-20 19:06:00 -05:00
Miguel Angel Califa Urquiza
948afddf0d
Update README.md 2020-04-20 19:05:08 -05:00
Miguel Angel Califa Urquiza
090de8f9e0
Update README.md 2020-04-11 10:48:04 -05:00
Miguel Angel Califa Urquiza
5053b10498
Create pull_request_template.md 2020-04-04 10:57:41 -05:00
Miguel Angel Califa Urquiza
8a87cce57e
Update README.md 2020-04-04 10:38:43 -05:00
Miguel Angel Califa Urquiza
972c79578e
Update README.md 2020-04-04 10:36:12 -05:00
Miguel Angel Califa Urquiza
4246716355
Update README.md 2020-04-04 10:35:52 -05:00
Miguel Angel Califa Urquiza
1fe32c4b6e
Update README.md 2020-04-04 10:21:19 -05:00
miguel5612
39b4449899 Added parenthesis to make clear the operation 2020-04-04 10:16:15 -05:00
miguel5612
edd573ba0f Updated example and created release 2020-04-04 10:12:30 -05:00
miguel5612
6ecc769676 Updated example 2020-04-04 09:52:56 -05:00
miguel5612
e07c4b7f71 Added example for ESP-32 2020-04-04 09:52:34 -05:00
miguel5612
952a456a35 Fixed board name 2020-04-04 09:51:16 -05:00
miguel5612
ff0cb0562a Fixed ADC_Bit_Resolution for _ADC_Bit_Resolution 2020-04-04 09:50:14 -05:00
Miguel Angel Califa Urquiza
f8d1bcbc76
Update README.md 2020-04-04 09:38:27 -05:00
Miguel Angel Califa Urquiza
7c3cd5793c
Update README.md 2020-04-04 09:23:47 -05:00
Miguel Angel Califa Urquiza
38f93037ce
Update README.md 2020-04-02 11:02:50 -05:00
Miguel Angel Califa Urquiza
b9ba249140
Update README.md 2020-04-02 11:01:56 -05:00
Miguel Angel Califa Urquiza
a7554fa8dd
Update README.md 2020-04-02 11:01:02 -05:00
Miguel Angel Califa Urquiza
f93b5128ce
Update README.md 2020-04-02 09:56:03 -05:00
Miguel Angel Califa Urquiza
1a14e12450
Removed excel - Not needed now 2020-04-02 09:52:30 -05:00
Miguel Angel Califa Urquiza
9bfea58092
Update README.md 2020-04-02 09:51:43 -05:00
Miguel Angel Califa Urquiza
555e49d31e
Update README.md 2020-04-02 09:35:18 -05:00
miguel5612
d0b00b4025 Updated props 2020-03-30 20:04:02 -05:00
miguel5612
f83e0fb0d7 Fixed bit comparation to strin 2020-03-30 19:54:22 -05:00
miguel5612
65a1246e51 Fixed ESP8266 volt resolution 3.3 2020-03-30 19:51:23 -05:00
miguel5612
28ce4c80f7 Removed setR0 before calibratioN 2020-03-30 19:46:49 -05:00
miguel5612
9497574e11 Fixed issue on calibratioN 2020-03-30 19:17:25 -05:00
miguel5612
9cbc753758 Updated MQ-board examples 2020-03-30 18:58:07 -05:00
miguel5612
c7b67d7fa5 Updated examples 2020-03-30 18:40:55 -05:00
miguel5612
6b3c764b71 Trying options for version 1.10 2020-03-30 12:39:13 -05:00
38 changed files with 1904 additions and 782 deletions

51
.github/pull_request_template.md vendored Normal file
View File

@ -0,0 +1,51 @@
# Description
Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.
Fixes # (issue)
## Type of change
Please delete options that are not relevant.
- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] 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?
Please describe the examples that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration
- [ ] MQ-3
- [ ] MQ-4
- [ ] MQ-5
- [ ] MQ-6
- [ ] MQ-7
- [ ] MQ-8
- [ ] MQ-9
- [ ] MQ-131
- [ ] MQ-135
- [ ] MQ-303
- [ ] MQ-309
- [ ] ESP8266
- [ ] ESP-32
**Test Configuration**:
* Board:
* Software (Version of your arduino):
* Result (Good - Medium - Bad):
* Your Comments:
# Checklist:
- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged and published in downstream modules

View File

@ -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.

163
README.md
View File

@ -1,57 +1,137 @@
<!-- PROJECT SHIELDS -->
<!--
*** I'm using markdown "reference style" links for readability.
*** Reference links are enclosed in brackets [ ] instead of parentheses ( ).
*** See the bottom of this document for the declaration of the reference variables
*** for contributors-url, forks-url, etc. This is an optional, concise syntax you may use.
*** https://www.markdownguide.org/basic-syntax/#reference-style-links
-->
[![DOI](https://zenodo.org/badge/170540207.svg)](https://zenodo.org/badge/latestdoi/170540207)
![Build Status](https://travis-ci.org/dwyl/esta.svg?branch=master)
![Build Status][build-url]
[![Contributors][contributors-shield]][contributors-url]
[![Forks][forks-shield]][forks-url]
[![Stargazers][stars-shield]][stars-url]
[![Issues][issues-shield]][issues-url]
[![MIT License][license-shield]][license-url]
[![LinkedIn][linkedin-shield]][linkedin-url]
# MQSensorsLib
This is a unified library to use sensors MQ: 2, 3, 4, 5, 6, 7, 8, 9, 131, 135, 303A and 309A.
We present a unified library for MQ sensors, this library allows to read MQ signals easily from Arduino, Genuino, ESP8266, ESP-32 boards whose references are MQ2, MQ3, MQ4, MQ5, MQ6, MQ7, MQ8, MQ9, MQ131, MQ135, MQ303A, MQ309A.
<!-- TABLE OF CONTENTS -->
## Table of Contents
* [Getting Started](#Getting-Started)
* [Wiring](#Wiring)
* [Sensor](#Sensor)
* [Arduino](#Arduino)
* [ESP8266 or ESP-32](#ESP8266-ESP32)
* [User Manual](#Manuals)
* [Sensor manufacturers](#Sensor-manufacturers)
* [Contributing](#Contributing)
* [Authors](#Authors)
* [Be a sponsor 💖](#Sponsor)
## Getting Started
```
#define placa "Arduino UNO"
#define Voltage_Resolution 5
#define pin A0 //Analog input 0 of your arduino
#define type "MQ-4" //MQ4
#define ADC_Bit_Resolution 10 // For arduino UNO/MEGA/NANO
MQUnifiedsensor MQ4(placa, Voltage_Resolution, ADC_Bit_Resolution, pin, type); //Example if sensor is MQ4 on Arduino UNO board
//Include the library
#include <MQUnifiedsensor.h>
/************************Hardware Related Macros************************************/
#define Board ("Arduino UNO")
#define Pin (A4) //Analog input 4 of your arduino
/***********************Software Related Macros************************************/
#define Type ("MQ-4") //MQ4
#define Voltage_Resolution (5)
#define ADC_Bit_Resolution (10) // For arduino UNO/MEGA/NANO
#define RatioMQ4CleanAir (4.4) //RS / R0 = 60 ppm
/*****************************Globals***********************************************/
//Declare Sensor
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.setR0(3.86018237);
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();
MQ4.update();
float ppmCH4 = MQ4.readSensor();
```
## Wiring
### Sensor
#### Important points:
##### Points you should identify
* VCC -> 5V Power supply (+) wire
* GND -> GND Ground (-) wire
* AO -> Analog Output of the sensor
##### Data of board that you should have
* RL Value in KOhms
##### Graph
![Wiring_MQSensor](https://raw.githubusercontent.com/miguel5612/MQSensorsLib_Docs/master/static/img/Points_explanation.jpeg)
#### RS/R0 value (From datasheet of your sensor)
* RS/R0 (Clean air - English) -> (Aire puro - Spanish)
* **Note**: RS/R0 is equal to Ratio variable on the program
![Graph from datasheet](https://raw.githubusercontent.com/miguel5612/MQSensorsLib_Docs/master/static/img/Graph_Explanation.jpeg)
### Arduino
![Arduino_Wiring_MQSensor](https://raw.githubusercontent.com/miguel5612/MQSensorsLib_Docs/master/static/img/MQ_Arduino.PNG)
### ESP8266
#### MQ-7 / MQ-309A
** Note ** [issue](https://github.com/miguel5612/MQSensorsLib/issues/26): MQ-7 and MQ-309 needs two different voltages for heater, they can be supplied by PWM and DC Signal controlled by your controller, another option is to use two different power sources, you should use the best option for you, next i will show the PWM option and on the examples this will be the way .
![MQ-7_MQ-309](https://raw.githubusercontent.com/miguel5612/MQSensorsLib_Docs/master/static/img/MQ-309_MQ-7.PNG)
### ESP8266-ESP32
![ESP8266_Wiring_MQSensor](https://raw.githubusercontent.com/miguel5612/MQSensorsLib_Docs/master/static/img/MQ_ESP8266.PNG)
### User Manual New!! 12.2019
[Manual](https://drive.google.com/open?id=1BAFInlvqKR7h81zETtjz4_RC2EssvFWX)
### 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.
[Excel_Help_Spreadsheet (Fill only Volaje Between RL - RS - RL Values)](https://drive.google.com/open?id=1MKDcudQ7BHL_vLGi-lgPh9-pblvygRMq)
### 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
```
MQ2.serialDebug(true);
```
* And on Loop Wrote
```
MQ2.serialDebug();
```
* Result:
![Serial debug output](https://github.com/miguel5612/MQSensorsLib_Docs/blob/master/static/img/Serial_Mon_Explanation.jpeg?raw=true)
**Note**:
* ![#c5f015](https://placehold.it/15/c5f015/000000?text=+) `Yellow -> Calibration status.`
* ![#008000](https://placehold.it/15/008000/000000?text=+) `Green -> Hardware and software characteristics.`
* ![#f03c15](https://placehold.it/15/f03c15/000000?text=+) `Red -> Headers of the library calculations.`
* Only valid for **1** gas sensor readings.
**Usage**
* Quick troubleshooting, since it shows everything the library does and the results of the calculations in each function.
### Prerequisites
You'll need Arduino desktop app 1.8.9 or later.
### Sensor manufacture:
### 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) |
@ -97,6 +177,10 @@ Examples/MQ-board.ino
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)
@ -105,8 +189,8 @@ Please read [CONTRIBUTING.md](https://github.com/miguel5612/MQSensorsLib/blob/ma
## Collaborators
* **Andres A. Martinez.**
* **Juan A. Rodríguez.** - [*Github*](https://github.com/Obiot24)
* **Andres A. Martinez.** - [*Github*](https://github.com/andresmacsi) - [CV](https://www.linkedin.com/in/andr%C3%A9s-acevedo-mart%C3%ADnez-73ab35185/?originalSubdomain=co)
* **Juan A. Rodríguez.** - [*Github*](https://github.com/Obiot24) - [CV]()
* **Mario A. Rodríguez O.** - [*GitHub*](https://github.com/MarioAndresR) - [CV](https://scienti.colciencias.gov.co/cvlac/visualizador/generarCurriculoCv.do?cod_rh=0000111304)
See also the list of [contributors](https://github.com/miguel5612/MQSensorsLib/contributors) who participated in this project.
@ -158,3 +242,26 @@ This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md
doi = {10.5281/zenodo.3384301},
url = {https://doi.org/10.5281/zenodo.3384301}
}
## Sponsor
* [Paypal](https://www.paypal.com/paypalme/miguel5612)
<!-- MARKDOWN LINKS & IMAGES -->
<!-- https://www.markdownguide.org/basic-syntax/#reference-style-links -->
[contributors-shield]: https://img.shields.io/github/contributors/miguel5612/MQSensorsLib.svg?style=flat-square
[contributors-url]: https://github.com/miguel5612/MQSensorsLib/graphs/contributors
[forks-shield]: https://img.shields.io/github/forks/miguel5612/MQSensorsLib.svg?style=flat-square
[forks-url]: https://github.com/miguel5612/MQSensorsLib/network/members
[stars-shield]: https://img.shields.io/github/stars/miguel5612/MQSensorsLib.svg?style=flat-square
[stars-url]: https://github.com/miguel5612/MQSensorsLib/stargazers
[issues-shield]: https://img.shields.io/github/issues/miguel5612/MQSensorsLib.svg?style=flat-square
[issues-url]: https://github.com/miguel5612/MQSensorsLib/issues
[license-shield]: https://img.shields.io/github/license/miguel5612/MQSensorsLib.svg?style=flat-square
[license-url]: https://github.com/miguel5612/MQSensorsLib/blob/master/LICENSE.txt
[linkedin-shield]: https://img.shields.io/badge/-LinkedIn-black.svg?style=flat-square&logo=linkedin&colorB=555
[build-url]: https://travis-ci.org/dwyl/esta.svg?branch=master
[linkedin-url]: https://www.linkedin.com/in/miguel5612
[product-screenshot]: images/screenshot.png

View File

@ -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.
@ -23,18 +23,18 @@
//Include the library
#include <MQUnifiedsensor.h>
/************************Hardware Related Macros************************************/
#define Board ("Arduino UNO")
#define Pin (A3) //Analog input 3 of your arduino
/***********************Software Related Macros************************************/
#define Type ("MQ-3") //MQ3
#define Voltage_Resolution (5)
#define ADC_Bit_Resolution (10) // For arduino UNO/MEGA/NANO
//Definitions
#define placa "Arduino UNO"
#define Voltage_Resolution 5
#define pin A3 //Analog input 3 of your arduino
#define type "MQ-3" //MQ3
#define ADC_Bit_Resolution 10 // For arduino UNO/MEGA/NANO
//#define calibration_button 13 //Pin to calibrate your sensor
double alcoholPPM = 0;
//Declare Sensor
MQUnifiedsensor MQ3(placa, Voltage_Resolution, ADC_Bit_Resolution, pin, type);
/*****************************Globals***********************************************/
double alcoholPPM = (0);
/**************************Object_Sensor********************************************/
MQUnifiedsensor MQ3(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin, Type);
void setup() {
//Init the serial port communication - to debug the library
@ -42,10 +42,10 @@ 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
Gas | a | b
LPG | 44771 | -3.245
CH4 | 2*10^31| 19.01
CO | 521853 | -3.821
@ -54,23 +54,40 @@ void setup() {
Hexane | 7585.3 | -2.849
*/
// Calibration setup
MQ3.setR0(3.86018237);
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ3.setRL(10);
*/
/***************************** MQ Init ********************************************/
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ3.init();
MQ3.init();
/***************************** 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 recomend 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 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, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR0 == 0){Serial.println("Warning: Conection 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
alcoholPPM = MQ3.readSensor(); // 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

View File

@ -13,20 +13,25 @@
with which the library was made.
*/
//Definitions
#define placa "Arduino Mega 2560"
#define Voltage_Resolution 5
#define type "Algorithm Tester"
#define ADC_Bit_Resolution 10 // For arduino UNO/MEGA/NANO
// On this program pin value doesn't matter
#define pin2 A2 //Analog input 2 of your arduino
//Declare Sensor
MQUnifiedsensor mySensor(placa, Voltage_Resolution, ADC_Bit_Resolution, pin2, type);
//Include the library
#include <MQUnifiedsensor.h>
/************************Hardware Related Macros************************************/
#define Board ("Arduino UNO")
#define Pin (A3) //Analog input 3 of your arduino
/***********************Software Related Macros************************************/
#define Type ("MQ-3") //MQ3
#define Voltage_Resolution (5)
#define ADC_Bit_Resolution (10) // For arduino UNO/MEGA/NANO
double ratio[4] = {};
double expectedValue[4] = {};
double calculatedValues[4] = {};
double error[4] = {};
//Declare Sensor
MQUnifiedsensor mySensor(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin, Type);
/*****************************Globals***********************************************/
double ratio[4] = {};
double expectedValue[4] = {};
double calculatedValues[4] = {};
double error[4] = {};
/**************************Object_Sensor********************************************/
void setup()
{

View File

@ -15,15 +15,16 @@
//Include the library
#include <MQUnifiedsensor.h>
/************************Hardware Related Macros************************************/
#define Board ("Arduino UNO")
#define Pin (A2) //Analog input 2 of your arduino
/***********************Software Related Macros************************************/
#define Type ("MQ-2") //MQ2
#define Voltage_Resolution (5)
#define ADC_Bit_Resolution (10) // For arduino UNO/MEGA/NANO
//Definitions
#define placa "Arduino UNO"
#define Voltage_Resolution 5
#define pin A2 //Analog input 2 of your arduino
#define type "MQ-2" //MQ2
#define ADC_Bit_Resolution 10 // For arduino UNO/MEGA/NANO
/*****************************Globals***********************************************/
double alcoholPPM = (0);
//Defaults, uncomment if you need
#define RatioMQ2CleanAir 9.83 //RS / R0 = 9.83 ppm
//#define RatioMQ3CleanAir 60 //RS / R0 = 60 ppm
@ -37,17 +38,20 @@
//#define RatioMQ135CleanAir 3.6//RS / R0 = 3.6 ppm
//#define RatioMQ303CleanAir 1 //RS / R0 = 1 ppm
//#define RatioMQ309CleanAir 11 //RS / R0 = 11 ppm
//Declare Sensor
MQUnifiedsensor MQ2(placa, Voltage_Resolution, ADC_Bit_Resolution, pin, type);
unsigned long contador = 0;
/**************************Object_Sensor********************************************/
MQUnifiedsensor MQ2(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin, Type);
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);
*/
//init the sensor
MQ2.init();
//Print in serial monitor

View File

@ -0,0 +1,136 @@
/*
* 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 recomend 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: Conection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR0 == 0){Serial.println("Warning: Conection 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
Serial.print(MQ2.readSensor()); // 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
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 484 KiB

93
examples/ESP32/esp32.ino Normal file
View File

@ -0,0 +1,93 @@
// This is an example of implementation using ESP8266
// Never connect the sensor direct to the ESP8266, sensor high level is 5V
// ADC of ESP8266 high level is 3.3
// To connect use a voltage divisor, where 5V will 3v3 on the middle point like
// this {{URL}}
/*
MQUnifiedsensor Library - reading an MQSensor using ESP8266 board
For this example wi will demonstrates the use a MQ3 sensor.
Library originally added 01 may 2019
by Miguel A Califa, Yersson Carrillo, Ghiordy Contreras, Mario Rodriguez
Added ESP8266 example
29.03.2020
Wiring:
https://github.com/miguel5612/MQSensorsLib_Docs/blob/master/static/img/MQ_ESP8266.PNG
This example code is in the public domain.
*/
//Include the library
#include <MQUnifiedsensor.h>
/************************Hardware Related Macros************************************/
#define Board ("ESP-32") // Wemos ESP-32 or other board, whatever have ESP32 core.
#define Pin (25) //IO25 for your ESP32 WeMos Board, pinout here: https://i.pinimg.com/originals/66/9a/61/669a618d9435c702f4b67e12c40a11b8.jpg
/***********************Software Related Macros************************************/
#define Type ("MQ-3") //MQ3 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 RatioMQ3CleanAir (60) // Ratio of your sensor, for this example an MQ-3
/*****************************Globals***********************************************/
MQUnifiedsensor MQ3(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin, Type);
/*****************************Globals***********************************************/
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
MQ3.setRegressionMethod(1); //_PPM = a*ratio^b
MQ3.setA(4.8387); MQ3.setB(-2.68); // Configure the equation to to calculate Benzene concentration
/*
Exponential regression:
Gas | a | b
LPG | 44771 | -3.245
CH4 | 2*10^31| 19.01
CO | 521853 | -3.821
Alcohol| 0.3934 | -1.504
Benzene| 4.8387 | -2.68
Hexane | 7585.3 | -2.849
*/
/***************************** MQ Init ********************************************/
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ3.init();
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ3.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 recomend 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 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, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR0 == 0){Serial.println("Warning: Conection 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 read the voltage from the analog pin
MQ3.readSensor(); // 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
}

View File

@ -22,16 +22,17 @@
//Include the library
#include <MQUnifiedsensor.h>
//Definitions
#define placa "ESP8266" //NodeMcu, WeMos D1, TTGo, ESP32.. etc
#define Voltage_Resolution 3.3
#define pin A0 //Analog input 0 of your ESP Board
#define type "MQ-3" //MQ3
#define ADC_Bit_Resolution 10 // For ESP8266
//Declare Sensor
MQUnifiedsensor MQ3(placa, Voltage_Resolution, ADC_Bit_Resolution, pin, type);
/************************Hardware Related Macros************************************/
#define Board ("ESP8266")
#define Pin (A0) //Analog input 3 of your arduino
/***********************Software Related Macros************************************/
#define Type ("MQ-3") //MQ3
#define Voltage_Resolution (3.3) // 3V3 <- IMPORTANT
#define ADC_Bit_Resolution (10) // For ESP8266
#define RatioMQ3CleanAir (60)
/*****************************Globals***********************************************/
MQUnifiedsensor MQ3(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin, Type);
/*****************************Globals***********************************************/
void setup() {
//Init the serial port communication - to debug the library
@ -39,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
@ -51,29 +52,42 @@ void setup() {
Hexane | 7585.3 | -2.849
*/
// Calibration setup
MQ3.setR0(3.86018237);
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ3.setRL(10);
*/
/***************************** MQ Init ********************************************/
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ3.init();
/***************************** MQ Init ********************************************/
//Input: setup flag, if this function are on setup will print the headers (Optional - Default value: False)
//Output: print on serial port the information about sensor and sensor readings
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ3.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 recomend 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 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, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR0 == 0){Serial.println("Warning: Conection 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
MQ3.readSensor(); // 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
}

View File

@ -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 recomend 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: Conection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR0 == 0){Serial.println("Warning: Conection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
/***************************** MQ CAlibration ********************************************/
/*
@ -75,23 +75,23 @@ 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
//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(); // 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(); // 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!");

View File

@ -15,7 +15,9 @@
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.
This example code is in the public domain.
@ -42,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:
@ -52,49 +54,43 @@ void setup() {
O3 | 23.943 | -1.11
*/
/***************************** MQ Init ********************************************/
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ131.init();
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ131.setRL(10);
*/
/***************************** 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 recomend 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: Conection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR0 == 0){Serial.println("Warning: Conection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
/***************************** MQ CAlibration ********************************************/
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ131.setRL(10);
*/
/***************************** MQ Init ********************************************/
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ131.init();
/***************************** MQ Init ********************************************/
//Input: setup flag, if this function are on setup will print the headers (Optional - Default value: False)
//Output: print on serial port the information about sensor and sensor readings
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
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.readSensor(); // 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
}

View File

@ -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.
@ -43,72 +43,72 @@ void setup() {
//Set math model to calculate the PPM concentration and the value of constants
MQ135.setRegressionMethod(1); //_PPM = a*ratio^b
/***************************** 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 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 recomend 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: Conection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR0 == 0){Serial.println("Warning: Conection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
/***************************** MQ CAlibration ********************************************/
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ135.setRL(10);
*/
MQ135.init();
/***************************** MQ Init ********************************************/
//Input: setup flag, if this function are on setup will print the headers (Optional - Default value: False)
//Output: print on serial port the information about sensor and sensor readings
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
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
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(); // 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(); // 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(); // 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(); // 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(); // 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(); // 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:
@ -116,10 +116,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
}
}

View File

@ -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 recomend 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: Conection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR0 == 0){Serial.println("Warning: Conection 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);
}

View File

@ -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,54 +50,46 @@ 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 ********************************************/
//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 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 recomend 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: Conection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR0 == 0){Serial.println("Warning: Conection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
/***************************** MQ CAlibration ********************************************/
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ135.setRL(10);
*/
/***************************** MQ Init ********************************************/
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ135.init();
/***************************** MQ Init ********************************************/
//Input: setup flag, if this function are on setup will print the headers (Optional - Default value: False)
//Output: print on serial port the information about sensor and sensor readings
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
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
MQ135.readSensor(); // 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
}

View File

@ -0,0 +1,88 @@
/*
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 recomend 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: Conection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR0 == 0){Serial.println("Warning: Conection 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
MQ136.readSensor(); // 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
}

View File

@ -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.
@ -22,17 +22,18 @@
//Include the library
#include <MQUnifiedsensor.h>
/************************Hardware Related Macros************************************/
#define Board ("Arduino UNO")
#define Pin (A2) //Analog input 3 of your arduino
/***********************Software Related Macros************************************/
#define Type ("MQ-2") //MQ2
#define Voltage_Resolution (5)
#define ADC_Bit_Resolution (10) // For arduino UNO/MEGA/NANO
#define RatioMQ2CleanAir (9.83) //RS / R0 = 9.83 ppm
//Definitions
#define placa "Arduino UNO"
#define Voltage_Resolution 5
#define pin A0 //Analog input 0 of your arduino
#define type "MQ-2" //MQ2
#define ADC_Bit_Resolution 10 // For arduino UNO/MEGA/NANO
#define RatioMQ2CleanAir 9.83 //RS / R0 = 9.83 ppm
//Declare Sensor
MQUnifiedsensor MQ2(placa, Voltage_Resolution, ADC_Bit_Resolution, pin, type);
/*****************************Globals***********************************************/
MQUnifiedsensor MQ2(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin, Type);
/*****************************Globals***********************************************/
void setup() {
//Init the serial port communication - to debug the library
@ -40,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
@ -51,47 +52,42 @@ void setup() {
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 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 recomend 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: Conection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR0 == 0){Serial.println("Warning: Conection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
/***************************** MQ CAlibration ********************************************/
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ2.setRL(10);
*/
/***************************** MQ Init ********************************************/
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ2.init();
/***************************** MQ Init ********************************************/
//Input: setup flag, if this function are on setup will print the headers (Optional - Default value: False)
//Output: print on serial port the information about sensor and sensor readings
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
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
MQ2.readSensor(); // 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
}

View File

@ -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.
@ -23,18 +23,17 @@
//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-3" //MQ3
#define ADC_Bit_Resolution 10 // For arduino UNO/MEGA/NANO
#define RatioMQ3CleanAir 60 //RS / R0 = 60 ppm
//#define calibration_button 13 //Pin to calibrate your sensor
/************************Hardware Related Macros************************************/
#define Board ("Arduino UNO")
#define Pin (A3) //Analog input 3 of your arduino
/***********************Software Related Macros************************************/
#define Type ("MQ-3") //MQ3
#define Voltage_Resolution (5)
#define ADC_Bit_Resolution (10) // For arduino UNO/MEGA/NANO
#define RatioMQ3CleanAir (60) //RS / R0 = 60 ppm
/*****************************Globals***********************************************/
//Declare Sensor
MQUnifiedsensor MQ3(placa, Voltage_Resolution, ADC_Bit_Resolution, pin, type);
MQUnifiedsensor MQ3(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin, Type);
void setup() {
//Init the serial port communication - to debug the library
@ -42,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
@ -54,49 +53,42 @@ void setup() {
Hexane | 7585.3 | -2.849
*/
/***************************** MQ Init ********************************************/
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ3.init();
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ3.setRL(10);
*/
/***************************** 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 recomend 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: Conection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR0 == 0){Serial.println("Warning: Conection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
/***************************** MQ CAlibration ********************************************/
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ3.setRL(10);
*/
/***************************** MQ Init ********************************************/
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ3.init();
/***************************** MQ Init ********************************************/
//Input: setup flag, if this function are on setup will print the headers (Optional - Default value: False)
//Output: print on serial port the information about sensor and sensor readings
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
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
MQ3.readSensor(); // 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
}

View File

@ -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:
@ -52,49 +52,41 @@ void setup() {
Ethanol | 3.4916 | -2.432
*/
/***************************** MQ Init ********************************************/
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ303.init();
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ303.setRL(10);
*/
/***************************** 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 recomend 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: Conection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR0 == 0){Serial.println("Warning: Conection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
/***************************** MQ CAlibration ********************************************/
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ303.setRL(10);
*/
/***************************** MQ Init ********************************************/
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ303.init();
/***************************** MQ Init ********************************************/
//Input: setup flag, if this function are on setup will print the headers (Optional - Default value: False)
//Output: print on serial port the information about sensor and sensor readings
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
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
MQ303.readSensor(true); // 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
}

View File

@ -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.
@ -31,18 +31,20 @@
#define type "MQ-309" //MQ309
#define ADC_Bit_Resolution 10 // For arduino UNO/MEGA/NANO
#define RatioMQ309CleanAir 11 //RS / R0 = 11 ppm
//#define calibration_button 13 //Pin to calibrate your sensor
#define PWMPin 5 // Pin connected to mosfet
//Declare Sensor
MQUnifiedsensor MQ309(placa, Voltage_Resolution, ADC_Bit_Resolution, pin, type);
unsigned long oldTime = 0;
void setup() {
//Init the serial port communication - to debug the library
Serial.begin(9600); //Init serial port
pinMode(PWMPin, OUTPUT);
//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:
@ -53,49 +55,61 @@ void setup() {
ALCOHOL | 473622 | -3.647
*/
/***************************** MQ Init ********************************************/
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ309.init();
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ309.setRL(10);
*/
/***************************** 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 recomend 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: Conection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR0 == 0){Serial.println("Warning: Conection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
/***************************** MQ CAlibration ********************************************/
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ309.setRL(10);
*/
/***************************** MQ Init ********************************************/
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ309.init();
/***************************** MQ Init ********************************************/
//Input: setup flag, if this function are on setup will print the headers (Optional - Default value: False)
//Output: print on serial port the information about sensor and sensor readings
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ309.serialDebug(true);
}
void loop() {
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.serialDebug(); // Will print the table on the serial port
delay(500); //Sampling frequency
// 60s cycle
oldTime = millis();
while(millis() - oldTime <= (30*1000))
{
// 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 read the voltage from the analog pin
MQ309.readSensor(); // 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
}
// 90s cycle
oldTime = millis();
while(millis() - oldTime <= (120*1000))
{
// 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 read the voltage from the analog pin
MQ309.readSensor(); // 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
}
// Total: 30 + 120s = 2.5 minutes.
}

View File

@ -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.
@ -23,18 +23,17 @@
//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-4" //MQ4
#define ADC_Bit_Resolution 10 // For arduino UNO/MEGA/NANO
#define RatioMQ4CleanAir 4.4 //RS / R0 = 4.4 ppm
//#define calibration_button 13 //Pin to calibrate your sensor
/************************Hardware Related Macros************************************/
#define Board ("Arduino UNO")
#define Pin (A4) //Analog input 4 of your arduino
/***********************Software Related Macros************************************/
#define Type ("MQ-4") //MQ4
#define Voltage_Resolution (5)
#define ADC_Bit_Resolution (10) // For arduino UNO/MEGA/NANO
#define RatioMQ4CleanAir (4.4) //RS / R0 = 60 ppm
/*****************************Globals***********************************************/
//Declare Sensor
MQUnifiedsensor MQ4(placa, Voltage_Resolution, ADC_Bit_Resolution, pin, type);
MQUnifiedsensor MQ4(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin, Type);
void setup() {
//Init the serial port communication - to debug the library
@ -44,47 +43,42 @@ void setup() {
MQ4.setRegressionMethod(1); //_PPM = a*ratio^b
/***************************** MQ Init ********************************************/
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ4.init();
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ4.setRL(10);
*/
/***************************** 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 recomend 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: Conection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR0 == 0){Serial.println("Warning: Conection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
/***************************** MQ CAlibration ********************************************/
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ4.setRL(10);
*/
/***************************** MQ Init ********************************************/
//Input: setup flag, if this function are on setup will print the headers (Optional - Default value: False)
//Output: print on serial port the information about sensor and sensor readings
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ4.init();
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
/*
Exponential regression:
@ -95,20 +89,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(); // 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(); // 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(); // 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(); // 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(); // 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);

View File

@ -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.
@ -23,18 +23,17 @@
//Include the library
#include <MQUnifiedsensor.h>
//Definitions
#define placa "Arduino UNO"
#define Voltage_Resolution 5
#define pin A4 //Analog input 4 of your arduino
#define type "MQ-4" //MQ4
#define ADC_Bit_Resolution 10 // For arduino UNO/MEGA/NANO
#define RatioMQ4CleanAir 4.4 //RS / R0 = 4.4 ppm
//#define calibration_button 13 //Pin to calibrate your sensor
/************************Hardware Related Macros************************************/
#define Board ("Arduino UNO")
#define Pin (A4) //Analog input 4 of your arduino
/***********************Software Related Macros************************************/
#define Type ("MQ-4") //MQ4
#define Voltage_Resolution (5)
#define ADC_Bit_Resolution (10) // For arduino UNO/MEGA/NANO
#define RatioMQ4CleanAir (4.4) //RS / R0 = 60 ppm
/*****************************Globals***********************************************/
//Declare Sensor
MQUnifiedsensor MQ4(placa, Voltage_Resolution, ADC_Bit_Resolution, pin, type);
MQUnifiedsensor MQ4(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin, Type);
void setup() {
//Init the serial port communication - to debug the library
@ -43,52 +42,46 @@ void setup() {
//Set math model to calculate the PPM concentration and the value of constants
MQ4.setRegressionMethod(0); //_PPM = pow(10, (log10(ratio)-b)/a)
/***************************** MQ Init ********************************************/
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ4.init();
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ4.setRL(10);
*/
/***************************** 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 recomend 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: Conection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR0 == 0){Serial.println("Warning: Conection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
/***************************** MQ CAlibration ********************************************/
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ4.setRL(10);
*/
/***************************** MQ Init ********************************************/
//Input: setup flag, if this function are on setup will print the headers (Optional - Default value: False)
//Output: print on serial port the information about sensor and sensor readings
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ4.init();
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 LPG = MQ4.readSensor(); // 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!");

View File

@ -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.
@ -23,18 +23,17 @@
//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-4" //MQ4
#define ADC_Bit_Resolution 10 // For arduino UNO/MEGA/NANO
#define RatioMQ4CleanAir 4.4 //RS / R0 = 4.4 ppm
//#define calibration_button 13 //Pin to calibrate your sensor
/************************Hardware Related Macros************************************/
#define Board ("Arduino UNO")
#define Pin (A4) //Analog input 4 of your arduino
/***********************Software Related Macros************************************/
#define Type ("MQ-4") //MQ4
#define Voltage_Resolution (5)
#define ADC_Bit_Resolution (10) // For arduino UNO/MEGA/NANO
#define RatioMQ4CleanAir (4.4) //RS / R0 = 60 ppm
/*****************************Globals***********************************************/
//Declare Sensor
MQUnifiedsensor MQ4(placa, Voltage_Resolution, ADC_Bit_Resolution, pin, type);
MQUnifiedsensor MQ4(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin, Type);
void setup() {
//Init the serial port communication - to debug the library
@ -42,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
@ -54,49 +53,41 @@ void setup() {
*/
/***************************** MQ Init ********************************************/
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ4.init();
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ4.setRL(10);
*/
/***************************** 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 recomend 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: Conection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR0 == 0){Serial.println("Warning: Conection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
/***************************** MQ CAlibration ********************************************/
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ4.setRL(10);
*/
/***************************** MQ Init ********************************************/
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ4.init();
/***************************** MQ Init ********************************************/
//Input: setup flag, if this function are on setup will print the headers (Optional - Default value: False)
//Output: print on serial port the information about sensor and sensor readings
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
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
MQ4.readSensor(); // 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
}

View File

@ -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
@ -53,49 +53,41 @@ void setup() {
Alcohol| 97124 | -4.918
*/
/***************************** MQ Init ********************************************/
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ5.init();
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ5.setRL(10);
*/
/***************************** 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 recomend 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: Conection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR0 == 0){Serial.println("Warning: Conection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
/***************************** MQ CAlibration ********************************************/
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ5.setRL(10);
*/
/***************************** MQ Init ********************************************/
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ5.init();
/***************************** MQ Init ********************************************/
//Input: setup flag, if this function are on setup will print the headers (Optional - Default value: False)
//Output: print on serial port the information about sensor and sensor readings
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
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
MQ5.readSensor(); // 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
}

View File

@ -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
@ -53,48 +53,41 @@ void setup() {
Alcohol | 50000000 | -6.017
*/
/***************************** MQ Init ********************************************/
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ6.init();
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ6.setRL(10);
*/
/***************************** 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 recomend 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: Conection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR0 == 0){Serial.println("Warning: Conection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
/***************************** MQ CAlibration ********************************************/
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ6.setRL(10);
*/
/***************************** MQ Init ********************************************/
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ6.init();
/***************************** MQ Init ********************************************/
//Input: setup flag, if this function are on setup will print the headers (Optional - Default value: False)
//Output: print on serial port the information about sensor and sensor readings
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
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
MQ6.readSensor(); // 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
}

View File

@ -13,9 +13,13 @@
modified 26 March 2020
by Miguel Califa
Updated example of MQ-7 AND MQ-309A
modified 20 April 2020
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.
@ -31,18 +35,20 @@
#define type "MQ-7" //MQ7
#define ADC_Bit_Resolution 10 // For arduino UNO/MEGA/NANO
#define RatioMQ7CleanAir 27.5 //RS / R0 = 27.5 ppm
//#define calibration_button 13 //Pin to calibrate your sensor
#define PWMPin 5 // Pin connected to mosfet
//Declare Sensor
MQUnifiedsensor MQ7(placa, Voltage_Resolution, ADC_Bit_Resolution, pin, type);
unsigned long oldTime = 0;
void setup() {
//Init the serial port communication - to debug the library
Serial.begin(9600); //Init serial port
pinMode(PWMPin, OUTPUT);
//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:
@ -54,49 +60,61 @@ void setup() {
Alcohol | 40000000000000000 | -12.35
*/
/***************************** MQ Init ********************************************/
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ7.init();
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ7.setRL(10);
*/
/***************************** 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 recomend 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: Conection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR0 == 0){Serial.println("Warning: Conection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
/***************************** MQ CAlibration ********************************************/
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ7.setRL(10);
*/
/***************************** MQ Init ********************************************/
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ7.init();
/***************************** MQ Init ********************************************/
//Input: setup flag, if this function are on setup will print the headers (Optional - Default value: False)
//Output: print on serial port the information about sensor and sensor readings
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ7.serialDebug(true);
}
void loop() {
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.serialDebug(); // Will print the table on the serial port
delay(500); //Sampling frequency
// 60s cycle
oldTime = millis();
while(millis() - oldTime <= (60*1000))
{
// VH 5 Volts
analogWrite(5, 255); // 255 is DC 5V output
MQ7.update(); // Update data, the arduino will read the voltage from the analog pin
MQ7.readSensor(); // 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
}
// 90s cycle
oldTime = millis();
while(millis() - oldTime <= (90*1000))
{
// 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 read the voltage from the analog pin
MQ7.readSensor(); // 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
}
// Total: 60 + 90s = 2.5 minutes.
}

View File

@ -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:
@ -54,48 +54,41 @@ void setup() {
Alcohol | 76101 | -1.86
*/
/***************************** MQ Init ********************************************/
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ8.init();
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ8.setRL(10);
*/
/***************************** 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 recomend 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: Conection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR0 == 0){Serial.println("Warning: Conection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
/***************************** MQ CAlibration ********************************************/
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ8.setRL(10);
*/
/***************************** MQ Init ********************************************/
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ8.init();
/***************************** MQ Init ********************************************/
//Input: setup flag, if this function are on setup will print the headers (Optional - Default value: False)
//Output: print on serial port the information about sensor and sensor readings
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
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
MQ8.readSensor(); // 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
}

View File

@ -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.
@ -23,18 +23,17 @@
//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-9" //MQ9
#define ADC_Bit_Resolution 10 // For arduino UNO/MEGA/NANO
#define RatioMQ9CleanAir 9.6 //RS / R0 = 9.6 ppm
//#define calibration_button 13 //Pin to calibrate your sensor
/************************Hardware Related Macros************************************/
#define Board ("Arduino UNO")
#define Pin (A9) //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
/*****************************Globals***********************************************/
//Declare Sensor
MQUnifiedsensor MQ9(placa, Voltage_Resolution, ADC_Bit_Resolution, pin, type);
MQUnifiedsensor MQ9(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin, Type);
void setup() {
//Init the serial port communication - to debug the library
@ -44,46 +43,41 @@ void setup() {
MQ9.setRegressionMethod(1); //_PPM = a*ratio^b
/***************************** MQ Init ********************************************/
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ9.init();
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ9.setRL(10);
*/
/***************************** 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 recomend 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: Conection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR0 == 0){Serial.println("Warning: Conection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
/***************************** MQ CAlibration ********************************************/
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ9.setRL(10);
*/
/***************************** MQ Init ********************************************/
//Input: setup flag, if this function are on setup will print the headers (Optional - Default value: False)
//Output: print on serial port the information about sensor and sensor readings
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ9.init();
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
/*
Exponential regression:
GAS | a | b
@ -92,14 +86,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(); // 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(); // 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(); // 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);

View File

@ -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.
@ -23,26 +23,29 @@
//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-9" //MQ9
#define ADC_Bit_Resolution 10 // For arduino UNO/MEGA/NANO
#define RatioMQ9CleanAir 9.6 //RS / R0 = 9.6 ppm
//#define calibration_button 13 //Pin to calibrate your sensor
/************************Hardware Related Macros************************************/
#define Board ("Arduino UNO")
#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(placa, Voltage_Resolution, ADC_Bit_Resolution, pin, type);
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:
@ -51,50 +54,68 @@ void setup() {
CH4 | 4269.6 | -2.648
CO | 599.65 | -2.244
*/
/***************************** MQ Init ********************************************/
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ9.init();
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ9.setRL(10);
*/
/***************************** 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 recomend 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: Conection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR0 == 0){Serial.println("Warning: Conection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
/***************************** MQ CAlibration ********************************************/
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ9.setRL(10);
*/
/***************************** MQ Init ********************************************/
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ9.init();
/***************************** MQ Init ********************************************/
//Input: setup flag, if this function are on setup will print the headers (Optional - Default value: False)
//Output: print on serial port the information about sensor and sensor readings
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
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
MQ9.readSensor(); // 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
}

View File

@ -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.
@ -19,39 +19,41 @@
//Include the library
#include <MQUnifiedsensor.h>
//Definitions
#define placa "Arduino Mega 2560"
#define Voltage_Resolution 5
#define type "MQ-Board"
#define ADC_Bit_Resolution 10 // For arduino UNO/MEGA/NANO
#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 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
/************************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 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 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(placa, Voltage_Resolution, ADC_Bit_Resolution, pin2, type);
MQUnifiedsensor MQ3(placa, Voltage_Resolution, ADC_Bit_Resolution, pin3, type);
MQUnifiedsensor MQ4(placa, Voltage_Resolution, ADC_Bit_Resolution, pin4, type);
MQUnifiedsensor MQ5(placa, Voltage_Resolution, ADC_Bit_Resolution, pin5, type);
MQUnifiedsensor MQ6(placa, Voltage_Resolution, ADC_Bit_Resolution, pin6, type);
MQUnifiedsensor MQ7(placa, Voltage_Resolution, ADC_Bit_Resolution, pin7, type);
MQUnifiedsensor MQ8(placa, Voltage_Resolution, ADC_Bit_Resolution, pin8, type);
MQUnifiedsensor MQ9(placa, Voltage_Resolution, ADC_Bit_Resolution, pin9, type);
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);
unsigned long oldTime = 0;
void setup() {
//Init serial port
@ -59,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 recomend 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,
@ -115,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();
@ -162,14 +164,35 @@ void setup() {
//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);
}
void loop() {
//Update the voltage lectures
oldTime = millis();
while(millis() - oldTime <= (60*1000))
{
// VH 5 Volts
analogWrite(5, 255); // 255 is DC 5V output
readAllSensors();
delay(500);
}
// 90s cycle
oldTime = millis();
while(millis() - oldTime <= (90*1000))
{
// VH 1.4 Volts
analogWrite(5, 20); // 255 is 100%, 20.4 is aprox 8% of Duty cycle for 90s
readAllSensors();
delay(500);
}
}
void readAllSensors()
{
//Update the voltage Values
MQ2.update();
MQ3.update();
MQ4.update();
@ -178,28 +201,6 @@ void loop() {
MQ7.update();
MQ8.update();
MQ9.update();
/*
//Rutina de calibracion - Uncomment if you need (setup too and header)
if(calibration_button)
{
float R0 = MQ2.calibrate();
MQ2.setR0(R0):
R0 = MQ3.calibrate();
MQ3.setR0(R0):
R0 = MQ4.calibrate();
MQ4.setR0(R0):
R0 = MQ5.calibrate();
MQ5.setR0(R0):
R0 = MQ6.calibrate();
MQ6.setR0(R0):
R0 = MQ7.calibrate();
MQ7.setR0(R0):
R0 = MQ8.calibrate();
MQ8.setR0(R0):
R0 = MQ9.calibrate();
MQ9.setR0(R0):
}
*/
//Read the sensor and print in serial port
float MQ2Lecture = MQ2.readSensor();
float MQ3Lecture = MQ3.readSensor();

View File

@ -0,0 +1,124 @@
/*
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: Conection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR0 == 0){Serial.println("Warning: Conection 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
MQ135.setA(605.18); MQ135.setB(-3.937); // Configure the equation to calculate CO concentration value
float CO = MQ135.readSensor(); // 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(); // 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(); // 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(); // 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(); // 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(); // 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
}

View File

@ -0,0 +1,272 @@
/*
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 9 of your arduino
#define Pin11 (A11) //Analog input 9 of your arduino
#define Pin12 (A12) //Analog input 9 of your arduino
#define Pin13 (A13) //Analog input 9 of your arduino
#define Pin14 (A14) //Analog input 9 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(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();
}

View File

@ -15,9 +15,11 @@
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.
Important:
1. Although it doesn't matter what pin you put in when initializing your MQ sensor function it is important that you don't
for any reason invoke the MQ.init() method because that method configures the selected pin as input and you may need it for
@ -27,10 +29,6 @@
3. You must ensure that when invoking the setADC(value) method the value you are passing is within the expected parameters,
for example if it is a 10-bit ADC converter, we expect a value between 0 and 2^10 = 1024
This example code is in the public domain.
*/
//Include the library
@ -54,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
@ -67,41 +65,41 @@ void setup() {
*/
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ3.setRL(10);
*/
/***************************** 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 recomend 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: Conection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR0 == 0){Serial.println("Warning: Conection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
/***************************** MQ CAlibration ********************************************/
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ3.setRL(10);
*/
MQ3.serialDebug(true);
}
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
MQ3.readSensor(); // 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
}

View File

@ -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
@ -51,34 +51,31 @@ void setup() {
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ4.init();
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ4.setRL(10);
*/
/***************************** 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 recomend 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: Conection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR0 == 0){Serial.println("Warning: Conection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
/***************************** MQ CAlibration ********************************************/
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ4.setRL(10);
*/
MQ4.serialDebug(true);
}
@ -91,8 +88,8 @@ 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 smokePPM = MQ4.readSensor(); // 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);
}

View File

@ -1,10 +1,10 @@
name=MQUnifiedsensor
version=1.9.9.2
author= Miguel Califa <miguelangel5612@gmail.com>, Yersson Carrillo, Ghiordy Contreras
version=3.0.0
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

View File

@ -1,14 +1,21 @@
#include "MQUnifiedsensor.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-> _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);
@ -27,13 +34,16 @@ void MQUnifiedsensor::setRL(float RL) {
}
void MQUnifiedsensor::setADC(int value)
{
this-> _sensor_volt = (value) * _VOLT_RESOLUTION / (pow(2, ADC_RESOLUTION) - 1);
this-> _sensor_volt = (value) * _VOLT_RESOLUTION / ((pow(2, _ADC_Bit_Resolution)) - 1);
this-> _adc = value;
}
void MQUnifiedsensor::setVoltResolution(float voltage_resolution)
{
_VOLT_RESOLUTION = voltage_resolution;
}
void MQUnifiedsensor::setPin(int pin) {
this->_pin = pin;
}
void MQUnifiedsensor::setRegressionMethod(int regressionMethod)
{
//this->_regressionMethod = regressionMethod;
@ -80,7 +90,7 @@ void MQUnifiedsensor::serialDebug(bool onSetup)
Serial.print("R0: "); Serial.print(_R0); Serial.println("");
Serial.print("RL: "); Serial.print(_RL); Serial.println("");
Serial.print("Model: "); if(_regressionMethod == "Exponential") Serial.println("Exponential"); else Serial.println("Linear");
Serial.print("Model: "); if(_regressionMethod == 1) Serial.println("Exponential"); else Serial.println("Linear");
Serial.print(_type); Serial.print(" -> a: "); Serial.print(_a); Serial.print(" | b: "); Serial.println(_b);
Serial.print("Development board: "); Serial.println(_placa);
@ -95,7 +105,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)); Serial.print(" | "); Serial.print(_sensor_volt);
Serial.print("|"); Serial.print(_adc); Serial.print("| v = ADC*"); Serial.print(_VOLT_RESOLUTION); Serial.print("/"); Serial.print((pow(2, _ADC_Bit_Resolution)) - 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(_ratio); Serial.print( " | ");
if(_regressionMethod == 1) Serial.print("ratio*a + b");
@ -108,6 +118,10 @@ 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);
@ -125,12 +139,34 @@ float MQUnifiedsensor::validateEcuation(float ratioInput)
//Serial.println("Result: "); Serial.println(_PPM);
return _PPM;
}
float MQUnifiedsensor::readSensor()
float MQUnifiedsensor::readSensor(bool isMQ303A, float correctionFactor, bool injected)
{
//More explained in: https://jayconsystems.com/blog/understanding-a-gas-sensor
if(isMQ303A) {
_VOLT_RESOLUTION = _VOLT_RESOLUTION - 0.45; //Calculations for RS using mq303a sensor look wrong #42
}
_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.
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 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
}
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.
return _PPM;
}
float MQUnifiedsensor::readSensorR0Rs()
{
//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
_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
@ -164,7 +200,7 @@ float MQUnifiedsensor::calibrate(float ratioInCleanAir) {
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)
{
@ -174,15 +210,33 @@ float MQUnifiedsensor::getVoltage(int read) {
avg += _adc;
delay(retry_interval);
}
voltage = (avg/ retries) * _VOLT_RESOLUTION / (pow(2, ADC_RESOLUTION) - 1);
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 = ((_VOLT_RESOLUTION*_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() );
}
}

View File

@ -6,30 +6,30 @@
/***********************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 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 readSensor(bool isMQ303A = false, float correctionFactor = 0.0, bool injected=false);
float readSensorR0Rs();
float validateEcuation(float ratioInput = 0);
//get function for info
@ -39,23 +39,26 @@ class MQUnifiedsensor
float getRL();
float getVoltResolution();
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 _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];
};