LCD Display of Temperature and Humidity using DHT11 & Arduino with Code

 In this Arduino Project we will use Arduino and DHT11 sensor to display Temperature and Humidity on LCD. We will also provide the Programming Code and video demonstration that shows temperature and humidity displayed on the LCD when heat and vapor is brought near it . 


DHT11 Humidity and Temperature Sensor

The DHT11 sensor can sense relative humidity and temperature in the environment and send the values to microcontrollers through it's data pin. It has three pins which are the Vcc, the voltage supply pin, the ground pin and the data pin. It can work with voltage in the range 3V to 5.5V. It can measure relative humidity in the range 20% to 90% with +/- 5% resolution. Similarly it can measure temperature in the range from 0 to 50 degree Celsius with +/- 2 degree resolution. 


Interfacing DHT11, LCD and Arduino UNO

The wiring diagram for interfacing DHT11, LCD and Arduino is shown below. We have connected the digital pin 7 of Arduino to the data pin of the DHT11 sensor. Also we have used the pins 2,3,4,5 and 11,12 of Arduino to connect to the 16x2 LCD. The power supply used is +5V and the LCD, Arduino and DHT11 should have common ground. A 10KOhm Potentiometer was connected to the LCD's Vcc, Vss and ground.

Video Demonstration of LCD display of Humidity & Temperature with DHT11 & Arduino


In the video you can see that when heat is near the DHT11 sensor the temperature increases. Similarly, water vapor was also applied(which is not visible in the video) and the humidity value increases as shown in the video.

Programming Code for DHT11, LCD and Arduino 

The following is program code for displaying humidity and temperature values on LCD read by Arduino from DHT11 sensor. 


#include <LiquidCrystal.h>

#include <DHT.h>    //include the DHT library


#define DHTTYPE DHT11 // define dhttype as dht11
#define DHTPIN 7     // Name dhtPin as pin 7


// initialize the LCD library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

DHT dht(DHTPIN, DHTTYPE);   // Initialize DHT sensor

void setup () {
	dht.begin();
	// set up the LCD's number of columns and rows:
	lcd.begin(16, 2);
	// Print a message to the LCD.
	lcd.print("DHT11");
	lcd.setCursor(0, 1);
	lcd.print("Humidity/Temp.");
}


void loop() {
	delay(2000);  // Wait two seconds between measurements
	float H = dht.readHumidity();     //Read Humidity
	float T = dht.readTemperature();    // Read temperature as Celsius

	  // Check if any reads failed and if exit
	  if (isnan(H) || isnan(T)) {
		lcd.print("Failed to read from DHT sensor!");
		return;
	  }

	lcd.clear();
	lcd.setCursor(0, 0);
	lcd.print("Humidity:"); 
	lcd.print(H);
	lcd.print("%");
	lcd.setCursor(0, 1);
	lcd.print("Temp.:"); 
	lcd.print(T);
	lcd.print((char)223);
	lcd.print("C");
}

In the above code, we have used two libraries, the LiquidCrystal and DHT library. The LiquidCrystal comes pre-installed with Arduino. But the DHT library from Adafruit has to be installed manually. You can see the DHT11 Humidity and Temperature Sensor with Arduino tutorial where we have explained how to install the DHT library. These two libraries header files, the LiquidCrystal.h and DHT.h, are included in the program above. 

In order to use the DHT library feature, we have to specify which DHT sensor we are using. The DHT library supports DHT11, DHT22 and DHT21 sensors. So to specify that we are using DHT11 we use the define DHTType DHT11 statement. Next we have to specify which microcontroller pin will be connected to the data pin of the sensor and as shown above in dht11 interfacing with arduino wiring diagram, we have to pin 7 and this is done using the define DHTPIN 7 statement.

Down the code we have used the LiquidCrystal lcd() and dht() function to initialize the LCD library and DHT library by specifying which pins are used for each case. 

In the setup() function, we initialize and start the DHT sensor using the dht.begin() and the LCD using the lcd.begin() function which requires to specify number of columns(16) and rows(2). After this, we use the print() function to print string "DHT11" on the LCD. The setCursor(0, 1) function puts the string following it("Humidity/Temp.") in the 2nd line of the LCD.

In the loop() function, we first use delay() function to wait for 2 second for DHT sensor and LCD to initialize. Next we have used the readHumidity() and readTemperature() methods of object dht to read humidity and temperature and have stored them in the float variable H and T. The next if statement is to check whether the data read from the sensor are valid or not. If the data read is not valid then error message is shown otherwise the next statement following it are executed. If there is no error, the last statements are just there to print the read values onto the LCD screen.

Post a Comment

Previous Post Next Post