Matériel :

  • un arduino MEGA 256 ou compatible et sa liaison PC
  • une breadboard
  • une sonde DHT22
  • une résistance de 10Kohm
  • des cables dupont

Schéma de câblage :

schema

Code :

Note :
Il faut télécharger le code et les librairies Adafruit pour la sonde DHT22 (il fonctionne aussi pour la DHT11 et DHT21).
Etant mon premier projet sur Arduino, j'ai eu une erreur car il me manqué la librairie Adafruit_Sensor.

Il faut mettre les dossiers DHT et Adafruit_Sensor dans le dossier librairies d'Arduino.
Pour Mac c'est dans ~/Documents/Arduino/libraries.

Le code est fournit avec la librairie, dans ~/Documents/Arduino/libraries/DHT/examples/DHTtester/DHTtester.ino

 1 // Example testing sketch for various DHT humidity/temperature sensors
 2 // Written by ladyada, public domain
 3 
 4 #include "DHT.h"
 5 
 6 #define DHTPIN 2     // what digital pin we're connected to
 7 
 8 // Uncomment whatever type you're using!
 9 //#define DHTTYPE DHT11   // DHT 11
10 #define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
11 //#define DHTTYPE DHT21   // DHT 21 (AM2301)
12 
13 // Connect pin 1 (on the left) of the sensor to +5V
14 // NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
15 // to 3.3V instead of 5V!
16 // Connect pin 2 of the sensor to whatever your DHTPIN is
17 // Connect pin 4 (on the right) of the sensor to GROUND
18 // Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
19 
20 // Initialize DHT sensor.
21 // Note that older versions of this library took an optional third parameter to
22 // tweak the timings for faster processors.  This parameter is no longer needed
23 // as the current DHT reading algorithm adjusts itself to work on faster procs.
24 DHT dht(DHTPIN, DHTTYPE);
25 
26 
27 void setup() {
28   Serial.begin(9600);
29   Serial.println("DHTxx test!");
30 
31   dht.begin();
32 }
33 
34 void loop() {
35   // Wait a few seconds between measurements.
36   delay(2000);
37 
38   // Reading temperature or humidity takes about 250 milliseconds!
39   // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
40   float h = dht.readHumidity();
41   // Read temperature as Celsius (the default)
42   float t = dht.readTemperature();
43   // Read temperature as Fahrenheit (isFahrenheit = true)
44   float f = dht.readTemperature(true);
45 
46   // Check if any reads failed and exit early (to try again).
47   if (isnan(h) || isnan(t) || isnan(f)) {
48     Serial.println("Failed to read from DHT sensor!");
49     return;
50   }
51 
52   // Compute heat index in Fahrenheit (the default)
53   float hif = dht.computeHeatIndex(f, h);
54   // Compute heat index in Celsius (isFahreheit = false)
55   float hic = dht.computeHeatIndex(t, h, false);
56 
57   Serial.print("Humidity: ");
58   Serial.print(h);
59   Serial.print(" %\t");
60   Serial.print("Temperature: ");
61   Serial.print(t);
62   Serial.print(" *C ");
63   Serial.print(f);
64   Serial.print(" *F\t");
65   Serial.print("Heat index: ");
66   Serial.print(hic);
67   Serial.print(" *C ");
68   Serial.print(hif);
69   Serial.println(" *F");
70 
71 }

Résultat dans la console :

1 DHTxx test!
2 Humidity: 48.70 %    Temperature: 23.00 *C 73.40 *F Heat index: 22.63 *C 72.73 *F
3 Humidity: 48.70 %    Temperature: 23.00 *C 73.40 *F Heat index: 22.63 *C 72.73 *F
4 Humidity: 48.80 %    Temperature: 22.90 *C 73.22 *F Heat index: 22.52 *C 72.54 *F
5 Humidity: 48.80 %    Temperature: 23.00 *C 73.40 *F Heat index: 22.63 *C 72.73 *F
6 Humidity: 48.80 %    Temperature: 23.00 *C 73.40 *F Heat index: 22.63 *C 72.73 *F
7 ...