Skip to content

Commit

Permalink
small fixes
Browse files Browse the repository at this point in the history
moved to V2 SHT11 lib aka sensirion
  • Loading branch information
empierre committed Sep 18, 2014
1 parent f3ba0e3 commit f4936f8
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 37 deletions.
114 changes: 114 additions & 0 deletions MQ135_2.ino
@@ -0,0 +1,114 @@
/*
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
*/

#include <SPI.h>
#include <MySensor.h>
#include <Wire.h>

#define CHILD_ID_AIQ 0
#define AIQ_SENSOR_ANALOG_PIN 0

#define MQ135_DEFAULTPPM 399 //default ppm of CO2 for calibration
#define MQ135_DEFAULTRO 68550 //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 = 30000; // 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;

MySensor gw;
MyMessage msg(CHILD_ID_AIQ, V_VAR1);

void setup()
{
gw.begin();

// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("AIQ Sensor MQ135", "1.0");

// Register all sensors to gateway (they will be created as child devices)
gw.present(CHILD_ID_AIQ, S_AIR_QUALITY);

}

/*
* 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 valr = analogRead(AIQ_SENSOR_ANALOG_PIN);// Get AIQ value
Serial.println(val);
uint16_t val = ((float)22000*(1023-valr)/valr);
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.send(msg.set((int)ceil(valAIQ)));
lastAIQ = ceil(valAIQ);
}

// Power down the radio. Note that the radio will get powered back up
// on the next write() call.
gw.sleep(SLEEP_TIME); //sleep for: sleepTime
}


/***************************** MQGetPercentage **********************************
Input: rs_ro_ratio - Rs divided by Ro
pcurve - pointer to the curve of the target gas
Output: ppm of the target gas
Remarks: By using the slope and a point of the line. The x(logarithmic value of ppm)
of the line could be derived if y(rs_ro_ratio) is provided. As it is a
logarithmic coordinate, power of 10 is used to convert the result to non-logarithmic
value.
************************************************************************************/
int MQGetPercentage(float rs_ro_ratio, float ro, float *pcurve)
{
return (double)(pcurve[0] * pow(((double)rs_ro_ratio/ro), pcurve[1]));
}
49 changes: 26 additions & 23 deletions MQv01dgi_1_4.ino
Expand Up @@ -21,20 +21,19 @@
#include <DHT.h>
#include <Adafruit_BMP085.h>

#define CHILD_ID_AIQ 0
#define MQ2_SENSOR 0
#define MQ6_SENSOR 1
#define MQ131_SENSOR 2
#define TGS2600_SENSOR 3
#define MQ135_SENSOR 4
#define S2SH12_SENSOR 15
#define DUST_SENSOR_ANALOG_PIN 11
#define DUST_SENSOR_DIGITAL_PIN 13
#define HUMIDITY_SENSOR_DIGITAL_PIN 6
#define PRESSURE_SENSOR_ANALOG_PIN 14

/************************Hardware Related Macros************************************/
#define MQ_SENSOR (0) //define which analog input channel you are going to use
#define RL_VALUE (5) //define the load resistance on the board, in kilo ohms
#define MQ2_SENSOR (0) //define which analog input channel you are going to use
#define MQ6_SENSOR (1)
#define MQ131_SENSOR (2)
#define TGS2600_SENSOR (3)
#define MQ135_SENSOR (4)
#define S2SH12_SENSOR (15)
#define DUST_SENSOR_ANALOG_PIN (11)
#define DUST_SENSOR_DIGITAL_PIN (13)
#define HUMIDITY_SENSOR_DIGITAL_PIN (6)
#define PRESSURE_SENSOR_ANALOG_PIN (14)
#define RL_VALUE (22000) //define the load resistance on the board, in ohms
#define RO_CLEAN_AIR_FACTOR (9.83) //RO_CLEAR_AIR_FACTOR=(Sensor resistance in clean air)/RO,
/***********************Software Related Macros************************************/
#define CALIBRATION_SAMPLE_TIMES (50) //define how many samples you are going to take in the calibration phase
Expand All @@ -43,7 +42,7 @@
#define READ_SAMPLE_INTERVAL (50) //define how many samples you are going to take in normal operation
#define READ_SAMPLE_TIMES (5) //define the time interal(in milisecond) between each samples in
/***********************Software Related Macros************************************/
#define CALIBARAION_SAMPLE_TIMES (50) //define how many samples you are going to take in the calibration phase
#define CALIBRATION_SAMPLE_TIMES (50) //define how many samples you are going to take in the calibration phase
#define CALIBRATION_SAMPLE_INTERVAL (500) //define the time interal(in milisecond) between each samples in the
//cablibration phase
#define READ_SAMPLE_INTERVAL (50) //define how many samples you are going to take in normal operation
Expand Down Expand Up @@ -92,12 +91,12 @@ float Ro = 10; //Ro is initialized to 10
unsigned long SLEEP_TIME = 600; // Sleep time between reads (in seconds)
//VARIABLES
//float Ro = 10000.0; // this has to be tuned 10K Ohm
float Ro0 = 4.340; //MQ2 3.83 this has to be tuned 10K Ohm
float Ro1 = 1.755; //MQ6 25.76 this has to be tuned 10K Ohm
float Ro2 = 2.501; //MQ131 2.24 this has to be tuned 10K Ohm
float Ro3 = 2.511; //TGS2600 0.05 this has to be tuned 10K Ohm
float Ro4 = 2.511; //MQ135 2.51 this has to be tuned 10K Ohm
float Ro5 = 2.51; //2SH12 2.51 this has to be tuned 10K Ohm
float Ro0 = 4340; //MQ2 3.83 this has to be tuned 10K Ohm
float Ro1 = 1755; //MQ6 25.76 this has to be tuned 10K Ohm
float Ro2 = 2501; //MQ131 2.24 this has to be tuned 10K Ohm
float Ro3 = 2511; //TGS2600 0.05 this has to be tuned 10K Ohm
float Ro4 = 2511; //MQ135 2.51 this has to be tuned 10K Ohm
float Ro5 = 2511; //2SH12 2.51 this has to be tuned 10K Ohm
int val = 0; // variable to store the value coming from the sensor
float valAIQ0 =0.0;
float lastAIQ0 =0.0;
Expand Down Expand Up @@ -307,7 +306,7 @@ void loop()
//MQ135 CO NH4 CH3 CO2
Serial.print("MQ135 :");
Serial.print("CO2 :");
Serial.print(MQGetGasPercentage(MQRead(MQ135_SENSOR),Ro4,GAS_CO2,MQ135_SENSOR) );
Serial.print(MQGetGasPercentage(MQRead(MQ135_SENSOR),68550,GAS_CO2,MQ135_SENSOR) );
Serial.print( "ppm" );
Serial.print(" ");
Serial.print("CO :");
Expand Down Expand Up @@ -390,11 +389,11 @@ float MQCalibration(int mq_pin)
int i;
float val=0;

