Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
Comments removed, DHT11 library and Fritzing file added.
  • Loading branch information
clarholm committed Jun 5, 2013
1 parent 5f5dd35 commit 89d47d3
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 33 deletions.
Binary file added DHT11 library/.DS_Store
Binary file not shown.
85 changes: 85 additions & 0 deletions DHT11 library/dht11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//
// FILE: dht11.cpp
// VERSION: 0.3.2
// PURPOSE: DHT11 Temperature & Humidity Sensor library for Arduino
// LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html)
//
// DATASHEET: http://www.micro4you.com/files/sensor/DHT11.pdf
//
// HISTORY:
// George Hadjikyriacou - Original version (??)
// Mod by SimKard - Version 0.2 (24/11/2010)
// Mod by Rob Tillaart - Version 0.3 (28/03/2011)
// + added comments
// + removed all non DHT11 specific code
// + added references
//

#include "dht11.h"

// returnvalues:
// 0 : OK
// -1 : checksum error
// -2 : timeout
int dht11::read(int pin)
{
// BUFFER TO RECEIVE
uint8_t bits[5];
uint8_t cnt = 7;
uint8_t idx = 0;

// EMPTY BUFFER
for (int i=0; i< 5; i++) bits[i] = 0;

// REQUEST SAMPLE
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
delay(18);
digitalWrite(pin, HIGH);
delayMicroseconds(40);
pinMode(pin, INPUT);

// ACKNOWLEDGE or TIMEOUT
unsigned int loopCnt = 10000;
while(digitalRead(pin) == LOW)
if (loopCnt-- == 0) return -2;

loopCnt = 10000;
while(digitalRead(pin) == HIGH)
if (loopCnt-- == 0) return -2;

// READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
for (int i=0; i<40; i++)
{
loopCnt = 10000;
while(digitalRead(pin) == LOW)
if (loopCnt-- == 0) return -2;

unsigned long t = micros();

loopCnt = 10000;
while(digitalRead(pin) == HIGH)
if (loopCnt-- == 0) return -2;

if ((micros() - t) > 40) bits[idx] |= (1 << cnt);
if (cnt == 0) // next byte?
{
cnt = 7; // restart at MSB
idx++; // next byte!
}
else cnt--;
}

// WRITE TO RIGHT VARS
// as bits[1] and bits[3] are allways zero they are omitted in formulas.
humidity = bits[0];
temperature = bits[2];

uint8_t sum = bits[0] + bits[2];

if (bits[4] != sum) return -1;
return 0;
}
//
// END OF FILE
//
37 changes: 37 additions & 0 deletions DHT11 library/dht11.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// FILE: dht11.h
// VERSION: 0.3.2
// PURPOSE: DHT11 Temperature & Humidity Sensor library for Arduino
// LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html)
//
// DATASHEET: http://www.micro4you.com/files/sensor/DHT11.pdf
//
// URL: http://arduino.cc/playground/Main/DHT11Lib
//
// HISTORY:
// George Hadjikyriacou - Original version
// see dht.cpp file
// *** Terry King: Changed include Arduino.h for 1.0x
// include core Wiring API and now Arduino
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

#ifndef dht11_h
#define dht11_h

#define DHT11LIB_VERSION "0.3.2"

class dht11
{
public:
int read(int pin);
int humidity;
int temperature;
};
#endif
//
// END OF FILE
//
103 changes: 103 additions & 0 deletions DHT11 library/examples/DHT11_test_YD.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/* YourDuino.com Example Software Sketch
DHT11 Humidity and Temperature Sensor test
Credits: Rob Tillaart
http://arduino-direct.com/sunshop/index.php?l=product_detail&p=162
terry@yourduino.com */

/*-----( Import needed libraries )-----*/
#include <dht11.h>

/*-----( Declare objects )-----*/
dht11 DHT11;

/*-----( Declare Constants, Pin Numbers )-----*/
#define DHT11PIN 2

