Pibrella Traffic Lights

advertisement
Pibrella Traffic Lights
This lesson uses the Pibrella to make a set of traffic
lights but uses the GPIO pin-outs rather than the
Pibrella library.
This will help you if you wish to use a Pibrella with
Minecraft or if you want to use the GPIO without the
Pibrella
Idle 3
Open an LX Terminal
To use the Pibrella we will need superuser rights
Type in
sudo idle3 @
and press Enter
This will open the IDLE 3 editor
Type the following into the editor
import time
import RPi.GPIO as GPIO
You will need to import the time library
You will need to import the Raspberry Pi GPIO library
Type the following into the editor
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
There are different ways of identifying the
numbers on the GPIO pins of the Raspberry Pi
We will use a mode called BOARD
This uses a numbering system which counts
down the pins down and across from 1-40
You will need to know what GPIO pins the lights
attached to
RED LED GPIO PIN 13
YELLOW LED GPIO PIN 11
GREEN LED GPIO 7
You can type this into the program if you wish but
prevent it from running by using #
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
#GPIO 13 RED LED
#GPIO 11 AMBER LED
#GPIO 7 GREEN LED
You now need to set these pins as outputs
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
#GPIO 13 RED LED
#GPIO 11 AMBER LED
#GPIO 7 GREEN LED
GPIO.setup (13, GPIO.OUT)
GPIO.setup (11, GPIO.OUT)
GPIO.setup (7, GPIO.OUT)
You now need to enter a loop and set the Red LED as on (or
HIGH) and the Yellow and Green LEDs as off (LOW)
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
#GPIO 13 RED LED
#GPIO 11 AMBER LED
#GPIO 7 GREEN LED
GPIO.setup (13, GPIO.OUT)
GPIO.setup (11, GPIO.OUT)
GPIO.setup (7, GPIO.OUT)
while True:
GPIO.output (13, GPIO.HIGH)
GPIO.output (11, GPIO.LOW)
GPIO.output (7, GPIO.LOW)
time.sleep(2)
Continue the program to add in the next sequence of
lights
Set the Red and Yellow LEDs as on (or HIGH) and the
Green LED as off (LOW)
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
#GPIO 13 RED LED
#GPIO 11 AMBER LED
#GPIO 7 GREEN LED
GPIO.setup (13, GPIO.OUT)
GPIO.setup (11, GPIO.OUT)
GPIO.setup (7, GPIO.OUT)
while True:
GPIO.output (13, GPIO.HIGH)
GPIO.output (11, GPIO.LOW)
GPIO.output (7, GPIO.LOW)
time.sleep(2)
GPIO.output (13, GPIO.HIGH)
GPIO.output (11, GPIO.HIGH)
GPIO.output (7, GPIO.LOW)
time.sleep(2)
Continue the program to add in the next sequence of
lights
Set the Red and Yellow LEDs as off (or LOW) and the
Green LED as on (HIGH)
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
#GPIO 13 RED LED
#GPIO 11 AMBER LED
#GPIO 7 GREEN LED
GPIO.setup (13, GPIO.OUT)
GPIO.setup (11, GPIO.OUT)
GPIO.setup (7, GPIO.OUT)
while True:
GPIO.output (13, GPIO.HIGH)
GPIO.output (11, GPIO.LOW)
GPIO.output (7, GPIO.LOW)
time.sleep(2)
GPIO.output (13, GPIO.HIGH)
GPIO.output (11, GPIO.HIGH)
GPIO.output (7, GPIO.LOW)
time.sleep(2)
GPIO.output (13, GPIO.LOW)
GPIO.output (11, GPIO.LOW)
GPIO.output (7, GPIO.HIGH)
time.sleep(2)
Continue the program to add in the next sequence of
lights
Did you get it correct?
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
#GPIO 13 RED LED
#GPIO 11 AMBER LED
#GPIO 7 GREEN LED
GPIO.setup (13, GPIO.OUT)
GPIO.setup (11, GPIO.OUT)
GPIO.setup (7, GPIO.OUT)
while True:
GPIO.output (13, GPIO.HIGH)
GPIO.output (11, GPIO.LOW)
GPIO.output (7, GPIO.LOW)
time.sleep(2)
GPIO.output (13, GPIO.HIGH)
GPIO.output (11, GPIO.HIGH)
GPIO.output (7, GPIO.LOW)
time.sleep(2)
GPIO.output (13, GPIO.LOW)
GPIO.output (11, GPIO.LOW)
GPIO.output (7, GPIO.HIGH)
time.sleep(2) GPIO.output (13, GPIO.LOW)
GPIO.output (11, GPIO.HIGH)
GPIO.output (7, GPIO.LOW)
time.sleep(2)
Can you make your code shorter?
Did you get it correct?
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
#GPIO 13 RED LED
#GPIO 11 AMBER LED
#GPIO 7 GREEN LED
GPIO.setup (13, GPIO.OUT)
GPIO.setup (11, GPIO.OUT)
GPIO.setup (7, GPIO.OUT)
while True:
GPIO.output (13, GPIO.HIGH)
GPIO.output (11, GPIO.LOW)
GPIO.output (7, GPIO.LOW)
time.sleep(2)
GPIO.output (11, GPIO.HIGH)
time.sleep(2)
GPIO.output (13, GPIO.LOW)
GPIO.output (11, GPIO.LOW)
GPIO.output (7, GPIO.HIGH)
time.sleep(2)
GPIO.output (11, GPIO.HIGH)
GPIO.output (7, GPIO.LOW)
time.sleep(2)
Some lights do not need turning on and off (because
they are already on or off)
You can improve your code by defining the LED pins
as constants
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
RED_LED = 13
YELLOW_LED = 11
GREEN_LED = 7
GPIO.setup (RED_LED, GPIO.OUT)
GPIO.setup (YELLOW_LED, GPIO.OUT)
GPIO.setup (GREEN_LED, GPIO.OUT)
while True:
GPIO.output (RED_LED, True)
GPIO.output (YELLOW_LED, False)
GPIO.output (GREEN_LED, False)
time.sleep(2)
GPIO.output (YELLOW_LED, True)
time.sleep(2)
GPIO.output (RED_LED, False)
GPIO.output (YELLOW_LED, False)
GPIO.output (GREEN_LED, True )
time.sleep(2)
GPIO.output (YELLOW_LED, True )
GPIO.output (GREEN_LED, False)
time.sleep(2)
The advantage of defining the LED GPIO pins is
two fold
i) The code is easier to read
ii) If you change the Pibrella for different board
then you only need to change the pin
definitions at the beginning of your code
Download