Le capteur utilisé est un DHT 11 Keyes, soudé sur une carte :
Attention, les connecteurs sont, de gauche à droite broche « S »=Signal, centre = +5v, droite = GND. La broche Signal doit être raccordée à une PIN Digitale d’Arduino, le centre sur la broche 5v, la branche GND sur GND de l’Arduino.
Dans l’exemple ci-dessous, le capteur est relié à la broche Digitale 4 d’une carte UNO. Il est nécessaire de télécharger la libraire ici et de la charger dans l’interface de l’Arduino via la menu Croquis->Importer Bibliothèque et choisir le fichier ZIP.
/* 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 4
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 ) */
Résultat :
http://arduino-info.wikispaces.com/DHT11-Humidity-TempSensor avec l’Internet Shield : http://arduino-info.wikispaces.com/ethernet-temp-humidity
http://playground.arduino.cc/main/DHT11Lib
Autre brochage utilisé pour le DHT11 :
Pin | Name | Description |
1 | VDD | Power supply 3 – 5.5 V DC |
2 | DATA | Serial data output |
3 | NC | Not connected |
4 | GND | Ground |
Le capteur se connecte sur l’Arduino selon :
DHT11 | Arduino |
Pin 1 | Vcc |
Pin 2 | Analog(x) |
Pin 4 | Gnd |
Pour son utilisation : http://www.hobbyist.co.nz/?q=documentations/wiring-up-dht11-temp-humidity-sensor-to-your-arduino
1°) Téléchargez la librairie ici (https://app.box.com/s/7sngfwjxmchpk1fcurzg) Fichiers à dézipper dans le répertoire Librairies\DHT
2°) Exemple de code :
#include <dht.h>
#define dht_dpin A0 //no ; here. Set equal to channel sensor is on
dht DHT;
void setup(){
Serial.begin(9600);
delay(300);//Let system settle
Serial.println("Humidity and temperature\n\n");
delay(700);//Wait rest of 1000ms recommended delay before
//accessing sensor
}//end "setup()"
void loop(){
//This is the "heart" of the program.
DHT.read11(dht_dpin);
Serial.print("Current humidity = ");
Serial.print(DHT.humidity);
Serial.print("% ");
Serial.print("temperature = ");
Serial.print(DHT.temperature);
Serial.println("C ");
delay(800);//Don't try to access too frequently... in theory
//should be once per two seconds, fastest,
//but seems to work after 0.8 second.
}// end loop()
Un autre exemple sans librairie :
int tempPin=A0;
float readTemp(int pin)
{
float bv=analogRead(pin);
float mv=bv*5/1024.0;
float temperatureC = (mv - 0.5) * 100 ;
return temperatureC;
}
float readAvgTemp(int pin, int waitDelay, int count)
{
int k=0;
float sum=0.0;
for(k=0;k<count;k++)
{
sum=sum + readTemp(pin);
delay(waitDelay);
}
float avg=sum/(float)count;
return avg;
}
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.print("Temperature : ");
Serial.println(readAvgTemp(tempPin, 1, 100));
delay(100);
}
Un autre exemple :
#define DHT11_PIN 0 // ADC0
byte read_dht11_dat()
{
byte i = 0;
byte result=0;
for(i=0; i< 8; i++)
{
while(!(PINC & _BV(DHT11_PIN))); // wait for 50us
delayMicroseconds(30);
if(PINC & _BV(DHT11_PIN))
result |=(1<<(7-i));
while((PINC & _BV(DHT11_PIN))); // wait '1' finish
}
return result;
}
void setup()
{
DDRC |= _BV(DHT11_PIN);
PORTC |= _BV(DHT11_PIN);
Serial.begin(9600);
Serial.println("Ready");
}
void loop()
{
byte dht11_dat[5];
byte dht11_in;
byte i;// start condition
// 1. pull-down i/o pin from 18ms
PORTC &= ~_BV(DHT11_PIN);
delay(18);
PORTC |= _BV(DHT11_PIN);
delayMicroseconds(40);
DDRC &= ~_BV(DHT11_PIN);
delayMicroseconds(40);
dht11_in = PINC & _BV(DHT11_PIN);
if(dht11_in)
{
Serial.println("dht11 start condition 1 not met");
return;
}
delayMicroseconds(80);
dht11_in = PINC & _BV(DHT11_PIN);
if(!dht11_in)
{
Serial.println("dht11 start condition 2 not met");
return;
}
delayMicroseconds(80);// now ready for data reception
for (i=0; i<5; i++)
dht11_dat[i] = read_dht11_dat();
DDRC |= _BV(DHT11_PIN);
PORTC |= _BV(DHT11_PIN);
byte dht11_check_sum = dht11_dat[0]+dht11_dat[1]+dht11_dat[2]+dht11_dat[3];// check check_sum
if(dht11_dat[4]!= dht11_check_sum)
{
Serial.println("DHT11 checksum error");
}
Serial.print("Current humdity = ");
Serial.print(dht11_dat[0], DEC);
Serial.print(".");
Serial.print(dht11_dat[1], DEC);
Serial.print("% ");
Serial.print("temperature = ");
Serial.print(dht11_dat[2], DEC);
Serial.print(".");
Serial.print(dht11_dat[3], DEC);
Serial.println("C ");
delay(2000);
}
Exemple de Sketch stockant les valeurs dans une chaîne :
//**** Put temperature in a String
#include <dht11.h>
//-- Define var
dht11 DHT11;
#define DHT11PIN 4
int chk;
float nFloat;
String sMessage;
char buffer[3]; //--Nombre total de chiffre y compris la virgule
//-- Setup
void setup()
{
Serial.begin(9600);
Serial.println("DHT11 TEST PROGRAM ");
Serial.print("LIBRARY VERSION: ");
Serial.println(DHT11LIB_VERSION);
Serial.println();
}
//--Sketch
void loop()
{ chk = DHT11.read(DHT11PIN);
Serial.print("Read sensor: ");
switch (chk)
{case 0:
nFloat=(float)DHT11.temperature;
sMessage=dtostrf(nFloat,2,0,buffer);
nFloat=(float)DHT11.humidity;
sMessage=sMessage+"c"+dtostrf(nFloat,3,0,buffer)+"%";
Serial.println(sMessage);
break;
case -1: Serial.println("Checksum error"); break;
case -2: Serial.println("Time out error"); break;
default: Serial.println("Unknown error"); break;
}
delay(1000);
}
Exemple de sketch affichant l’heure, la date, la température et l’humidité :
////////////////////////////////////////
// Print Time and Date from DS1307 I2C
// And Temp with DHT 11
///////////////////////////////////////
#include "Wire.h"
#include <dht11.h>
//-- Define var
dht11 DHT11;
#define DHT11PIN 4
#define DS1307_ADDRESS 0x68
String sMessage;
int chk;
float nFloat;
char buffer[3];
//-- Setup
void setup(){
Wire.begin();
Serial.begin(9600);
}
//-- Sketch
void loop(){
sMessage="";
printDate();
printTemp();
Serial.println(sMessage);
delay(1000);
}
//-- Function
byte bcdToDec(byte val) {
// Convert binary coded decimal to normal decimal numbers
return ( (val/16*10) + (val%16) );
}
String sNb(int nNb)
{if (nNb<10)
return "O"+String(nNb);
else
return String(nNb);
}
void printDate(){
Wire.beginTransmission(DS1307_ADDRESS);
byte zero = 0x00;
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
int second = bcdToDec(Wire.read());
int minute = bcdToDec(Wire.read());
int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
int monthDay = bcdToDec(Wire.read());
int month = bcdToDec(Wire.read());
int year = bcdToDec(Wire.read());
String sSec, sMin, sHour, sDay, sMonth, sYear;
//print the date
sMessage=sNb(monthDay)+"/"+sNb(month)+"/"+sNb(year)+" "+sNb(hour)+":"+sNb(minute)+":"+sNb(second);
}
void printTemp(){
chk = DHT11.read(DHT11PIN);
switch (chk)
{case 0:
nFloat=(float)DHT11.temperature;
sMessage=sMessage+" "+dtostrf(nFloat,2,0,buffer);
nFloat=(float)DHT11.humidity;
sMessage=sMessage+"c"+dtostrf(nFloat,3,0,buffer)+"%";
break;
case -1: Serial.println("Checksum error"); break;
case -2: Serial.println("Time out error"); break;
default: Serial.println("Unknown error"); break;
}
}
Votre commentaire