DHTxx series are simple and cheap temperature and humidity sensors perfect for DIY projects. They contain a basic chip converting analog values to a digital form, so it is very easy to read them with a microcontroller like Arduino. Both values are sent via a single data pin.
DHT11 or DHT22?
You can choose one of two models – either DHT11 or DHT22. It is going to depend mostly on your budget and required temperature/humidity range. Specific differences are in the table:
DHT11 | DHT22 | |
---|---|---|
Price | Ultra low cost | Low cost |
Temperature range | 0 to 50°C | -40 to 125°C |
Temperature accuracy | ±2°C | ±0.5°C |
Humidity range | 20 to 80% | 0 to 100% |
Humidity accuracy | 5% | 2-5% |
Sampling rate | 1 Hz (once per second) | 0.5 Hz (once per two seconds) |
Size | 15.5 x 12 x 5.5 mm | 15.1 x 25 x 7.7 mm |
Connection
The connection between DHT and Arduino is very straightforward. The sensor pins also fit perfectly into a breadboard. There are four pins:
- VCC (3 to 5V power)
- Data
- Not used
- GND
The third pin can be ignored, it is not connected anywhere. For VCC use the Arduino 5V power output, and GND for ground. The DHT data pin can be connected to any microcontroller input pin.
When you bought the sensor, it is very likely that a resistor 4.7k or 10k was included. It should be used as a pull-up resistor between VCC and Data pins, because the data line to DHT is bidirectional – Arduino sends a request and after that receives data. However it might work also without the resistor thanks to the builtin board pull-ups (not recommended as they are weak).
For this project I also used a 16×2 LCD display with I2C bus connected to Arduino SDA/SCL pins, so the board can be independent on the computer to display results.
Code
The code is very simple because it utilizes libraries – DHT library created by Adafruit to read data and LiquidCrystal_I2C to print temperature and humidity to an LCD display (in addition to a serial monitor). Make sure you install both using Arduino IDE menu Sketch -> Include Library -> Manage Libraries.
#include "DHT.h" #include LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display #define DHTPIN 2 // sensor's Data pin #define DHTTYPE DHT22 // sensor type DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(9600); Serial.println("Initializing..."); // initialize the LCD lcd.init(); lcd.backlight(); lcd.setCursor(0, 0); lcd.print("Initializing..."); dht.begin(); } void loop() { float h = dht.readHumidity(); float c = dht.readTemperature(); // Celsius float f = dht.readTemperature(true); // Fahrenheit if (isnan(h) || isnan(c) || isnan(f)) { Serial.println("Failed to read from DHT sensor!"); return; } lcd.setCursor(0, 0); lcd.print("T: "); lcd.print(c, 1); // print only one decimal digit due to limited space lcd.print("C | "); lcd.print(f, 1); // print only one decimal digit due to limited space lcd.print("F"); lcd.setCursor(0, 1); lcd.print("H: "); lcd.print(h); lcd.print("%"); Serial.print("Humidity: "); Serial.print(h); Serial.print(" %\t"); Serial.print("Temperature: "); Serial.print(c); Serial.print(" *C "); Serial.print(f); Serial.print(" *F\t"); delay(2000); }
0 Comment for "Arduino Uno Temperature and Humidity Sensor DHT11 or DHT22"