View Course Path

Interfacing of Arduino Uno with Temperature and Humidity sensor

The second sensor in our Arduino Uno course is the DHT 11. Also known as the temperature and humidity sensor. In this post, we will discuss the complete interfacing of the DHT11 sensor with the Arduino Uno. Additionally, we will also understand the sensor’s features and working. The full code to display the data of the sensor on a serial monitor is given below. Also, upon scrolling further, you will find the code to display the DHT11’s data on an LCD too.

What are the components required for interfacing an Arduino Uno with the temperature and humidity sensor (DHT11)?

For interfacing DHT-11 with Arduino, we need the following components. Click on the links to check out these products on Amazon. Disclaimer: These purchases net us some money and help us maintain this website!

[geoip_detect2_hide_if country=”IN”] [/geoip_detect2_hide_if] [geoip_detect2_show_if country=”IN”] [/geoip_detect2_show_if]

Understanding the temperature and humidity sensor Arduino (DHT11)

DHT11 sensor with four pins
DHT11 sensor with four pins

DHT 11 is a sensor that measures two physical aspects of the environment. Temperature and Humidity. The water content present in the air is termed as humidity. The gaseous state of water is called vapor. As the temperature of the air increases, more water vapor can be generated. The DHT11 calculates relative humidity using capacitive measurement.

Furthermore, it can measure the surrounding temperature by using a thermistor. The DHT11 is available in two variants. A variant with three pins and another one with four.

DHT11 sensor with three pins
DHT11 sensor with three pins

Why should we interface the DHT 11 sensor with Arduino Uno?

We can sense the temperature around ourselves. Kind of an underrated superpower, but it’s incredible when you think about it. When you feel hot, your body starts to sweat to make your body cool. Conversely, when you feel cold, your body starts to shiver and burn fat to produce heat. How about giving this power to a machine? Maybe even getting a numerical read on it? Yes, that’s possible by using the DHT 11 sensor. Not the changing of temperatures, but the detection of changes in temperature and humidity.

What does the DHT 11 sensor do?

DHT 11 is used to measure the humidity content and temperature around it.

Humidity measurement in industries is critical. Moreover, the variation in humidity may affect the quality of the product. It plays a vital role in many industries, especially in the control systems for industrial processes like dryers, Ovens, film desiccation, Paper and textile production, and food processing, etc. It calculates the humidity by measuring the electrical resistance between the two electrodes

This sensor has NTC to measure the temperature and 8-bit microcontroller to output the values of temperature and humidity as serial data. It is easy to interface with other microcontrollers.

What is the difference between DHT11 and DHT22?

The DHT-11 is always better than DHT-22 because the data flow rate for DHT-11 is 1Hz or one reading per second. Whereas for DHT-22, the data flow rate is 0.5Hz, which is one reading for every two seconds. In short, the DHT 11 is faster to report changes in temperature and humidity.

What are the features of temperature and humidity module Arduino (DHT 11)?

  • Ultra-low-cost.
  • Operating Voltage: 3.5V – 5.5V
  • Operating current: 0.3mA to 60uA
  • Output: Serial data.
  • Temperature Range: 0°C – 50°C
  • Humidity Range: 20% to 90%
  • Resolution: Temperature and Humidity both are 8-bit.
  • Accuracy: ±1°C and ±1%
  • Compatible-interfaces 2.54 3-pin interface and 4-pin Grove interface

What are the physical dimensions of the temperature and humidity sensor?

DHT 11 sensor physical dimensions
DHT 11 sensor physical dimensions

DHT 11 – sensor size:

  • Height (without pins): 15.5 mm
  • Height (with pins): 23.5 mm
  • Length: 12 mm
  • Width : 5.5 mm

DHT 11 – PCB size :

  • 20.0mm X 20.5mm X1.6mm

Let’s get into the thick of things for a moment and understand the working of the DHT 11 sensor. If this takes some trouble to understand, I would suggest you trudge on and skip to the interfacing circuit here.

What is the working principle of DHT-11?

DHT-11 sensor determines the amount of moisture present in the air using capacitive measurement. It has two electrical conductors with a non-conducting polymer film present between them. The film collects the moisture of the air causes the changes in voltage levels between the two conducting plates. Notably, the humidity sensing layer of the DHT-11 sensor is a moisture holding substrate. When the substrate absorbs water vapor, the substrate starts to release the ions, which increases the conductivity between the electrodes. The change in resistance between the electrodes is proportional to the relative humidity. An increase in humidity decreases the resistance between the electrodes.

It calculates the temperature using a thermistor, which has an NTC-Negative Temperature Coefficient. The resistance decreases with the increase in temperature.

Humidity sensor of the DHT 11's working
The working of the humidity sensing element of the DHT 11

