We are now going to talk about one small cheap sensor that we call DHT11, and we can use that for measuring humidity and also temperature. You can see that many Arduino people like this sensor because it is very simple and also very low cost, so anybody can buy and try.
The DHT11 can sense humidity from around 20 percent up to 90 percent and also it can measure temperature from 0 degree Celsius up to 50 degree Celsius. But the accuracy is not very high, still for basic hobby circuits it is good enough and people keep using it.
Understanding DHT11 pins
This DHT11 has four pins in total when it is in a original, raw sensor form, as shown below.

- Pin one is VCC that we connect with 5 V supply.
- Pin two is the data pin through which all readings are sent to Arduino.
- Pin three is not connected with anything.
- Pin four is the ground pin that we connect with Arduino ground.
So as we can see above, actually only three pins are useful for us.
Sometimes this sensor comes in module form as shown below, where already one pull up resistor is fixed inside.

But when you use the raw sensor then you have to connect one external pull up resistor like 10 K from data pin to VCC.
How DHT11 works
Inside this DHT11 there is one humidity sensing capacitor and also one thermistor type part.
There is also one small chip inside that takes the values and converts them into digital signal. This digital signal is transmitted in one special single wire format.
It is not like UART or I2C but it is very simple protocol where the timing of high pulses and low pulses carry the data. So you cannot read it directly with analog pin.
You need one microcontroller like Arduino that has proper library which can decode those pulses and then it will show you humidity and temperature values.
Connecting DHT11 to Arduino
Now let us see how to connect DHT11 with Arduino.
- We connect pin one of DHT11 with 5 V of Arduino.
- We connect pin four of DHT11 with ground of Arduino.
- Then we connect pin two of DHT11 with any digital pin of Arduino like pin two.
- Between pin two of DHT11 and 5 V supply we put one resistor of 10 K.

That is all we need for wiring this sensor with Arduino.
Hooking up Arduino with PC to read data
Now let us see how we can hook up Arduino with PC so that we can read the data coming from DHT11. First we connect the DHT11 sensor with Arduino like we already explained, VCC to 5 V, GND to ground, and DATA pin to any digital pin of Arduino like pin two.
If it is raw sensor then we also put 10 K resistor between data and VCC. After wiring is done we take one USB cable and connect Arduino board with PC.
This USB cable will give power to Arduino and also create serial communication channel with computer.
Understanding the setup and data
When we open Arduino IDE on computer then we need to select the right board from Tools menu, like Arduino Uno, and also the right COM port which is shown when Arduino is connected.
Then we upload the example code for DHT11 which includes the DHT library.
After uploading the code we go to Tools and open Serial Monitor. In Serial Monitor we set the baud rate to 9600 because that is what we used in the code.
Now every two seconds the Arduino will print humidity and temperature values. It will look like Humidity: 55 % Temperature: 28 *C or similar.
If sometimes sensor does not respond then you may see message like Failed to read from DHT sensor. That is normal because this sensor can miss data sometimes.
So basically the setup is simple. Arduino is connected with PC by USB cable. Arduino reads the data from sensor and sends the result to computer serial monitor.
You only need to select right port and baud rate to see the values. That is how we understand the data and check if sensor is working fine.
Arduino code for DHT11
Here is one simple Arduino sketch for reading data from DHT11 and printing it on serial monitor.
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT11 sensor");
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" % ");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C");
delay(2000);
}
Explanation of the code
So first we include the DHT library because that makes it very easy for us to work with this sensor. Then we define the pin where the data pin of sensor is connected, in this case it is pin number two.
We also define the sensor type as DHT11. After that we create one DHT object using those values so Arduino can use that for communication.
In the setup function we start the serial monitor at 9600 baud so that we can see the readings on the computer screen. We also call dht.begin so that Arduino initializes the sensor before using it.
In the loop part we call dht.readHumidity to get humidity value and also dht.readTemperature to get temperature value.
Both these functions give floating point numbers as result. We also check that if any of these readings is not a number then Arduino will print failed message because sometimes the sensor may not respond properly.
Then Arduino will print the humidity value with percentage sign and the temperature value with Celsius unit.
After that we put delay of 2000 milliseconds which means Arduino will take one reading every 2 seconds. This delay is important because DHT11 is slow and it cannot provide data faster than one reading per second.
Why DHT11 cannot give analog output
You must remember that DHT11 can never give analog output signal. It always sends digital bits using its own protocol.
So if you need analog output then you have to use another type sensor like LM35 or HIH type sensors.
Otherwise what you can do is use Arduino to read the DHT11 data and then use one Arduino PWM pin with RC filter to generate one analog like voltage.
This voltage will then vary according to the reading that DHT11 is giving.
Practical uses of DHT11
We can use DHT11 in many simple projects like weather station, greenhouse monitoring, home automation and other applications where we only need basic humidity and temperature sensing.
It is not very accurate and also a little slow because it can give only one reading per second. But for learning purpose and for small projects it is one of the best choices for beginners.
DHT11 Temperature Controller with Relay
Now let us understand how we can upgrade the above code with a relay driver circuit, so that we can switch ON or OFF an external load when a specified level of atmospheric temperature threshold is exceeded.
Full Arduino code
#include "DHT.h"
#define DHTPIN 2 // Pin where the DHT11 data is connected
#define DHTTYPE DHT11 // We are using DHT11 sensor
#define RELAYPIN 8 // Relay driver connected to digital pin 8
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
pinMode(RELAYPIN, OUTPUT);
digitalWrite(RELAYPIN, LOW); // Keep relay OFF at start
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" % Temperature: ");
Serial.print(t);
Serial.println(" *C");
// Example condition: switch relay ON if temperature goes above 30
if (t > 30) {
digitalWrite(RELAYPIN, HIGH);
Serial.println("Relay ON");
} else {
digitalWrite(RELAYPIN, LOW);
Serial.println("Relay OFF");
}
delay(2000);
}
Understand the Code
Now let us understand what is happening in this code. First we include the DHT library because that makes it easy to work with DHT11 sensor.
Then we define the DHT pin as pin 2 where sensor data pin is connected. We also define DHT type as DHT11 because we are using that sensor.
After that we define one more pin called RELAYPIN which is pin 8 of Arduino. This pin will go to the base of transistor of relay driver circuit.
In setup function we start the serial monitor at 9600 baud so that we can see data on computer. Then we start the DHT sensor using dht.begin.
We also set relay pin as OUTPUT and at beginning we write LOW so that relay remains OFF when Arduino starts.
In the loop function we read humidity with dht.readHumidity and temperature with dht.readTemperature. Both values are float type numbers.
Then we check if any value is not a number then Arduino prints failed message and skips the loop. This is important because DHT11 sometimes may not respond.
After that Arduino prints the humidity with percentage and temperature with Celsius on the serial monitor.
Then we check condition for relay. We say that if temperature is greater than 30 degree then Arduino will send HIGH to relay pin which means relay will turn ON.
If temperature is less than or equal to 30 then Arduino will send LOW to relay pin which means relay will remain OFF.
Finally we put delay of 2000 milliseconds, so Arduino will take one reading every 2 seconds. This is necessary because DHT11 is a slow sensor.
So with this arrangement you can not only read humidity and temperature but also use that information to drive a relay.
That relay can control fan, heater, or any other AC or DC load through proper driver circuit. You just need transistor, diode, and one relay to make the driver. Arduino only gives 5 V logic, so the transistor drives the relay coil, and the relay contact can switch any external load.
Circuit Diagram

