Using the Digital Humidity and temperature sensor
Have you ever realized that there are some days when the weather seems so nice to you and some days when it feels like your enemy already? Okay lets look at something today.
This same air we move in day in day out has some wonderful properties that we humans are sensitive to and care to know about. Depending on the quantity or quality of theses properties in the air we may surely experience some effects.
And it’s not just humans, the sytems and machines and robots we build, may also need to operate in some condusive enviroments, so it is very important for them too to know what the weather says, yeah!
And hei of course there are so many applications out there which would require you to measure or know properties like humidity and temperature. So today I want to show us how to use this awesome DHT11 sensor.
The DHT11 sensor
The DHT11 is a cool two in one sensor. It measures both humidity and then temperature and this is because both properties of the air have some direct influence on each other.
If You’ve been using some sensors for sometime you’d realize most sensors produce analog outputs which gives you a range of values that describe the quantity or quality of the property being measured.
It’s the same with humidity and temperature. With the DHT11 you will see relative humidity being measured between 20% to 90% on the average and you will see temperature being measured between 0°C to 50°C.
But there is something special about the DHT11 you should know; its output is digital and so don’t be tempted to hook the data pin to the analog pins of your microcontroller. Another mystery is that, it is the same single output pin that spits out both the humidity and temperature values.
Mmm dont be scared or confused already you will get everything soon! Lets go ahead and build this super simple circuit.
As you can see, hooking up the DHT11 to the arduino is very simple. The data pin of your module is to be connected to any of the digital pins of your microcontroller. In our case above, it was connected to pin 4.
Let’s go for the codes now….
Coding the DHT11 sensor
Of the many sensors you will find yourself using, the DHT sensors are quite complicated. Retrieving data from this sensor is not as simple as it is for other sensors. It requires a special timing protocol to get the data out. But the good news is that, there is a library we will use that handles all the complicated things and makes coding it very simple. You should download the dht sensor library directly from the arduino site or from your library manager if you dont have it yet.
/* This sample code works for both dht11 and dht22. change read11 to read22 if it is dht22
Download dht library from https://playground.arduino.cc/Main/DHTLib
*/
#include <dht.h>
#define dataPin 4 // This defines the pin number to which the sensor is connected to
dht mydhtsensor; // This creates a DHT object for your sensor. Change the name to anything else
void setup() {
Serial.begin(9600);//begin the Serial monitor to display the sensor readings
}
void loop() {
mydhtsensor.read11(dataPin); // Reads the raw data from the sensor::::change read11 to read22 if your sensor is dht22
float temp = mydhtsensor.temperature; // This retrieves the temperature value from the raw data
float humid = mydhtsensor.humidity; // This retrieves the humidity value from the raw data
// Print the results on the serial monitor
Serial.print("Temperature = ");
Serial.print(temp);//print the temperature value
Serial.print(" *C ");
Serial.print(" Humidity = ");
Serial.print(humid);//print the humidity value
Serial.println(" % ");
delay(2000); /* wait for 2 seconds or more before taking another reading.
a lesser delay may cause disruptions in the data being read
*/
}
So yh that’s basically it for the codes too. The comments in the code do well to explain the program. And as you can see, if your sensor is a dht22 you only have change the “read11” to “read22“. It is very important.
Applications of DHT sensors.
Such a sensor which can measure both humidity and temperature can be useful in so many ways:
- Heating, ventilation and air conditioning systems
- Can be used to build mini weather stations
- For medical equipments that measure humidity
- For home automations systems
- To monitor and control the enviroment of your hydroponics farm
- and many more you can think about.
If you also know of any applications or projects where such a sensor is useful, let us all know in the comment section below.
So hei genius, this is it for totoday! Go be you..
And dont forget to share your cool projects with the rest of us in the Aaenics community.
Stay blessed!
Wow, this is awesome