Simple Weather Station using Raspberry pi
Component:
- Raspberry pi
- LCD 16*2
- DHT Sensor
- Jumper wires
- Bread board
Installing the Adafruit LCD library on Raspberry Pi:
The value of the temperature and humidity will be displayed on a 16*2 LCD display. Adafruit provides us a library to easily operate this LCD in 4-bit mode, so let us add it to our Raspberry Pi by opening the terminal window Pi and following the below steps.
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
That is it the library should have been installed successfully. Now similarly let’s proceed with installing the DHT library which is also from Adafruit.
Code:
#Program to read the values of Temp and Humidity from the DHT11 sensor and display them on the LCD
#Website: www.iotianhub.com
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 \n -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 ('\nHum = %.1f %%' % humidity) #Display the value of Humidity
time.sleep(2) #Wait for 2 sec then update the values