• Dhanush kumar S

Interfacing DHT11 sensor with Raspberry pi and LCD

Updated: Nov 28, 2020

As we all know that there are instruments for measuring the temperature and humidity of our environment. They are employed by the meteorological centers in the process of rain prediction. The cost of those devices is a few thousand and we can't afford them easily. But by using the DHT11 sensor we can measure the temperature and humidity level of our room on a small scale.


The values can be acquired by interfacing the DHT11 sensor with the raspberry pi module and LCD display. This interfacing is pretty simple and can be used as a reference for measuring temperature and humidity levels at a large scale in the future. We are going to learn about the entire process involved in this interfacing, starting from the list of required components.


APPARATUS REQUIRED


The following list provides the hardware and software components required to perform this interfacing.


Hardware components:


1.DHT11 sensor:

DHT11 sensor is a low-cost digital sensor used to measure temperature and humidity levels. They can be easily interfaced with microcontroller-based devices such as Arduino board, raspberry pi module. The sensor contains a capacitive humidity sensor for detecting the humidity level. The change in capacitance value occurs with the change in humidity level. The IC process changed value and convert them into digital form.


For measuring the temperature this sensor employs a thermistor that has a negative temperature coefficient. This DHT11 sensor is small-sized and has an operating voltage from 3 to 5 volts.

DHT11 sensor

2.Raspberry Pi 3:

We are using Raspberrypi to connect with the fingerprint module and display the output. It contains several I/O pins using which we can connect the module and then by coding the Raspberrypi, we will receive the commands/signals that are sent from the fingerprint sensor.

Raspberry Pi 3 module

3.Jumper wires:

These are simple connecting wires, that are used to connect the ends without any soldering. The male wires have the ends that can be connected to other ends and the female wires are the ones that have the receiving end. In this interfacing, female to female wires is used.


4.USB Cable:

This cable is used to connect the RaspberryPi to the power supply. It also helps in the transmission of data.


5.LCD Display:

This 16x2 LCD display has 2 rows and 16 columns and a total of 32 characters can be displayed. The operating voltage of this display is fro 4.7V to 5.3 V. The display is available in black, blue backlight and it can also display any custom generated characters.

16x2 LCD Display

6. 12C module for LCD display:

This 12C module has an inbuilt PCF8574 I2C chip that converts the serial data to the parallel data for the LCD display. This module has a contrast adjustment pot beneath the display. This helps to display the text correctly. The potentiometer toggles the brightness level of the LCD and the LED controls the backlight of the display.

12C module

Software components:


7.SSH Terminal:

The SHH Terminal is used in this project to connect the Raspberry Pi module to the terminal of another computer.


8.Python IDE:

Python Programming is done in an IDE (Integrated Development Environment) is a platform where we can write the code, run, and test the working.

Circuit Diagram:

Circuit diagram

The DHT11 sensor if first interfaced with the raspberry pi module. Then the 16x2 LCD display is connected to the circuit so as to view the results.


What goes where?

  • The yellow wire connects the signal pin of the DHT11 sensor to the pin GPIO 4 of the raspberry pi 3 board.

  • The red wire connects the voltage pin(power supply) of the sensor to the 5V pin of the raspberry pi board.

  • The black wire connects the GND (ground) pin of the sensor to the GND pin of the raspberry pi device.

By performing these connections, the DHT11 sensor can be interfaced with the raspberry pi module. Now, this circuit has to be interfaced with the 16x2 LCD display.

  • The black wire connects the GND (ground) pin of the LCD display to the GND pin of the raspberry pi device.

  • The red wire connects the voltage pin(power supply) of the LCD display to the 5V pin of the raspberry pi board.

  • The blue wire connects the SDA pin of the LCD display to the SDA pin of the raspberry pi device.

  • The orange wire connects the SCL pin of the LCD display to the SCL pin of the raspberry pi board.

After performing the above connections, the power supply is given. The raspberry pi device gets boot up.

Hardware powered up

Software interfacing


Few more steps are needed to be performed before acquiring the temperature and humidity level values. For newer versions of raspberry pi, to enable the one-wire interface, certain code has to be executed.


The following steps have to be followed to install the Adafruit library for interfacing the DHT11 sensor with the raspberry pi device.

