add bme280
This commit is contained in:
parent
3797d45f1e
commit
a794de5eb8
5 changed files with 159 additions and 76 deletions
|
@ -3,13 +3,10 @@
|
||||||
@file MQ135.cpp
|
@file MQ135.cpp
|
||||||
@author G.Krocker (Mad Frog Labs)
|
@author G.Krocker (Mad Frog Labs)
|
||||||
@license GNU GPLv3
|
@license GNU GPLv3
|
||||||
|
|
||||||
First version of an Arduino Library for the MQ135 gas sensor
|
First version of an Arduino Library for the MQ135 gas sensor
|
||||||
TODO: Review the correction factor calculation. This currently relies on
|
TODO: Review the correction factor calculation. This currently relies on
|
||||||
the datasheet but the information there seems to be wrong.
|
the datasheet but the information there seems to be wrong.
|
||||||
|
|
||||||
@section HISTORY
|
@section HISTORY
|
||||||
|
|
||||||
v1.0 - First release
|
v1.0 - First release
|
||||||
*/
|
*/
|
||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
|
@ -19,7 +16,6 @@ v1.0 - First release
|
||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
/*!
|
/*!
|
||||||
@brief Default constructor
|
@brief Default constructor
|
||||||
|
|
||||||
@param[in] pin The analog input pin for the readout of the sensor
|
@param[in] pin The analog input pin for the readout of the sensor
|
||||||
*/
|
*/
|
||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
|
@ -32,10 +28,8 @@ MQ135::MQ135(uint8_t pin) {
|
||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
/*!
|
/*!
|
||||||
@brief Get the correction factor to correct for temperature and humidity
|
@brief Get the correction factor to correct for temperature and humidity
|
||||||
|
|
||||||
@param[in] t The ambient air temperature
|
@param[in] t The ambient air temperature
|
||||||
@param[in] h The relative humidity
|
@param[in] h The relative humidity
|
||||||
|
|
||||||
@return The calculated correction factor
|
@return The calculated correction factor
|
||||||
*/
|
*/
|
||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
|
@ -46,7 +40,6 @@ float MQ135::getCorrectionFactor(float t, float h) {
|
||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
/*!
|
/*!
|
||||||
@brief Get the resistance of the sensor, ie. the measurement value
|
@brief Get the resistance of the sensor, ie. the measurement value
|
||||||
|
|
||||||
@return The sensor resistance in kOhm
|
@return The sensor resistance in kOhm
|
||||||
*/
|
*/
|
||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
|
@ -59,10 +52,8 @@ float MQ135::getResistance() {
|
||||||
/*!
|
/*!
|
||||||
@brief Get the resistance of the sensor, ie. the measurement value corrected
|
@brief Get the resistance of the sensor, ie. the measurement value corrected
|
||||||
for temp/hum
|
for temp/hum
|
||||||
|
|
||||||
@param[in] t The ambient air temperature
|
@param[in] t The ambient air temperature
|
||||||
@param[in] h The relative humidity
|
@param[in] h The relative humidity
|
||||||
|
|
||||||
@return The corrected sensor resistance kOhm
|
@return The corrected sensor resistance kOhm
|
||||||
*/
|
*/
|
||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
|
@ -73,7 +64,6 @@ float MQ135::getCorrectedResistance(float t, float h) {
|
||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
/*!
|
/*!
|
||||||
@brief Get the ppm of CO2 sensed (assuming only CO2 in the air)
|
@brief Get the ppm of CO2 sensed (assuming only CO2 in the air)
|
||||||
|
|
||||||
@return The ppm of CO2 in the air
|
@return The ppm of CO2 in the air
|
||||||
*/
|
*/
|
||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
|
@ -85,10 +75,8 @@ float MQ135::getPPM() {
|
||||||
/*!
|
/*!
|
||||||
@brief Get the ppm of CO2 sensed (assuming only CO2 in the air), corrected
|
@brief Get the ppm of CO2 sensed (assuming only CO2 in the air), corrected
|
||||||
for temp/hum
|
for temp/hum
|
||||||
|
|
||||||
@param[in] t The ambient air temperature
|
@param[in] t The ambient air temperature
|
||||||
@param[in] h The relative humidity
|
@param[in] h The relative humidity
|
||||||
|
|
||||||
@return The ppm of CO2 in the air
|
@return The ppm of CO2 in the air
|
||||||
*/
|
*/
|
||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
|
@ -99,7 +87,6 @@ float MQ135::getCorrectedPPM(float t, float h) {
|
||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
/*!
|
/*!
|
||||||
@brief Get the resistance RZero of the sensor for calibration purposes
|
@brief Get the resistance RZero of the sensor for calibration purposes
|
||||||
|
|
||||||
@return The sensor resistance RZero in kOhm
|
@return The sensor resistance RZero in kOhm
|
||||||
*/
|
*/
|
||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
|
@ -111,10 +98,8 @@ float MQ135::getRZero() {
|
||||||
/*!
|
/*!
|
||||||
@brief Get the corrected resistance RZero of the sensor for calibration
|
@brief Get the corrected resistance RZero of the sensor for calibration
|
||||||
purposes
|
purposes
|
||||||
|
|
||||||
@param[in] t The ambient air temperature
|
@param[in] t The ambient air temperature
|
||||||
@param[in] h The relative humidity
|
@param[in] h The relative humidity
|
||||||
|
|
||||||
@return The corrected sensor resistance RZero in kOhm
|
@return The corrected sensor resistance RZero in kOhm
|
||||||
*/
|
*/
|
||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
|
|
|
@ -25,7 +25,7 @@ v1.0 - First release
|
||||||
#define RLOAD 10.0
|
#define RLOAD 10.0
|
||||||
/// Calibration resistance at atmospheric CO2 level
|
/// Calibration resistance at atmospheric CO2 level
|
||||||
// #define RZERO 76.63
|
// #define RZERO 76.63
|
||||||
#define RZERO 480
|
#define RZERO 4000
|
||||||
/// Parameters for calculating ppm of CO2 from sensor resistance
|
/// Parameters for calculating ppm of CO2 from sensor resistance
|
||||||
#define PARA 116.6020682
|
#define PARA 116.6020682
|
||||||
#define PARB 2.769034857
|
#define PARB 2.769034857
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
; Please visit documentation for the other options and examples
|
; Please visit documentation for the other options and examples
|
||||||
; https://docs.platformio.org/page/projectconf.html
|
; https://docs.platformio.org/page/projectconf.html
|
||||||
|
|
||||||
[env:uno]
|
[env:mega]
|
||||||
platform = atmelavr
|
platform = atmelavr
|
||||||
board = uno
|
board = megaatmega2560
|
||||||
framework = arduino
|
framework = arduino
|
100
src/main.cpp
100
src/main.cpp
|
@ -1,10 +1,9 @@
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <MQ135.h>
|
#include <MQ135.h>
|
||||||
#include <dht.h>
|
#include <Adafruit_Sensor.h>
|
||||||
|
#include <Adafruit_BME280.h>
|
||||||
|
|
||||||
#define PIN_MQ135 A0
|
#define PIN_MQ135 A0
|
||||||
#define PIN_DHT11 A1
|
|
||||||
|
|
||||||
#define PIN_LED_GREEN DD2
|
#define PIN_LED_GREEN DD2
|
||||||
#define PIN_LED_YELLOW DD3
|
#define PIN_LED_YELLOW DD3
|
||||||
#define PIN_LED_RED DD4
|
#define PIN_LED_RED DD4
|
||||||
|
@ -13,74 +12,38 @@
|
||||||
#define MEASURE_DELAY 1000
|
#define MEASURE_DELAY 1000
|
||||||
#define NOISE_DELAY 100
|
#define NOISE_DELAY 100
|
||||||
|
|
||||||
|
#define SEALEVELPRESSURE_HPA (1013.25)
|
||||||
|
|
||||||
MQ135 co2_sensor = MQ135(PIN_MQ135);
|
MQ135 co2_sensor = MQ135(PIN_MQ135);
|
||||||
dht dht_sensor;
|
Adafruit_BME280 bme(10, 11, 12, 13);
|
||||||
const int maxCount = MEASURE_DELAY / NOISE_DELAY;
|
const int maxCount = MEASURE_DELAY / NOISE_DELAY;
|
||||||
int count = 0;
|
int count = -1;
|
||||||
float ppm = -1;
|
float ppm = 0;
|
||||||
bool noise = false;
|
bool noise = false;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
|
||||||
pinMode(PIN_MQ135, INPUT);
|
pinMode(PIN_MQ135, INPUT);
|
||||||
pinMode(PIN_DHT11, INPUT);
|
|
||||||
pinMode(PIN_LED_GREEN, OUTPUT);
|
pinMode(PIN_LED_GREEN, OUTPUT);
|
||||||
pinMode(PIN_LED_YELLOW, OUTPUT);
|
pinMode(PIN_LED_YELLOW, OUTPUT);
|
||||||
pinMode(PIN_LED_RED, OUTPUT);
|
pinMode(PIN_LED_RED, OUTPUT);
|
||||||
pinMode(PIN_NOISE, OUTPUT);
|
pinMode(PIN_NOISE, OUTPUT);
|
||||||
|
|
||||||
|
Serial.begin(9600);
|
||||||
|
|
||||||
|
if (!bme.begin()) {
|
||||||
|
Serial.println(F("Could not find a valid BME280 sensor, check wiring!"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
float measure() {
|
void printValues();
|
||||||
Serial.print(RZERO);
|
|
||||||
Serial.println("---------------------------");
|
|
||||||
Serial.print("DHT:\t");
|
|
||||||
int chk = dht_sensor.read11(PIN_DHT11);
|
|
||||||
switch (chk)
|
|
||||||
{
|
|
||||||
case DHTLIB_OK:
|
|
||||||
Serial.print("OK,\t");
|
|
||||||
break;
|
|
||||||
case DHTLIB_ERROR_CHECKSUM:
|
|
||||||
Serial.print("Checksum error,\t");
|
|
||||||
break;
|
|
||||||
case DHTLIB_ERROR_TIMEOUT:
|
|
||||||
Serial.print("Time out error,\t");
|
|
||||||
break;
|
|
||||||
case DHTLIB_ERROR_CONNECT:
|
|
||||||
Serial.print("Connect error,\t");
|
|
||||||
break;
|
|
||||||
case DHTLIB_ERROR_ACK_L:
|
|
||||||
Serial.print("Ack Low error,\t");
|
|
||||||
break;
|
|
||||||
case DHTLIB_ERROR_ACK_H:
|
|
||||||
Serial.print("Ack High error,\t");
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
Serial.print("Unknown error,\t");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
Serial.println();
|
|
||||||
|
|
||||||
Serial.print ("temperature: ");
|
|
||||||
Serial.println (dht_sensor.temperature);
|
|
||||||
Serial.print ("humidity: ");
|
|
||||||
Serial.println (dht_sensor.humidity);
|
|
||||||
|
|
||||||
float val = analogRead(A0);
|
|
||||||
Serial.print ("raw = ");
|
|
||||||
Serial.println (val);
|
|
||||||
float zero = co2_sensor.getCorrectedRZero(dht_sensor.temperature, dht_sensor.humidity);
|
|
||||||
Serial.print ("rzero: ");
|
|
||||||
Serial.println (zero);
|
|
||||||
float ppm = co2_sensor.getCorrectedPPM(dht_sensor.temperature, dht_sensor.humidity);
|
|
||||||
Serial.print ("ppm: ");
|
|
||||||
Serial.println (ppm);
|
|
||||||
return ppm;
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
if (count >= maxCount || ppm < 0) {
|
if (count >= maxCount || count < 0) {
|
||||||
ppm = measure();
|
ppm = co2_sensor.getPPM();
|
||||||
|
Serial.print ("ppm: ");
|
||||||
|
Serial.println (ppm);
|
||||||
|
Serial.print ("rzero: ");
|
||||||
|
Serial.println (co2_sensor.getRZero());
|
||||||
count = 0;
|
count = 0;
|
||||||
if (ppm < 1000) {
|
if (ppm < 1000) {
|
||||||
digitalWrite(PIN_LED_GREEN, 1);
|
digitalWrite(PIN_LED_GREEN, 1);
|
||||||
|
@ -97,6 +60,8 @@ void loop() {
|
||||||
digitalWrite(PIN_LED_YELLOW, 0);
|
digitalWrite(PIN_LED_YELLOW, 0);
|
||||||
digitalWrite(PIN_LED_RED, 1);
|
digitalWrite(PIN_LED_RED, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// printValues();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ppm > 2000) {
|
if (ppm > 2000) {
|
||||||
|
@ -110,3 +75,24 @@ void loop() {
|
||||||
delay(NOISE_DELAY);
|
delay(NOISE_DELAY);
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void printValues() {
|
||||||
|
Serial.print("Temperature = ");
|
||||||
|
Serial.print(bme.readTemperature());
|
||||||
|
Serial.println(" *C");
|
||||||
|
|
||||||
|
Serial.print("Pressure = ");
|
||||||
|
|
||||||
|
Serial.print(bme.readPressure() / 100.0F);
|
||||||
|
Serial.println(" hPa");
|
||||||
|
|
||||||
|
Serial.print("Approx. Altitude = ");
|
||||||
|
Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
|
||||||
|
Serial.println(" m");
|
||||||
|
|
||||||
|
Serial.print("Humidity = ");
|
||||||
|
Serial.print(bme.readHumidity());
|
||||||
|
Serial.println(" %");
|
||||||
|
|
||||||
|
Serial.println();
|
||||||
|
}
|
112
test/main.cpp
Normal file
112
test/main.cpp
Normal file
|
@ -0,0 +1,112 @@
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include <MQ135.h>
|
||||||
|
#include <dht.h>
|
||||||
|
|
||||||
|
#define PIN_MQ135 A0
|
||||||
|
#define PIN_DHT11 A1
|
||||||
|
|
||||||
|
#define PIN_LED_GREEN DD2
|
||||||
|
#define PIN_LED_YELLOW DD3
|
||||||
|
#define PIN_LED_RED DD4
|
||||||
|
#define PIN_NOISE DD5
|
||||||
|
|
||||||
|
#define MEASURE_DELAY 1000
|
||||||
|
#define NOISE_DELAY 100
|
||||||
|
|
||||||
|
MQ135 co2_sensor = MQ135(PIN_MQ135);
|
||||||
|
dht dht_sensor;
|
||||||
|
const int maxCount = MEASURE_DELAY / NOISE_DELAY;
|
||||||
|
int count = 0;
|
||||||
|
float ppm = -1;
|
||||||
|
bool noise = false;
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
Serial.begin(9600);
|
||||||
|
pinMode(PIN_MQ135, INPUT);
|
||||||
|
pinMode(PIN_DHT11, INPUT);
|
||||||
|
pinMode(PIN_LED_GREEN, OUTPUT);
|
||||||
|
pinMode(PIN_LED_YELLOW, OUTPUT);
|
||||||
|
pinMode(PIN_LED_RED, OUTPUT);
|
||||||
|
pinMode(PIN_NOISE, OUTPUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
float measure() {
|
||||||
|
Serial.print(RZERO);
|
||||||
|
Serial.println("---------------------------");
|
||||||
|
Serial.print("DHT:\t");
|
||||||
|
int chk = dht_sensor.read11(PIN_DHT11);
|
||||||
|
switch (chk)
|
||||||
|
{
|
||||||
|
case DHTLIB_OK:
|
||||||
|
Serial.print("OK,\t");
|
||||||
|
break;
|
||||||
|
case DHTLIB_ERROR_CHECKSUM:
|
||||||
|
Serial.print("Checksum error,\t");
|
||||||
|
break;
|
||||||
|
case DHTLIB_ERROR_TIMEOUT:
|
||||||
|
Serial.print("Time out error,\t");
|
||||||
|
break;
|
||||||
|
case DHTLIB_ERROR_CONNECT:
|
||||||
|
Serial.print("Connect error,\t");
|
||||||
|
break;
|
||||||
|
case DHTLIB_ERROR_ACK_L:
|
||||||
|
Serial.print("Ack Low error,\t");
|
||||||
|
break;
|
||||||
|
case DHTLIB_ERROR_ACK_H:
|
||||||
|
Serial.print("Ack High error,\t");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
Serial.print("Unknown error,\t");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Serial.println();
|
||||||
|
|
||||||
|
Serial.print ("temperature: ");
|
||||||
|
Serial.println (dht_sensor.temperature);
|
||||||
|
Serial.print ("humidity: ");
|
||||||
|
Serial.println (dht_sensor.humidity);
|
||||||
|
|
||||||
|
float val = analogRead(A0);
|
||||||
|
Serial.print ("raw = ");
|
||||||
|
Serial.println (val);
|
||||||
|
float zero = co2_sensor.getCorrectedRZero(dht_sensor.temperature, dht_sensor.humidity);
|
||||||
|
Serial.print ("rzero: ");
|
||||||
|
Serial.println (zero);
|
||||||
|
float ppm = co2_sensor.getCorrectedPPM(dht_sensor.temperature, dht_sensor.humidity);
|
||||||
|
Serial.print ("ppm: ");
|
||||||
|
Serial.println (ppm);
|
||||||
|
return ppm;
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
if (count >= maxCount || ppm < 0) {
|
||||||
|
ppm = measure();
|
||||||
|
count = 0;
|
||||||
|
if (ppm < 1000) {
|
||||||
|
digitalWrite(PIN_LED_GREEN, 1);
|
||||||
|
digitalWrite(PIN_LED_YELLOW, 0);
|
||||||
|
digitalWrite(PIN_LED_RED, 0);
|
||||||
|
}
|
||||||
|
else if (ppm <= 2000) {
|
||||||
|
digitalWrite(PIN_LED_GREEN, 0);
|
||||||
|
digitalWrite(PIN_LED_YELLOW, 1);
|
||||||
|
digitalWrite(PIN_LED_RED, 0);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
digitalWrite(PIN_LED_GREEN, 0);
|
||||||
|
digitalWrite(PIN_LED_YELLOW, 0);
|
||||||
|
digitalWrite(PIN_LED_RED, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ppm > 2000) {
|
||||||
|
noise = !noise;
|
||||||
|
digitalWrite(PIN_NOISE, noise);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
digitalWrite(PIN_NOISE, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
delay(NOISE_DELAY);
|
||||||
|
count++;
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue