Introduction:

Measuring temperature and humidity are useful functions performed by various sensors. One common sensor is the DHT11 which not only offers accuracy but a wide range too. We can easily interface the DHT11 with our microprocessors/microcontrollers. In this article, we will be teaching you how to interface a DH11 sensor with your Raspberry Pi which will display its readings on a 16×2 LCD screen.

The DHT11 Sensor:

The DHT11 is a basic, low cost, digital humidity, and temperature sensor. It measures temperature from 0-50°C with an accuracy of ±2 °C. It also measures humidity from 20-90% RH with an accuracy of ±5 %. It is available in a module form and a sensor form. We will be using the module form in this tutorial. Both of them differ in this sense that the sensor has a filtering capacitor and a pull-up resistor attached to the output pin of the sensor.

Working of the DH11 Sensor:

The sensor comes with a blue or white casing which has two important components we will use. One is a pair of electrodes; the electric resistance between these two electrodes is controlled by the moisture holding substrate i.e. the measured resistance is inversely proportional to the relative humidity of the environment. Keep in mind this relative humidity not actual humidity so it measured the water content in the air relative to temperature in the air. The second component is a surface mounted NTC (Negative Temperature Coefficient) Thermistor. With the increase in temperature, the value of resistance will decrease.

Pre-Requisites for this Project:

Your Raspberry Pi should be interfaced with an Operating System and should connect to the internet. You should also have access to your pi either through terminal windows or through other application through which you can write and execute python programs and use the terminal window

Installing the Adafruit LCD library on your Raspberry Pi:

Sensor data will be displayed on a 16 x 2 LCD display. We will use Adafruit to easily operate this LCD in 4-bit mode, so we will install it in our Raspberry Pi. For this follow the following steps:

Step 1:

First, use the below-provided line to install it on your Raspberry Pi. It will allow us to clone our project file on Github, so we have to install Git in order to download out the library.

apt-get install git

Step 2:

The provided link is to the Github page where our library is present, execute this line and the project file will be cloned on your Pi home directory.

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

Step 3:

Next, use the provided command to change the directory line and open the project file we just downloaded.

cd Adafruit_Python_CharLCD

Step 4:

You will see a file called setup.py in the directory, we will have to install it to install the library. Use the provided code below to install the library.

sudo python setup.py install

Now your library should be installed successfully.

Installing the Adafruit DHT11 library on Your Raspberry Pi:

DHT11 sensor works by sensing the temperature and humidity and then transmitting the data through the output pin as serial data. We can read this data through the I/O pin on a microprocessor/microcontroller. In order to read the values you would have to go through the datasheet of the DHT11 sensor, but for now, to keep things simple we will use a library to talk with the DHT11 sensor. We will be using the same procedure to install the DHT11 library as we did for the LCD library. We will only change the link of the Github page. Use the following commands one by one 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

Now you should have installed both your libraries on your Raspberry Pi.

Circuit Diagram:

For making building circuit you will make the following connections. Connect the LCD and DHT11 sensor to the 5V pins on the Raspberry Pi. In case you are using a module you do not need to attach a resistor on the output pin of the DHT11 sensor. If not then use a pull-up resistor of 1k. A trimmer potentiometer of 10k is added to the VEE pin of the LCD. Sensor Output Pin is Connected to GPIO17 ( 11 ) of raspberry pi.

Then next connections are pretty basic but take note of which GPIO pins you are using to connect the pins since we will need in our program. Make your connections according to the provided circuit diagram. Since we used a DHT11 module, we wired it directly to our Raspberry Pi.

DHT11 Sensor Output Pin GPIO17 ( 11 ) lcd_rs 7 #RS of LCD is connected to GPIO 7 ( 26 )on PI lcd_en 8 #EN of LCD is connected to GPIO 8 ( 24 )on PI lcd_d4 25 #D4 of LCD is connected to GPIO 25 ( 22 ) on PI lcd_d5 24 #D5 of LCD is connected to GPIO 24 ( 18 ) on PI lcd_d6 23 #D6 of LCD is connected to GPIO 23 ( 16 ) on PI lcd_d7 18 #D7 of LCD is connected to GPIO 18 ( 12 ) on PI lcd_backlight 0 #LED is not connected so we assign to 0

Measuring Humidity and Temperature using Raspberry Pi:

Once you’ve made all the necessary connections and installed the libraries listed, you will launch the python program given at the end of this project.

As soon as you open IDLE, you should get a window with the Python Shell, on the IDE, as illustrated in the below picture.

Your LCD will display an intro message and then give you the current temperature and humidity values. In case of no display, check your python shell window for any errors, your connections and also adjust the potentiometer. Now you have completed this project!

Source Code:

#Program to read the values of Temp and Hum from the DHT11 sensor and display them on the LCD import time #import time for creating delay import Adafruit_CharLCD as LCD #Import LCD library import Adafruit_DHT #Import DHT Library for sensor sensor_name = Adafruit_DHT . DHT11 #we are using the DHT11 sensor sensor_pin = 17 #The sensor is connected to GPIO17 on Pi lcd_rs = 7 #RS of LCD is connected to GPIO 7 on PI lcd_en = 8 #EN of LCD is connected to GPIO 8 on PI lcd_d4 = 25 #D4 of LCD is connected to GPIO 25 on PI lcd_d5 = 24 #D5 of LCD is connected to GPIO 24 on PI lcd_d6 = 23 #D6 of LCD is connected to GPIO 23 on PI lcd_d7 = 18 #D7 of LCD is connected to GPIO 18 on PI lcd_backlight = 0 #LED is not connected so we assign to 0 lcd_columns = 16 #for 16 * 2 LCD lcd_rows = 2 #for 16 * 2 LCD lcd = LCD . Adafruit_CharLCD ( lcd_rs , lcd_en , lcd_d4 , lcd_d5 , lcd_d6 , lcd_d7 , lcd_columns , lcd_rows , lcd_backlight ) #Send all the pin details to library lcd . message ( 'DHT11 with Pi

-CircuitDigest' ) #Give a intro message time . sleep ( 2 ) #wait for 2 secs while 1 : #Infinite Loop humidity , temperature = Adafruit_DHT . read_retry ( sensor_name , sensor_pin ) #read from sensor and save respective values in temperature and humidity varibale lcd . clear ( ) #Clear the LCD screen lcd . message ( 'Temp = %.1f C' % temperature ) # Display the value of temperature lcd . message ( '

Hum = %.1f %%' % humidity ) #Display the value of Humidity time . sleep ( 2 ) #Wait for 2 sec then update the values

Project and image Courtesy © : circuitdigest.com