void setup() /*----( SETUP: RUNS ONCE )----*/
{
Serial.begin(9600);
Serial.println("DHT11 TEST PROGRAM ");
Serial.print("LIBRARY VERSION: ");
Serial.println(DHT11LIB_VERSION);
Serial.println();
}/*--(end setup )---*/

void loop() /*----( LOOP: RUNS CONSTANTLY )----*/
{
Serial.println("\n");

int chk = DHT11.read(DHT11PIN);

Serial.print("Read sensor: ");
switch (chk)
{
case 0: Serial.println("OK"); break;
case -1: Serial.println("Checksum error"); break;
case -2: Serial.println("Time out error"); break;
default: Serial.println("Unknown error"); break;
}

Serial.print("Humidity (%): ");
Serial.println((float)DHT11.humidity, 2);

Serial.print("Temperature (oC): ");
Serial.println((float)DHT11.temperature, 2);

Serial.print("Temperature (oF): ");
Serial.println(Fahrenheit(DHT11.temperature), 2);

Serial.print("Temperature (K): ");
Serial.println(Kelvin(DHT11.temperature), 2);

Serial.print("Dew Point (oC): ");
Serial.println(dewPoint(DHT11.temperature, DHT11.humidity));

Serial.print("Dew PointFast (oC): ");
Serial.println(dewPointFast(DHT11.temperature, DHT11.humidity));

delay(2000);
}/* --(end main loop )-- */

/*-----( Declare User-written Functions )-----*/
//
//Celsius to Fahrenheit conversion
double Fahrenheit(double celsius)
{
return 1.8 * celsius + 32;
}

//Celsius to Kelvin conversion
double Kelvin(double celsius)
{
return celsius + 273.15;
}

// dewPoint function NOAA
// reference: http://wahiduddin.net/calc/density_algorithms.htm
double dewPoint(double celsius, double humidity)
{
double A0= 373.15/(273.15 + celsius);
double SUM = -7.90298 * (A0-1);
SUM += 5.02808 * log10(A0);
SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
SUM += log10(1013.246);
double VP = pow(10, SUM-3) * humidity;
double T = log(VP/0.61078); // temp var
return (241.88 * T) / (17.558-T);
}

// delta max = 0.6544 wrt dewPoint()
// 5x faster than dewPoint()
// reference: http://en.wikipedia.org/wiki/Dew_point
double dewPointFast(double celsius, double humidity)
{
double a = 17.271;
double b = 237.7;
double temp = (a * celsius) / (b + celsius) + log(humidity/100);
double Td = (b * temp) / (a - temp);
return Td;
}

/* ( THE END ) */

