This guide also available on Youtube here:
The DHT11 Humidity and temp sensor comes in a small package similar to that below
In the version used here there are three connection pins but in others I’ve seen there have been four but one has been unused. The three connections are Vcc,Gnd and Signal. The connection diagram is shown below:
In order to talk to this device you will need two libraries from Adafruit. The first is specific to the DHT11.
https://github.com/adafruit/DHT-sensor-library
If you do not know how to install a Arduino Library see this guide.
The second library is the Adafruit Common sensor library, which as the name suggests is a library (created by Adafruit) that has common functions in used by their other sensor libraries. If you’ve not already installed it for another sensor then it can be found here:
https://github.com/adafruit/Adafruit_Sensor
Edits to the code
Look at this line in the code (should be at line 6)
#define DHTPIN 2
This is stating which digital pin is going to be supplying data from the sensor, we have used pin 7 in our circuit, so change this to
#define DHTPIN 7
Lines 8-11 also need some editing;
8 9 10 11 |
// Uncomment whatever type you're using! //#define DHTTYPE DHT11 // DHT 11 #define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 //#define DHTTYPE DHT21 // DHT 21 (AM2301) |
This shows that the Adafruit library can work with various similar sensors, we are using the DHT11 so comment out the #define DHTTYPE DHT22 and uncomment the //#define DHTTYPE DHT11. The code should look as follows;
8 9 10 11 |
// Uncomment whatever type you're using! #define DHTTYPE DHT11 // DHT 11 //#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 //#define DHTTYPE DHT21 // DHT 21 (AM2301) |
Testing
Open the supplied example at “File->Examples->DHT Sensor Library->DHTtester“. Compile and upload. Once upload data i fed back over the serial link, open “Tools->Serial Monitor”. You should see a screen similar to this one;
Using with an OLED
If you know how to connect up an OLED to your system you can have a more pleasing readout of data, see this guide if you are not sure how to connect up an OLED screen. The code below will output the data to this screen:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
#include <Adafruit_Sensor.h> #include "DHT.h" #include <Adafruit_SSD1306.h> #define OLED_Address 0x3C #define DHTTYPE DHT11 // DHT 11 #define DHTPIN 7 // what digital pin we're connected to DHT dht(DHTPIN, DHTTYPE); Adafruit_SSD1306 oled(1); float StartPressure; void setup() { oled.begin(SSD1306_SWITCHCAPVCC, OLED_Address); oled.clearDisplay(); oled.setTextSize(1); oled.setTextColor(WHITE); dht.begin(); } void loop() { oled.clearDisplay(); // Wait a few seconds between measurements. delay(2000); // Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) float h = dht.readHumidity(); // Read temperature as Celsius (the default) float t = dht.readTemperature(); // Read temperature as Fahrenheit (isFahrenheit = true) float f = dht.readTemperature(true); // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t) || isnan(f)) { oled.setCursor(0,0); Serial.println("Failed to read from DHT sensor!"); return; } // Compute heat index in Fahrenheit (the default) float hif = dht.computeHeatIndex(f, h); // Compute heat index in Celsius (isFahreheit = false) float hic = dht.computeHeatIndex(t, h, false); oled.setCursor(0,0); oled.print("Humidity: "); oled.print(h); oled.print(" %"); oled.setCursor(0,16); oled.print("Temperature: "); oled.print(t); oled.print(" *C "); oled.display(); delay(2000); } |
That’s it for now, hope this helps you use one of these sensors. If you look at the other sensor guides on this site (Pressure and Temp ) you should now have enough information to make a small weather station. All that’s needed is a small anemometer (wind speed indicator).