Thermistor Equation

Relation between the resistance of a thermistor and the ambient temperature
The relation between the resistance of a thermistor and the ambient temperature
thermistor equation
Thermistor Equation

Where:

  • T1 is the first temperature point in Kelvin
  • T2 is the second temperature point in Kelvin
  • R1 is the resistance of the thermistor at temperature T1 in Ohms
  • R2 is the resistance of the thermistor at temperature T2 in Ohms
Internal circuit of the DHT 11 sensor
The internal circuit of the DHT 11 sensor

The temperature and humidity module consists of three parts such as NTC temperature sensor, a Humidity sensing component, and an IC.

How does the NTC- Temperature sensing element or the thermistor of the DHT 11 sensor work?

Internal structure of the thermistor in a DHT 11 sensor
The internal structure of the thermistor in a DHT 11 sensor
  • A thermistor is a type of resistor whose resistance depends on temperature more than in other standard resistors
  • These sensors are made up of semiconductive materials such as ceramics or polymers to provide more significant changes in the resistance with just small changes in temperature.
  • A thermistor is a two-terminal solid-state thermally sensitive transducer made from sensitive semiconductor-based metal oxides with metalized or sintered connecting leads onto a ceramic disc or bead. This allows it to change its resistive value in proportion to small changes in temperature.
  • In other words, as its temperature changes, so does its resistance. “Thermistor” is a combination of the words THERM-ally sensitive resistor.

How does the Humidity sensing component of the DHT 11 work?

Internal structure of the humidity sensing element in a DHT 11 sensor
The internal structure of the humidity sensing element in a DHT 11 sensor
  • This humidity sensor consists of 2 copper conductors that are located at a small distance from each other. The relay switches as soon as the moisture makes a connection that is more or less electrically conductive between the two electrodes.
  • When the electrical resistance between the two sensors drops below a particular value, then the Schmitt trigger (T1 and T2) switches. The RS N1/N2 bistable multivibrator is flipped through C1 so that in point B, we have a low voltage and so T3 will close the relay.
  • The relay is opened when the 10K resistor is connected to point A, not to point B.

What are the pins present on the temperature sensor?

DHT11 sensor Pins
Pin diagram of DHT11 sensor: three pins variant (left) and four pins variant (right)

The DHT-11 sensor is available as a sensor and also as a module. But there is no variation in performance. The only difference is the sensor will come as a 4-pin package out of three pins will be used, whereas the module will come with three pins. For the sensor, you have to use them externally if required. On the other hand, the module will have a filtering capacitor and an inbuilt pull-up resistor.

Pin No:

Pin Name

Description

For Sensor

1 Vcc Power supply 3.5V to 5.5V
2 Data Outputs both Temperature and Humidity through serial Data
3 NC No Connection and hence not used
4 Ground Connected to the ground of the circuit

For module

1 Vcc Power supply 3.5V to 5.5V
2 Data Outputs both Temperature and Humidity through serial Data
3 Ground Connected to the ground of the circuit

What is the circuit design for the temperature and humidity module Arduino?

Arduino Uno is a development board that can read data from various inputs like sensors and also can compute it according to the program coded. Here we use Arduino Uno, which has 14 digital input/output pins and six analog pins with 32 KB flash memory.

You can switch on the Arduino using a USB cable or an external power supply. The external power supplies mainly include AC to DC adapter, otherwise a battery. The adapter can be connected to the Arduino Uno by plugging into the power jack of the Arduino board. Similarly, the battery leads can be connected to the Vin pin and the GND pin of the POWER connector. The suggested voltage range will be 7 volts to 12 volts.

How to connect the DHT-11 sensor with the Arduino Uno?

Arduino Uno connection with the DHT 11 sensor (4 pin)
Arduino Uno connection with the DHT 11 sensor (4 pin)
Arduino Uno connection with the DHT 11 sensor (4 pin)
Arduino Uno connection with the DHT 11 sensor (4 pins)

Circuit connections for Arduino interfacing with the DHT-11 sensor are shown above.

  • You can connect the Vcc pin of the DHT-11 sensor with 5V Vcc out pin of the Arduino Uno board.
  • Signal pin of humidity sensor should be connected with any of the digital pins from D0 to D13, which should be declared as a signal pin in your Arduino program in the Arduino Uno board. Further, a 10kohm pull-up resistor is connected between the signal line and the 5V line to make sure that the signal line stays high by default. In the case of the DHT-11 module, it has a built-in ten kΩ pull-up resistor. In that case, it doesn’t need any pull-up resistor between the signal line and the 5v line because
  • The ground pin of the ultrasonic sensor should be connected with any of the Arduino’s ground pin. 

