Introduction: How to Read DHT Data on LCD Using Raspberry Pi

The temperature and the relative humidity are important

weather data in the environments. The two can be the data a mini weather station delivers. Reading your temperature and Relative humidity with Raspberry Pi can be achieved using different variety of modules and add-ons. In this tutorial, we’ll be using a common Sensor DHT11 to read the temperature and will be displaying the data on a 16-bits LCD display.

Step 1: DHT Sensor

The DHT11 sensor can measure relative humidity and temperature with the following specifications

Temperature Range: 0-50°C

Temperature Accuracy: ±2 °C

Humidity Range: 20-90% RH

Humidity Accuracy: ±5 %

Step 2: Installing the Adafruit LCD Library on Raspberry Pi:

With the shell of your raspberry pi open, follow the instructions below to install the Adafruit LCD display library in the raspberry pi. The value of the temperature and humidity will be displayed on a LCD display

Step 1: Install git on your Raspberry Pi by using the below line. Git allows you to clone any project files on Github and use it on your Raspberry pi. Our library is on Github so we have to install git to download that library into pi.

apt-get install git

Step 2: The following line links to the GitHub page where the library is present just execute the line to clone the project file on Pi home directory

git clone git://github.com/adafruit/Adafruit_Python_CharLCD

Step 3: Use the below command to change directory line, to get into the project file that we just downloaded. The command line is given below

cd Adafruit_Python_CharLCD

Step 4: Inside the directory there will be a file called setup.py, we have to install it, to install the library. Use the following code to install the library

sudo python setup.py install

Step 3: Installing the Adafruit DHT11 Library on Raspberry Pi:

The DHT11 library provided by Adafruit can be used for DHT11, DHT22 and other one wire temperature sensors as well. The procedure to install the DHT11 library is also similar to the one followed for installing LCD library. The only line that would change is the link of the GitHub page on which the DHT library is saved.

Enter the four command lines one by one on the terminal to install the DHT library

git clone https://github.com/adafruit/Adafruit_Python_DHT.git

cd Adafruit_Python_DHT

sudo apt-get install build-essential python-dev

sudo python setup.py install

Step 4: Circuit Diagram

The DHT11 Module comes in 3 pins, Connect the Vcc to 5V on the pi, connect the ground pin to any ground pin on the pi and connect the data pin to your choice GPIO pin on the pi, in this tutorial we’re using GPIO 17 which is pin number 11 on the pi.

NOTE: The DHT11 comes in Module or sensor type, the one shown in the schematic below is the sensor type that has 4 pins, a resistor is connected between the data pin and the Vcc, if you’re using the module type with only 3 pins, there’s no need for the resistor.

Reference the diagram below for the pinout of the raspberry pi pins.

Step 5:

Below is the full schematic for the connection. Since the LCD will be using the two 5V available on the pi, we can use a breadboard to share the 5V between the LCD and the DHT11 Module. The LCD pins will be connected to the pi in the following order. Note that pin 7,8,9 and 10 of the LCD will not be used

Step 6:

The full code for reading the Data and displaying it on the LCD is shown below

from time import sleep
import Adafruit_DHT
from Adafruit_CharLCD import Adafruit_CharLCD

sensor = Adafruit_DHT.DHT11
pin = 17
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
lcd = Adafruit_CharLCD(rs=26, en=19, d4=13, d5=6, d6=5, d7=11, cols=16, lines=2)

#DISPLAY A STATIC TEXT

lcd.clear()
if humidity is not None and temperature is not None:
print('Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity))
lcd.message('Temp={0:0.1f}*C \nHumidity={1:0.1f}%'.format(temperature, humidity))
else:
print('Failed to get reading. Try again!')
lcd.message('Failed to get reading. Try again!')