custom mq135 api

This commit is contained in:
GrafZeppelin 2021-04-03 16:06:39 +02:00
parent 5f9ccca0cf
commit b0ac32d423
4 changed files with 135 additions and 72 deletions

59
lib/MQ135New/MQ135New.cpp Normal file
View file

@ -0,0 +1,59 @@
#include "MQ135New.h"
MQ135::MQ135(uint8_t pin) {
_pin = pin;
}
double MQ135::getRS() {
double voltage = getVoltage();
double RS = (double)((double)(VIn * RL) / voltage) - RL;
return RS;
}
double MQ135::getVoltage() {
int value = analogRead(_pin);
double voltage = (double)(value * VIn) / (double)(Resolution);
return voltage;
}
double MQ135::getPPM(float a, float b) {
double ratio = getRS() / R0;
// double ppm_log = (log10(ratio) - b) / a;
// double ppm = pow(10, ppm_log);
double ppm = a * pow(ratio, b);
return ppm;
}
double MQ135::getAcetona() {
return getPPM(34.668, -3.369);
}
double MQ135::getAlcohol() {
return getPPM(77.255, -3.18);
}
double MQ135::getCO2() {
// return getPPM(-0.3525, 0.7142) + ATMOCO2;
return getPPM(110.47, -2.862) + ATMOCO2;
}
double MQ135::getCO() {
return getPPM(605.18, -3.937);
}
double MQ135::getNH4() {
return getPPM(102.2, -2.473);
}
double MQ135::getTolueno() {
return getPPM(44.947, -3.445);
}
float MQ135::getR0() {
double r0 = getRS() / 3.6;
return r0;
}
void MQ135::setR0(float r0) {
R0 = r0;
}

31
lib/MQ135New/MQ135New.h Normal file
View file

@ -0,0 +1,31 @@
#ifndef MQ135New_H
#define MQ135New_H
#include "Arduino.h"
#define RL 10
#define VIn 5
#define Resolution 1024
#define ATMOCO2 397.13
class MQ135 {
private:
uint8_t _pin;
float R0;
double getPPM(float a, float b);
public:
MQ135(uint8_t pin);
double getAcetona();
double getAlcohol();
double getCO();
double getCO2();
double getNH4();
double getTolueno();
float getR0();
void setR0(float r0);
double getRS();
double getVoltage();
};
#endif

View file

@ -12,5 +12,6 @@
platform = atmelavr platform = atmelavr
board = megaatmega2560 board = megaatmega2560
framework = arduino framework = arduino
lib_deps = ; lib_deps =
adafruit/Adafruit BME280 Library @ ^2.1.2 ; adafruit/Adafruit BME280 Library @ ^2.1.2
; miguel5612/MQUnifiedsensor @ ^2.0.1

View file

@ -1,7 +1,5 @@
#include <Arduino.h> #include <Arduino.h>
#include <MQ135.h> #include <MQ135New.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define PIN_MQ135 A0 #define PIN_MQ135 A0
#define PIN_LED_GREEN DD2 #define PIN_LED_GREEN DD2
@ -12,83 +10,57 @@
#define MEASURE_DELAY 1000 #define MEASURE_DELAY 1000
#define NOISE_DELAY 100 #define NOISE_DELAY 100
#define SEALEVELPRESSURE_HPA (1013.25) MQ135 co2_sensor(PIN_MQ135);
MQ135 co2_sensor = MQ135(PIN_MQ135);
Adafruit_BME280 bme(10, 11, 12, 13);
const int maxCount = MEASURE_DELAY / NOISE_DELAY;
int count = -1;
float ppm = 0;
bool noise = false;
void setup() { void setup() {
pinMode(PIN_MQ135, INPUT);
pinMode(PIN_LED_GREEN, OUTPUT);
pinMode(PIN_LED_YELLOW, OUTPUT);
pinMode(PIN_LED_RED, OUTPUT);
pinMode(PIN_NOISE, OUTPUT);
Serial.begin(9600); Serial.begin(9600);
pinMode(PIN_MQ135, INPUT);
if (!bme.begin()) { co2_sensor.setR0(66.0);
Serial.println(F("Could not find a valid BME280 sensor, check wiring!"));
}
} }
void printValues(float ppm, float temp, float humidity); void printValues(float ppm, float temp, float humidity);
void loop() { void loop() {
if (count >= maxCount || count < 0) { float value = co2_sensor.getCO2();
float temp = bme.readTemperature(); Serial.println(value);
float humidity = bme.readHumidity(); Serial.print("rs: ");
ppm = co2_sensor.getCorrectedPPM(temp, humidity); Serial.println(co2_sensor.getRS());
Serial.print("voltage: ");
Serial.println(co2_sensor.getVoltage());
delay(500);
// if (count >= maxCount || count < 0) {
// float temp = bme.readTemperature();
// float humidity = bme.readHumidity();
// ppm = co2_sensor.getCorrectedPPM(temp, humidity);
count = 0; // count = 0;
if (ppm < 1000) { // if (ppm < 1000) {
digitalWrite(PIN_LED_GREEN, 1); // digitalWrite(PIN_LED_GREEN, 1);
digitalWrite(PIN_LED_YELLOW, 0); // digitalWrite(PIN_LED_YELLOW, 0);
digitalWrite(PIN_LED_RED, 0); // digitalWrite(PIN_LED_RED, 0);
} // }
else if (ppm <= 2000) { // else if (ppm <= 2000) {
digitalWrite(PIN_LED_GREEN, 0); // digitalWrite(PIN_LED_GREEN, 0);
digitalWrite(PIN_LED_YELLOW, 1); // digitalWrite(PIN_LED_YELLOW, 1);
digitalWrite(PIN_LED_RED, 0); // digitalWrite(PIN_LED_RED, 0);
} // }
else { // else {
digitalWrite(PIN_LED_GREEN, 0); // digitalWrite(PIN_LED_GREEN, 0);
digitalWrite(PIN_LED_YELLOW, 0); // digitalWrite(PIN_LED_YELLOW, 0);
digitalWrite(PIN_LED_RED, 1); // digitalWrite(PIN_LED_RED, 1);
} // }
printValues(ppm, temp, humidity); // printValues(ppm, temp, humidity);
} // }
if (ppm > 2000) { // if (ppm > 2000) {
noise = !noise; // noise = !noise;
digitalWrite(PIN_NOISE, noise); // digitalWrite(PIN_NOISE, noise);
} // }
else { // else {
digitalWrite(PIN_NOISE, 0); // digitalWrite(PIN_NOISE, 0);
} // }
delay(NOISE_DELAY); // delay(NOISE_DELAY);
count++; // count++;
}
void printValues(float ppm, float temp, float humidity) {
Serial.print("Temperature = ");
Serial.print(temp);
Serial.println(" *C");
Serial.print("Humidity = ");
Serial.print(humidity);
Serial.println(" %");
Serial.print ("ppm = ");
Serial.println (ppm);
Serial.print ("rzero = ");
Serial.println (co2_sensor.getCorrectedRZero(temp, humidity));
Serial.println();
} }