add bme280

This commit is contained in:
GrafZeppelin 2021-03-30 17:57:27 +02:00
parent 3797d45f1e
commit a794de5eb8
5 changed files with 159 additions and 76 deletions

View file

@ -3,13 +3,10 @@
@file MQ135.cpp
@author G.Krocker (Mad Frog Labs)
@license GNU GPLv3
First version of an Arduino Library for the MQ135 gas sensor
TODO: Review the correction factor calculation. This currently relies on
the datasheet but the information there seems to be wrong.
@section HISTORY
v1.0 - First release
*/
/**************************************************************************/
@ -19,7 +16,6 @@ v1.0 - First release
/**************************************************************************/
/*!
@brief Default constructor
@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
@param[in] t The ambient air temperature
@param[in] h The relative humidity
@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
@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
for temp/hum
@param[in] t The ambient air temperature
@param[in] h The relative humidity
@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)
@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
for temp/hum
@param[in] t The ambient air temperature
@param[in] h The relative humidity
@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
@return The sensor resistance RZero in kOhm
*/
/**************************************************************************/
@ -111,13 +98,11 @@ float MQ135::getRZero() {
/*!
@brief Get the corrected resistance RZero of the sensor for calibration
purposes
@param[in] t The ambient air temperature
@param[in] h The relative humidity
@return The corrected sensor resistance RZero in kOhm
*/
/**************************************************************************/
float MQ135::getCorrectedRZero(float t, float h) {
return getCorrectedResistance(t, h) * pow((ATMOCO2/PARA), (1./PARB));
}
}

View file

@ -25,7 +25,7 @@ v1.0 - First release
#define RLOAD 10.0
/// Calibration resistance at atmospheric CO2 level
// #define RZERO 76.63
#define RZERO 480
#define RZERO 4000
/// Parameters for calculating ppm of CO2 from sensor resistance
#define PARA 116.6020682
#define PARB 2.769034857

View file

@ -8,7 +8,7 @@
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:uno]
[env:mega]
platform = atmelavr
board = uno
framework = arduino
board = megaatmega2560
framework = arduino

View file

@ -1,10 +1,9 @@
#include <Arduino.h>
#include <MQ135.h>
#include <dht.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define PIN_MQ135 A0
#define PIN_DHT11 A1
#define PIN_LED_GREEN DD2
#define PIN_LED_YELLOW DD3
#define PIN_LED_RED DD4
@ -13,74 +12,38 @@
#define MEASURE_DELAY 1000
#define NOISE_DELAY 100
#define SEALEVELPRESSURE_HPA (1013.25)
MQ135 co2_sensor = MQ135(PIN_MQ135);
dht dht_sensor;
Adafruit_BME280 bme(10, 11, 12, 13);
const int maxCount = MEASURE_DELAY / NOISE_DELAY;
int count = 0;
float ppm = -1;
int count = -1;
float ppm = 0;
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.begin(9600);
if (!bme.begin()) {
Serial.println(F("Could not find a valid BME280 sensor, check wiring!"));
}
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 printValues();
void loop() {
if (count >= maxCount || ppm < 0) {
ppm = measure();
if (count >= maxCount || count < 0) {
ppm = co2_sensor.getPPM();
Serial.print ("ppm: ");
Serial.println (ppm);
Serial.print ("rzero: ");
Serial.println (co2_sensor.getRZero());
count = 0;
if (ppm < 1000) {
digitalWrite(PIN_LED_GREEN, 1);
@ -97,6 +60,8 @@ void loop() {
digitalWrite(PIN_LED_YELLOW, 0);
digitalWrite(PIN_LED_RED, 1);
}
// printValues();
}
if (ppm > 2000) {
@ -109,4 +74,25 @@ void loop() {
delay(NOISE_DELAY);
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
View 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++;
}