Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
108 lines (85 sloc) 2.87 KB
/*
Vera Arduino MQ135
connect the sensor as follows :
A H A >>> 5V
B >>> A0
H >>> GND
B >>> 10K ohm >>> GND
Contribution: epierre
Based on David Gironi http://davidegironi.blogspot.fr/2014/01/cheap-co2-meter-using-mq135-sensor-with.html
http://skylink.dl.sourceforge.net/project/davidegironi/avr-lib/avr_lib_mq135_01.zip
License: Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)
*/
#include <Sleep_n0m1.h>
#include <SPI.h>
#include <RF24.h>
#include <EEPROM.h>
#include <Sensor.h>
#include <Wire.h>
#define CHILD_ID_AIQ 0
#define AIQ_SENSOR_ANALOG_PIN 0
#define MQ135_DEFAULTPPM 392 //default ppm of CO2 for calibration
#define MQ135_DEFAULTRO 41763 //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
unsigned long SLEEP_TIME = 30; // Sleep time between reads (in seconds)
//VARIABLES
//float Ro = 10000.0; // this has to be tuned 10K Ohm
float mq135_ro = 10000.0; // this has to be tuned 10K Ohm
int val = 0; // variable to store the value coming from the sensor
float valAIQ =0.0;
float lastAIQ =0.0;
Sensor gw;
Sleep sleep;
void setup()
{
gw.begin();
// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("AIQ Sensor", "1.0");
// Register all sensors to gateway (they will be created as child devices)
gw.sendSensorPresentation(CHILD_ID_AIQ, 22);
}
/*
* get the calibrated ro based upon read resistance, and a know ppm
*/
long mq135_getro(long resvalue, double ppm) {
return (long)(resvalue * exp( log(MQ135_SCALINGFACTOR/ppm) / MQ135_EXPONENT ));
}
/*
* get the ppm concentration
*/
double mq135_getppm(long resvalue, long ro) {
double ret = 0;
double validinterval = 0;
validinterval = resvalue/(double)ro;
if(validinterval<MQ135_MAXRSRO && validinterval>MQ135_MINRSRO) {
ret = (double)MQ135_SCALINGFACTOR * pow( ((double)resvalue/ro), MQ135_EXPONENT);
}
return ret;
}
void loop()
{
uint16_t val = analogRead(AIQ_SENSOR_ANALOG_PIN);// Get AIQ value
Serial.println(val);
mq135_ro = mq135_getro(val, MQ135_DEFAULTPPM);
//convert to ppm (using default ro)
valAIQ = mq135_getppm(val, MQ135_DEFAULTRO);
Serial.print ( "Vrl / Rs / ratio:");
Serial.print ( val);
Serial.print ( " / ");
Serial.print ( mq135_ro);
Serial.print ( " / ");
Serial.print ( valAIQ);
if (valAIQ != lastAIQ) {
gw.sendVariable(CHILD_ID_AIQ, V_VAR1, (int)ceil(valAIQ));
lastAIQ = ceil(valAIQ);
}
// Power down the radio. Note that the radio will get powered back up
// on the next write() call.
delay(1000); //delay to allow serial to fully print before sleep
gw.powerDown();
sleep.pwrDownMode(); //set sleep mode
sleep.sleepDelay(SLEEP_TIME * 1000); //sleep for: sleepTime
}
You can’t perform that action at this time.