For a workshop I’m going to give about Raspberry PI and how to interact with the outside world, I thought for a start, why not something simple like some kind of a Weather Station.
Materials
1x Raspberry PI (I’m using b+)
1x LCD 16×2
1x DHT22 (or DHT11 or am2303) sensor
1x 10K potentiometer
1x 10K resistor
1x breadboard
Here’s the schematic in Fritzing
Connections
Sensor | | Arduino PINs |
LCD display | | Board | BCM |
1 | | GND | |
2 | | 5v | |
3 | Potentiometer | | |
4 | | 7 | GPIO27 |
5 | | GND | |
6 | | 9 | GPIO22 |
7 | | | |
8 | | | |
9 | | | |
10 | | | |
11 | | 22 | GPIO25 |
12 | | 18 | GPIO24 |
13 | | 16 | GPIO23 |
14 | | 12 | GPIO18 |
15 | | 5v | |
16 | | 7 | GPIO4 |
DHT22 | | | |
| | 8 | GPIO14 |
| | GND | |
| | 5v | |
After the wiring, time to connect the PI
Note: Using this wiring and the Adafruit’s LCD libraries, the LCD display will not light up when connecting the PI. After testing the example and running the python script, the LCD will light up and start displaying information
Software
DHT
For the sensor to work, we need the DHT libraries developed by Adafruit and setup the environment.
NOTE: As stated by Adafruit, this driver is work in progress
First, set up your development environment
|
sudo apt-get install build-essential python-dev python-openssl
|
Now, clone the Adafruit libraries
|
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
|
After cloned, enter the directory
Install the libraries
|
sudo python setup.py install
|
Everything now should be installed. Let’s try the sensor
Now, execute the test file without arguments to see the options available
|
pi@raspberrypi ~/Adafruit_Python_DHT/examples $ sudo ./AdafruitDHT.py
usage: sudo ./Adafruit_DHT.py [11|22|2302] GPIOpin#
example: sudo ./Adafruit_DHT.py 2302 4 - Read from an AM2302 connected to GPIO #4
|
We’re using DHT22 and connected to the GPIO PIN 4, so, our command line will be:
|
sudo ./AdafruitDHT.py 22 14
|
And the result:
|
Temp=27.0* Humidity=50.8%
|
LCD 16×2
With this method, we’re not going to use i2c or UART, but Adafruit’s libraries for LCD. They are easy to install and easy to work with.
Note: If you just want to connect the LCD, refere to this post.
Install the necessary packages
|
sudo apt-get install python-setuptools
|
Upgrade distribute
|
sudo easy_install -U distribute
|
|
sudo apt-get install python-pip python-smbus
|
Use PIP to install the GPIO packages
|
sudo pip install rpi.gpio
|
Install the necessary software modules
|
git clone https://github.com/adafruit/Adafruit_Python_CharLCD.git
|
|
cd Adafruit_Python_CharLCD
|
|
sudo python setup.py install
|
Try with an example
|
cd examples
python ./char_lcd.py
|
Now, you can see information on the LCD display
If you want to check the API of the LCD Adafruit’s Python Library:
|
python
Python 2.7.3 (default, Mar 18 2014, 05:13:23)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import Adafruit_CharLCD as LCD
>>> help(LCD.Adafruit_CharLCD)
|
The Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
#import libraries
import RPi.GPIO as GPIO
import time
import sys
# DHT sensor
import Adafruit_DHT
# LCD
import Adafruit_CharLCD as LCD
# Define sensor
sensor = Adafruit_DHT.AM2302
pin = 14
# Raspberry PI connections
lcd_rs = 27 # Older versions if not working change to 21
lcd_en = 22
lcd_d4 = 25
lcd_d5 = 24
lcd_d6 = 23
lcd_d7 = 18
lcd_backlight = 4
# Define some device constants
lcd_columns = 16
lcd_rows = 2
# Initialize the LCD
lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7,
lcd_columns, lcd_rows, lcd_backlight)
while True:
try:
# Get values
humidity, temperature = Adafruit_DHT.read_retry(sensor,pin)
temp = "{:0.1f}*C".format(temperature)
hum = "{:0.1f}%".format(humidity)
# clear lcd
lcd.clear()
# display temperature
lcd.message ("Temperatura:\n")
# Mostrar a temperatura quase no final
toMove = lcd_columns - len(temp)
lcd.set_cursor(toMove,1)
lcd.message(temp)
time.sleep(5) # 5 second delay
lcd.clear()
# display humidity
lcd.message ("Humidade:\n")
toMove = lcd_columns - len(hum)
lcd.set_cursor(toMove,1)
lcd.message (hum)
time.sleep(5)
except KeyboardInterrupt:
lcd.clear()
lcd.message("Adeus!")
time.sleep(3)
sys.exit()
|
The final product
Tidak ada komentar:
Posting Komentar