Modifying the above Concept to Control Humidity with Relay
That is very easy to do. In the above code we are checking temperature value to decide relay ON or OFF but instead we can check humidity value and use that for switching the relay. Only small change in the code is required.
Here is the modified code for humidity control:
#include "DHT.h"
#define DHTPIN 2 // Pin where the DHT11 data pin is connected
#define DHTTYPE DHT11 // DHT11 sensor
#define RELAYPIN 8 // Relay driver pin
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
pinMode(RELAYPIN, OUTPUT);
digitalWrite(RELAYPIN, LOW); // Keep relay OFF at start
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" % Temperature: ");
Serial.print(t);
Serial.println(" *C");
// Example condition: switch relay ON if humidity goes above 70%
if (h > 70) {
digitalWrite(RELAYPIN, HIGH);
Serial.println("Relay ON (humidity high)");
} else {
digitalWrite(RELAYPIN, LOW);
Serial.println("Relay OFF (humidity low)");
}
delay(2000);
}
Understanding the Code
So in this code everything is almost same like before. We still use the DHT library, we still define pin 2 for sensor data and pin 8 for relay driver. Setup is also same where we start serial monitor and set relay pin as output.
The difference comes in the loop section. Here we read humidity value into variable h and temperature into variable t. Then we print both values on serial monitor.
Now instead of checking the temperature, we put condition on humidity. We say that if humidity is more than 70 percent then Arduino will write HIGH to relay pin which means relay will turn ON.
If humidity is equal or less than 70 percent then Arduino will write LOW to relay pin which means relay will stay OFF.
So this way the relay is now controlled fully by humidity value.
You can use this for applications like turning on a dehumidifier, fan, or exhaust when humidity becomes too high in a room, or turning off when humidity goes back down.
Basically you can change the number 70 to any other threshold value that you want. If you want relay to trigger at 60 percent or 80 percent you just change that number in the code.
Final Thoughts
So we saw that DHT11 is very easy to use with Arduino. We only need three wires and one resistor to make it work.
The Arduino library does all decoding work and we directly get temperature and humidity values.
But then we should not expect this sensor to give very accurate or very fast results.
For better precision we can use DHT22 or other advanced type sensors. Still for basic hobby circuits DHT11 is very useful and simple to use.




Need Help? Please Leave a Comment! We value your input—Kindly keep it relevant to the above topic!