Circuit diagram to interface DHT 11 with Arduino

Circuit diagram to interface DHT 11 with Arduino
Circuit diagram to interface DHT 11 with Arduino

Coding the temperature and humidity module Arduino

Open the Arduino IDE software on your computer. First, we need to connect the Arduino environment with our Arduino Uno board by selecting a suitable serial port. The Arduino code should contain two functions as void setup() and void loop(). In void setup() function, you can declare all pin modes. In void loop(), you can write the code, and it will run infinitely. These programs are usually done using a special Arduino syntax that is similar to embedded C.

How to interface DHT 11 without using the library? 

Usually, you can use a special library file named DHT.h while interfacing DHT-11 with Arduino, but it is also possible to interface DHT-11 without using any library. In this case, a single-wire communication can be established between any micro-controller and the DHT-11 sensor.

The length of the cable can be as long as 20 meters. The data given by the sensor has integral and decimal parts for both humidity and temperature. It consists of 40 bits,

#include <LiquidCrystal.h>
LiquidCrystal lcd(4, 5, 0, 1, 2, 3);
byte degree_symbol[8] =
{
0b00111,
0b00101,
0b00111,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000
};
int gate=11;
volatile unsigned long duration=0;
unsigned char i[5];
unsigned int j[40];
unsigned char value=0;
unsigned answer=0;
int z=0;
int b=1;
void setup()
{

lcd.begin(16, 2);
lcd.print("Temp = ");
lcd.setCursor(0,1);
lcd.print("Humidity = ");
lcd.createChar(1, degree_symbol);
lcd.setCursor(9,0);
lcd.write(1);
lcd.print("C");
lcd.setCursor(13,1);
lcd.print("%");
}

void loop()
{
delay(1000);

while(1)
{
delay(1000);

pinMode(gate,OUTPUT);

digitalWrite(gate,LOW);

delay(20);

digitalWrite(gate,HIGH);

pinMode(gate,INPUT_PULLUP);//by default it will become high due to internal pull up
// delayMicroseconds(40);

duration=pulseIn(gate, LOW);

if(duration <= 84 && duration >= 72)
{
while(1)
{
duration=pulseIn(gate, HIGH);

if(duration <= 26 && duration >= 20)
{
value=0;
}

else if(duration <= 74 && duration >= 65)
{
value=1;
}

else if(z==40)
{
break;
}

i[z/8]|=value<<(7- (z%8));
j[z]=value;
z++;
}
}
answer=i[0]+i[1]+i[2]+i[3];

if(answer==i[4] && answer!=0)
{
lcd.setCursor(7,0);
lcd.print(i[2]);
lcd.setCursor(11,1);
lcd.print(i[0]);
}

z=0;
i[0]=i[1]=i[2]=i[3]=i[4]=0;
}
}

How to add  DHT.h library to the code?

DHT-11 interfacing with Arduino can also be done by using a library called DHT.h. Here’s a tutorial on importing library zip files to your Arduino sketch. The DHT.h library that has many built-in functions such as:

  • DHT.read11(data pin) – to read the binary data that represents the relative humidity and temperature.
  • DHT.humidity – to separate the relative humidity value alone from the binary data set.
  • DHT.temperature – to separate the temperature value alone from the binary data set.

How to write a DHT 11 Code for Arduino to display temperature and humidity readings on the serial monitor?

If you want to display the readings from the DHT-11 on serial monitor, you can use the following source code:

#include <dht.h>
dht DHT;
#define D 4;
int relay_pin = 13;

void setup() {

Serial.begin(9600);

Serial.println("DHT test!");

pinMode(relay_pin, OUTPUT);

digitalWrite(relay_pin, HIGH);

}

void loop() {

int chk=DHT.read11(D);

float t =DHT.temperature ;

if (t > 30)
{
digitalWrite(relay_pin,LOW);
}
else
{
digitalWrite(relay_pin, HIGH);
}
Serial.print("Humidity:");

Serial.print(DHT.humidity,1);

Serial.print("\n");

Serial.print("temperature:");

Serial.print(DHT.temperature,1);

Serial.print("\n");

delay(700);

}

How to interface the DHT 11 with LCD with the Arduino?

An LCD module has 16 pins. It comes with a backlight and contrast adjustment functions. Here we are just gonna share the circuit connections to display temperature and humidity sensor data on the LCD screen. We also have a detailed tutorial on connecting an LCD with the Arduino Uno in case you want to get down to the nitty-gritty.

