Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update calculations, README and add Arduino example #7

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
12 changes: 10 additions & 2 deletions MQ135.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,15 @@ MQ135::MQ135(uint8_t pin) {
*/
/**************************************************************************/
float MQ135::getCorrectionFactor(float t, float h) {
return CORA * t * t - CORB * t + CORC - (h-33.)*CORD;
// Linearization of the temperature dependency curve under and above 20 degree C
// below 20degC: fact = a * t * t - b * t - (h - 33) * d
// above 20degC: fact = a * t + b * h + c
// this assumes a linear dependency on humidity
if(t < 20){
return CORA * t * t - CORB * t + CORC - (h-33.)*CORD;
} else {
return CORE * t + CORF * h + CORG;
}
}

/**************************************************************************/
Expand All @@ -52,7 +60,7 @@ float MQ135::getCorrectionFactor(float t, float h) {
/**************************************************************************/
float MQ135::getResistance() {
int val = analogRead(_pin);
return ((1023./(float)val) * 5. - 1.)*RLOAD;
return ((1023./(float)val) - 1.)*RLOAD;
}

/**************************************************************************/
Expand Down
3 changes: 3 additions & 0 deletions MQ135.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ v1.0 - First release
#define CORB 0.02718
#define CORC 1.39538
#define CORD 0.0018
#define CORE -0.003333333
#define CORF -0.001923077
#define CORG 1.130128205

/// Atmospheric CO2 level for calibration purposes
#define ATMOCO2 397.13
Expand Down
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
MQ135
MQ135 GAS SENSOR
=====

Arduino library for the MQ135
Arduino library for the MQ135 gas sensor

## Datasheet
Can be found [here](https://www.olimex.com/Products/Components/Sensors/SNS-MQ135/resources/SNS-MQ135.pdf)

## Application
They are used in air quality control equipments for buildings/offices, are suitable for detecting of NH3, NOx, alcohol, Benzene, smoke, CO2, etc

## Features
This library has:
- Corrections for temperature and humidity
- Measurements:
- getResistance
- getCorrectedResistance
- getPPM
- getCorrectedPPM
- getRZero
- getCorrectedRZero

More Info
=====

https://hackaday.io/project/3475-sniffing-trinket/log/12363-mq135-arduino-library
43 changes: 43 additions & 0 deletions examples/MQ135/MQ135.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "MQ135.h"

// MQ135 gas sensor
//
// Datasheet can be found here: https://www.olimex.com/Products/Components/Sensors/SNS-MQ135/resources/SNS-MQ135.pdf
//
// Application
// They are used in air quality control equipments for buildings/offices, are suitable for detecting of NH3, NOx, alcohol, Benzene, smoke, CO2, etc
//
// Original creator of this library: https://github.com/GeorgK/MQ135


#define PIN_MQ135 A2
MQ135 mq135_sensor = MQ135(PIN_MQ135);

float temperature = 21.0; // assume current temperature. Recommended to measure with DHT22
float humidity = 25.0; // assume current humidity. Recommended to measure with DHT22

void setup() {
Serial.begin(9600);
}

void loop() {
float rzero = mq135_sensor.getRZero();
float correctedRZero = mq135_sensor.getCorrectedRZero(temperature, humidity);
float resistance = mq135_sensor.getResistance();
float ppm = mq135_sensor.getPPM();
float correctedPPM = mq135_sensor.getCorrectedPPM(temperature, humidity);

Serial.print("MQ135 RZero: ");
Serial.print(rzero);
Serial.print("\t Corrected RZero: ");
Serial.print(correctedRZero);
Serial.print("\t Resistance: ");
Serial.print(resistance);
Serial.print("\t PPM: ");
Serial.print(ppm);
Serial.print("\t Corrected PPM: ");
Serial.print(correctedPPM);
Serial.println("ppm");

delay(300);
}