for (i=0;i<CALIBARAION_SAMPLE_TIMES;i++) { //take multiple samples
for (i=0;i<CALIBRATION_SAMPLE_TIMES;i++) { //take multiple samples
val += MQResistanceCalculation(analogRead(mq_pin));
delay(CALIBRATION_SAMPLE_INTERVAL);
}
val = val/CALIBARAION_SAMPLE_TIMES; //calculate the average value
val = val/CALIBRATION_SAMPLE_TIMES; //calculate the average value

val = val/RO_CLEAN_AIR_FACTOR; //divided by RO_CLEAN_AIR_FACTOR yields the Ro
//according to the chart in the datasheet
Expand Down Expand Up @@ -502,6 +501,10 @@ int MQGetPercentage(float rs_ro_ratio, float ro, float *pcurve)
return (double)(pcurve[0] * pow(((double)rs_ro_ratio/ro), pcurve[1]));
}

/***************************** MQGetPercentage **********************************
Input: pressure
Output: an int containing the weather based on pressure
************************************************************************************/
int sample(float pressure) {
// Algorithm found here
// http://www.freescale.com/files/sensors/doc/app_note/AN3914.pdf
Expand Down
25 changes: 11 additions & 14 deletions SoilMoistSensorSHT1x.ino
Expand Up @@ -3,20 +3,19 @@
connect the sensor as follows :
VCC -- Red = VCC (3-5VDC)
GND -- Black = Ground
Pin 8 -- Yellow = Clock
Pin 3 -- Green = Data.
VCC -- Red = VCC (3-5VDC)
GND -- Black or Green = Ground
Pin 8 -- Yellow = Clock
Pin 3 -- Blue = Data.
Contribution: epierre
Based on David Gironi http://davidegironi.blogspot.fr/2014/01/cheap-co2-meter-using-mq135-sensor-with.html
Contribution: epierre
SHT1x library V2.0 10Dec2010 : follow instructions http://playground.arduino.cc/code/Sensirion#SHT1x
License: Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)
*/

#include <SPI.h>
#include <MySensor.h>
#include "SHT1x.h"
#include "Sensirion.h"

#define DIGITAL_INPUT_SOIL_SENSOR 3 // Digital input did you attach your soil sensor.
#define CHILD_ID_TEMP 0 // Id of the sensor child
Expand All @@ -34,7 +33,7 @@
// example: @ 0/50 degrees, +/- 1.2 degrees


SHT1x th_sensor(DIGITAL_INPUT_SOIL_SENSOR, sckPin);
Sensirion th_sensor=Sensirion(DIGITAL_INPUT_SOIL_SENSOR, sckPin);

MySensor gw;
MyMessage msgtemp(CHILD_ID_TEMP, V_TEMP);
Expand All @@ -59,12 +58,10 @@ void loop()
{
float temp_c;
float humid;
float dewpoint;
// Read values from the sensor
humid = th_sensor.readHumidity();
// Since the humidity reading requires the temperature we simply
// retrieve the reading capture from the readHumidity() call. See the lib.
temp_c = th_sensor.readTemperatureC();

th_sensor.measure(&temp_c, &humid, &dewpoint);


if (temp_c != lastTempValue) {
Serial.println(temp_c);
Expand Down

0 comments on commit f4936f8

Please sign in to comment.