LCD pin out
LCD pinout
  1. Connect the LCD pin 1 (GND) to the ground pin of the Arduino.
  2. Pin 2 (Vcc) to the 5V Vcc pin of Arduino.
  3. Connect Pin 3 of the LCD (Vee) with the middle pin of the 10 k potentiometer.
  4. You can connect the fourth pin (RS) with any of the digital pins on the Arduino. However, make sure that you include this connection in the code accurately. The Register select pin controls whether commands or data is being sent to the LCD. When the pin is given a 0 (LOW), it accepts commands as input. When it is given a 1 (HIGH), it takes data as input.
  5. Connect the fifth pin (R/W) with the ground pin of the Arduino. As we are writing to the LCD, select the Write mode by connecting it to a LOW value.
  6. LCD pin 6(EN) connects with any of the digital pins on Arduino. This is the enable pin, and it allows the next seven pins of the LCD (D0-D7) to send 8-bit data.
  7. Connect LCD pin 7- pin 14(DB0-DB7) with any of the digital pins of the Arduino.
  8. Next, connect the fifteenth Pin (LED+) with the 5V Vcc pin of Arduino. To control the backlight of the LCD screen.
  9. Finally, LCD pin 16(LED-) should be connected with the ground pin of the Arduino.

The final circuit after we are done interfacing with the DHT 11 and the LCD with the Arduino Uno should look something like this.

Interfacing DHT 11 with Arduino and LCD
Interfacing DHT 11 with Arduino and LCD
Interfacing DHT 11 with Arduino and LCD schematic
DHT 11 with Arduino and LCD schematic

How to write a DHT 11 Code for Arduino to display temperature and humidity readings on the LCD screen?

Interfacing of Arduino with an LCD screen requires the LiquidCrystal.h library. Further, you can see the Arduino code for displaying readings of DHT-11 on the LCD screen.

#include <LiquidCrystal.h>

#include <dht.h>

dht DHT;

#define D 4

int relay_pin = 13;

LiquidCrystal lcd(0, 1, 8, 9, 10, 11);

void setup() {

//put your setup code here, to run once:

Serial.begin(9600);

Serial.println("DHT test!");

lcd.begin(16, 2);

pinMode(relay_pin, OUTPUT);

digitalWrite(relay_pin, HIGH);

}

void loop() {

int chk=DHT.read11(D);

float t =DHT.temperature ; // put your main code here, to run repeatedly:

float h=DHT.humidity;

float t=DHT.temperature;




lcd.clear();

lcd.setCursor(0,0);

lcd.print("Temp: ");

lcd.print(t);  //printing temperarture to the LCD display

if (t > 30){

digitalWrite(relay_pin,LOW);}

else

{

digitalWrite(relay_pin, HIGH);

}

lcd.print("'C");

lcd.setCursor(0,1);

lcd.print("Humidity: ");

lcd.print(h);  //printing humidity to the LCD display

lcd.print("%");

Serial.print("Humidity:");

Serial.print(DHT.humidity,1);

Serial.print("\n");

Serial.print("temperature:");

Serial.print(DHT.temperature,1);

Serial.print("\n");

delay(700);

}

Common errors and troubleshooting

While using the sensor, we need to add an extra 4.7 kΩ resistance with the 10kΩ pullup resistor to get the desired output. Make sure that the data pin next to the Vcc pin alone connects with the Arduino. You shouldn’t connect another data pin. Install the library properly, and you will have no errors.

Make sure to connect Read/Write, Enable pins to the Arduino’s transmitter, and receiver pins, which are D0 and D1. You can connect the LCD’s data pins with any of the Arduino’s digital pins. However, you should declare the connections appropriately in the program.

You have to connect a potentiometer with Vout to control the backlight. Else, the screen will appear shady. To get a crisp display, throw in a resistor of 1 kΩ or 3.9 kΩ with the LED+ Anode pin of the LCD for contrast control.

Liquid crystal display requires input voltage Vcc in the range between 4.7 V to 5.3 V. There can be fluctuations in the voltage given by 5V Vcc out pin of the Arduino. You can easily avoid this by using a stable power supply.

What are the different applications of the DHT-11 sensor?

The DHT-11 sensor will help you kind of give a type of sense to your machine to measure temperature and humidity around itself. After that, you can compute it according to your needs. Some simple and exciting projects using the Arduino and ultrasonic sensors are shown below.

  • Automated irrigation system.
  • Automated humidifier.
  • Temperature based fan controller.
  • Weather and plant monitoring system.

Got any issues completing this project? Let us know, and we will be happy to assist you! Check out the remaining projects in this Arduino course. You can find them in the sidebar to the side (or above if you are on your phone).

2 thoughts on “Interfacing of Arduino Uno with Temperature and Humidity sensor

  1. The details for the usage of DHT-11 and the lcd display is superb. Can you help me to add 2 relays so that one can switchon the exhaust at a time when the humidity raises and another one to switch on another such as an air cooler to raise the humidity in a room.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.