Hello everyone! Today, I’ll talk about those famous sensors that come to your rescue when you’re wondering “What is the temperature outside?” or “What is the humidity level?” in your projects: DHT11 and DHT22. These tiny yet powerful devices have become a must-have for electronics enthusiasts like me. Initially, I was a bit hesitant about these sensors, feeling they required complicated setups, but after trying them, I realized how easy they actually are, in my opinion.
Now, you might ask, ‘What exactly do these DHT11 and DHT22 do?’ It’s quite simple. One measures temperature, and the other measures both temperature and humidity. When the humidity rises too high at home, or when heating dries out the air in winter, these sensors provide us with relevant information. For example, if you’re building a smart home system and want to automatically control heating or cooling, these sensors come into play. In my beginner projects, I used them for simpler automation, like plant watering systems. Isn’t that wonderful?
So, which one should we choose? DHT11 or DHT22? Honestly, it depends on your needs and budget. DHT11 is more common and cheaper. But if you require more precise measurements—say, temperature accuracy within ±2°C and humidity within ±5%—then DHT22 is a better option. DHT22 offers higher accuracy, with temperature precision of ±0.5°C and humidity of ±2-5%. Also, DHT22 has a wider measurement range, I believe. Of course, it costs a bit more. When I started, I used DHT11, but as my projects grew, I realized the difference DHT22 makes.
What do you need to use these? They are generally used with microcontrollers like Arduino. There are ready-made libraries for these sensors, which make our job much easier. You don’t need deep electronics knowledge. Just install the Arduino IDE, connect the sensor to the correct pins, add the library, and everything else falls into place. Isn’t that great?
Now, let me share a simple Arduino code. This code is configured to work with both DHT11 and DHT22. You just need to make a few small adjustments depending on which sensor you are using. The code reads temperature and humidity from the sensor and prints the values to the serial monitor. You can see these values live in the Arduino IDE’s Serial Monitor. Try this simple example before saying ‘My program failed.’
Before jumping into the code, let me briefly explain the connection schema for these sensors. They usually have 3 or 4 pins. One is VCC (power), another is GND (ground), and the others are for data output. Some models have an extra pin, but most work with just 3 pins. When connecting, check the datasheet of the sensor, but generally connect VCC to 5V, GND to ground, and DATA pin to a digital pin of Arduino. Sometimes, miswiring happens, so checking the datasheet is essential.
Here’s the sample code. You can copy and paste it into your Arduino IDE. Just remember to change the `DHTTYPE` line in row 7 depending on your sensor. Use `DHT11` for DHT11, or `DHT22` for DHT22. It’s that simple.
#include <DHT.h> // Include the DHT sensor library.#define DHTPIN 2 // The digital pin number where the sensor is connected. Adjust this to your wiring. #define DHTTYPE DHT11 // Specify the type of your sensor (DHT11 or DHT22). Currently testing with DHT11 but you can also use DHT22.
DHT dht(DHTPIN, DHTTYPE); // Create DHT object.
void setup() { Serial.begin(9600); // Start serial communication at 9600 baud rate. Serial.println(F("DHT sensor test!")); // Print initial message to serial monitor.
dht.begin(); // Initialize the DHT sensor. }
void loop() { // It's good to wait a bit before reading again, as rapid readings may produce errors. delay(2000); // Wait for 2 seconds.
// Read humidity. float h = dht.readHumidity(); // Read temperature in Celsius. float t = dht.readTemperature();
// Check if reading is successful. if (isnan(h) || isnan(t)) { Serial.println(F("Failed to read from DHT sensor!")); return; }
// Convert temperature to Fahrenheit (optional). float f = dht.computeHeatIndex(t, h, false); // Set third parameter to 'true' for Fahrenheit.
// Print the values to serial monitor. Serial.print(F("Humidity: ")); Serial.print(h); Serial.print(F("%\t")); // Percent sign and tab for separation. Serial.print(F("Temperature: ")); Serial.print(t); Serial.print(F("°C ")); // Degree symbol. Serial.print(F("Heat Index: ")); Serial.print(f); Serial.println(F("°C")); // Print the heat index and new line. }
This code works quite well, but sometimes inaccurate readings can occur. You know, sensors can be a bit temperamental. For example, loose cables or high electrical noise can cause problems. That’s why the `isnan(h) || isnan(t)` check is helpful to display errors. Also, some older Arduino boards or incompatible libraries may cause issues. I faced these situations myself and had to try different libraries. Anyway, with this code, you can comfortably read temperature and humidity values.
Now, let’s talk about the use cases for these sensors. As I mentioned, smart home systems top the list. Managing humidity can prevent mold issues, and controlling temperature can save energy. Besides home automation, they can be used in greenhouse automation, air quality monitoring systems, or even to optimize conditions for your pets’ living environment. If you enjoy gardening like me, you can also use them for plants—monitoring when they need watering or how much light they get. I believe the possibilities with these sensors are limited only by our imagination.
By the way, more advanced models of these sensors exist, but for beginners, DHT11 and DHT22 are really sufficient. You can find hundreds of projects online using these sensors. For example, if you search for “DHT11 DHT22 Arduino project” on Google, you’ll be amazed at the results. Sometimes you think “I didn’t think of this!” and get inspired. There are also many videos on YouTube with detailed explanations. Explore them for more ideas.
Ultimately, DHT11 and DHT22 are excellent sensors for both beginner hobby projects and more complex automation systems. Their setup and usage are straightforward, and they are affordable. If you want to add some “smartness” to your projects, I recommend giving these sensors a try. They may seem like a small step, but the results will surprise you. Isn’t that great? Just connect them to the right pins and use the correct library.
Finally, keep in mind that the accuracy of these sensors might not be suitable for highly precise laboratory environments. For higher precision, sensors like BME280 may be necessary. But for general projects, the DHT series is sufficient. I also think these sensors work well in humid cities like Bursa. 🙂 That’s all for now! See you in another article, take care!