Commands for configuring DHT11 with RPi
• 770B

Step-1: Install the Adafruit library for DHT11/DHT22 by running the command given below on the RPi terminal

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

Step-2: Run through these commands given below one by one to successfully install the library on you RPi

	cd Adafruit_Python_DHT
	sudo apt-get install build-essential python-dev
	sudo python setup.py install

Step-3 (Optional, for newer versions of RPi): If the device you are using returns "None" values, then use the commands given below

	(i) Run "sudo raspi-config" in the terminal
	(ii) Select "Interfacing Options" from the list that appears on the screen
	(iii) Select the "One-wire Interface" option from the list, enable it, and reboot
	(iv) Run the program to check the output

While performing the experiments with newer versions of the raspberry pi the step-3 has to be performed. After performing, the device needs to be restarted. When these processes have completed the main code should be executed.

Commands for configuring DHT11 with RPi
• 770B



Code


1. Main code file

First import the sys module, for using the system-specific parameters in ou program. Then I2C_LCD_driver which is the support file for configuring LCD with the raspberry pi board. The Adafruit_DHT module is imported to interface the DHT11 sensor to the raspberry pi. Then the time module is imported for including delay in our program.


After importing the necessary libraries, a variable named mylcd is created to call all the functions associated with I2C_LCD_driver. The first command mylcd.lcd_clear clears the LCD screen. Then LearnElectronics DHT11 with RPi string will be displayed in the allocated row and column.


Then the Try block is followed by the infinite while loop. Inside the while loop, the read function acquires the temperature and humidity values 15 times from the sensor. Sometimes the values cannot be read by the pins of raspberry pi or it may read some none value. In such cases, the value has to be cleared from the LCD screen and again the values have to be read. This condition is given under the if loop because this cannot happen every time.


The time.sleep(1) condition makes the device remain inactive for 1 second and again it can extract the value from the sensor. Whenever a keyboard interrupt is given, the LCD will be cleared, and then the string Thank for watching will be displayed in the mentioned row and column. The string will be displayed for about 5 seconds then the LCD screen will be cleared.


import sys
import I2C_LCD_driver
import Adafruit_DHT
import time

mylcd = I2C_LCD_driver.lcd()

mylcd.lcd_clear()
mylcd.lcd_display_string('LearnElectronics',1)
mylcd.lcd_display_string('DHT11 with RPi',2,1)

try:
    while True:

        humidity, temperature = Adafruit_DHT.read_retry(11, 4)

        if(temperature != None and humidity != None):
            mylcd.lcd_clear()
            mylcd.lcd_display_string('Temp:{0:0.1f} C '.format(temperature),1,2)
            mylcd.lcd_display_string('Humidity:{0:0.1f} %'.format(humidity),2,1)
        time.sleep(1)

except KeyboardInterrupt:
    mylcd.lcd_clear()
    mylcd.lcd_display_string('Thanks',1,5)
    mylcd.lcd_display_string('For Watching',2,2)
    time.sleep(5)
    mylcd.lcd_clear()

Download the code here.

dht_lcd (Main)
.txt
TXT • 770B




Note: The files above are in .txt format but should be converted into python file format (.py format) before uploading the code to Raspberry Pi.


Output


When the circuit is powered up, before displaying the temperature and humidity values, the following string is displayed.


Start text output

Then after a delay, the first reading measured by the sensor will be displayed in the LCD display.

output-1

After sometime, again a set of values will be displayed depending on the surrounding environment.

output-2

If we place any warm object near the sensor, the value will be changed accordingly.

output-3

In case of giving any keyboard interrupt, the following string will be displayed.

End text output

Video Explanation


To see the practical implementation of the above-explained tutorial, watch the video below:

Learn Electronics. (Aug 16, 2020). "Interfacing Temperature Sensor - DHT11 with Raspberry Pi and LCD Display". https://www.youtube.com/watch?v=t6hWaDLS9DA


NOTE: By keeping this DHT11 sensor tutorial as basic, we can develop several advanced projects. Some of the applications include IoT weather stations, condition monitoring.


See also:

  1. Voltage divider

  2. Shift registers

  3. Zigbee Technology

#raspberrypi

#electronics

#technology

#programming

#iot

#engineering

420 views