Binary file added Ketosense_0.1.fzz
Binary file not shown.
50 changes: 17 additions & 33 deletions ketosense.ino → ketosense/ketosense.ino
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,6 @@ void loop() {
readsensor();
//Check if all readings are the same which indictae a stabile behaviour and if the value is higher or lower update global max and min variables.
updateNewMaxOrMinWithTempHumidity(tempRead1, tempRead1, tempRead1);
//Debug start
//updateNewMaxOrMin(acetoneResistanceToPPM(toResistance(tempRead1)), acetoneResistanceToPPM(toResistance(tempRead2)), acetoneResistanceToPPM(toResistance(tempRead3)));
//updateNewMaxOrMin(acetoneResistanceToPPMf(toResistance(tempRead1)), acetoneResistanceToPPMf(toResistance(tempRead2)), acetoneResistanceToPPMf(toResistance(tempRead3)));
//updateNewMaxOrMin(tempRead1, tempRead1, tempRead1);
//Debug stop


//print result to display if current value is different to previous value and update the highest value.
if (acetoneResistanceToPPMf(toResistance(temperatureScaledValue)) != lastPPM){
lastPPM = acetoneResistanceToPPMf(toResistance(temperatureScaledValue));
Expand Down Expand Up @@ -234,14 +227,15 @@ void readsensor(){
//Update screen with result
void updateScreen(){
clearLcd();
// printToRow1("Acetone in PPM");
///// DEBUG start
printToRow1("H:");
printIntToCurrentCursorPossition((int)currentHumidity);
printStringToCurrentCursorPossition(" T:");
printIntToCurrentCursorPossition((int)currentTemperature);
printStringToCurrentCursorPossition(" S:");
printFloatToCurrentCursorPossition((float)scalingFactor);
// printStringToCurrentCursorPossition(" S:");
// printFloatToCurrentCursorPossition((float)scalingFactor);
printStringToCurrentCursorPossition(" R:");
printIntToCurrentCursorPossition((int)GlobalMaxValue);
///// Debug end

printToRow2("Now: Max: ");
Expand All @@ -262,7 +256,7 @@ void updateScreen(){
}
}


//calculate the gas concentration relative to the resistance
int acetoneResistanceToPPMf(float resistance){
double tempResistance = (double)resistance;
double PPM;
Expand All @@ -283,7 +277,7 @@ int acetoneResistanceToPPMf(float resistance){



//read reset max pin
//debounce function
boolean debounce(boolean last, int pin )
{
boolean current = digitalRead(pin);
Expand All @@ -295,40 +289,29 @@ boolean debounce(boolean last, int pin )
return current;
}

//temperature sensor function, values has been hardcoded to humidity = 60 and temperature = 28 to speed up the measuring.
int tempHumidityCompensation(int value){
int chk = DHT11.read(DHT11PIN);
delay(300);
/*
delay(20);
switch (chk){
case 0:
break;
case -1:
tempHumidityCompensation(value);
case -2:
tempHumidityCompensation(value);
default:
tempHumidityCompensation(value);
}
*/
//currentHumidity = ((double)DHT11.humidity);
//Hardcoded after realizing that the temperature and humidity were beahving satbilly.
//Hardcoded after realizing that the temperature and humidity were beahaving stabilly.
currentHumidity = 60;
//currentTemperature = ((double)DHT11.temperature);
currentTemperature = 28;
//function derrived from regression analysis of the graph in the datasheet
scalingFactor = (((currentTemperature * -0.02573)+1.898)+((currentHumidity*-0.011)+0.3966));
debug
clearLcd();
printToRow1("Scalefactor:");
printFloatToCurrentCursorPossition((float)scalingFactor);
//debug
//clearLcd();
//printToRow1("Scalefactor:");
//printFloatToCurrentCursorPossition((float)scalingFactor);
delay(1000);
//debugstop*/
double scaledValue = value * scalingFactor;
return (int)scaledValue;

}


//check if we have new max or min after temperature and humidity scaling has been done.
void updateNewMaxOrMinWithTempHumidity(int value1, int value2, int value3){
if (value1 == value2 && value1 == value3){
temperatureScaledValue = tempHumidityCompensation(value1);
Expand All @@ -347,7 +330,7 @@ void updateNewMaxOrMinWithTempHumidity(int value1, int value2, int value3){
}
}
}
//check if the value of the sensor has been stabile for
//check if we have new max or min without temperature and humidity scaling.
void updateNewMaxOrMin(int value1, int value2, int value3){
if (value1 == value2 && value1 == value3){
if (GlobalMaxValue==0){
Expand All @@ -365,7 +348,7 @@ void updateNewMaxOrMin(int value1, int value2, int value3){
}
}

//Convert the 1-1023 value from gas sensor analog read to a resistance.
//Convert the 1-1023 voltage value from gas sensor analog read to a resistance, 9800 is the value of the other resistor in the voltage divide.
float toResistance(int reading){
float resistance = ((5/toVoltage(reading) - 1) * 9800);
return resistance;
Expand Down Expand Up @@ -485,3 +468,4 @@ boolean checkIfSensorIsStabile()
}



0 comments on commit 89d47d3

Please sign in to comment.