Pibrella Traffic Lights - WordPress.com · Pibrella Traffic Lights This lesson uses the Pibrella to...

Post on 18-Apr-2020

10 views 0 download

Transcript of Pibrella Traffic Lights - WordPress.com · Pibrella Traffic Lights This lesson uses the Pibrella to...

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 3Open an LX Terminal

To use the Pibrella we will need superuser rightsType in

sudo idle3 @and press Enter

This will open the IDLE 3 editor

Type the following into the editor

import timeimport RPi.GPIO as GPIO

You will need to import the time libraryYou will need to import the Raspberry Pi GPIO library

Type the following into the editor

import timeimport RPi.GPIO as GPIOGPIO.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 BOARDThis 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 13YELLOW 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 timeimport RPi.GPIO as GPIOGPIO.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 timeimport RPi.GPIO as GPIOGPIO.setmode(GPIO.BOARD)#GPIO 13 RED LED#GPIO 11 AMBER LED#GPIO 7 GREEN LEDGPIO.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 timeimport RPi.GPIO as GPIOGPIO.setmode(GPIO.BOARD)#GPIO 13 RED LED#GPIO 11 AMBER LED#GPIO 7 GREEN LEDGPIO.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 timeimport RPi.GPIO as GPIOGPIO.setmode(GPIO.BOARD)#GPIO 13 RED LED#GPIO 11 AMBER LED#GPIO 7 GREEN LEDGPIO.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 timeimport RPi.GPIO as GPIOGPIO.setmode(GPIO.BOARD)#GPIO 13 RED LED#GPIO 11 AMBER LED#GPIO 7 GREEN LEDGPIO.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 timeimport RPi.GPIO as GPIOGPIO.setmode(GPIO.BOARD)#GPIO 13 RED LED#GPIO 11 AMBER LED#GPIO 7 GREEN LEDGPIO.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 timeimport RPi.GPIO as GPIOGPIO.setmode(GPIO.BOARD)#GPIO 13 RED LED#GPIO 11 AMBER LED#GPIO 7 GREEN LEDGPIO.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 timeimport RPi.GPIO as GPIOGPIO.setmode(GPIO.BOARD)RED_LED = 13YELLOW_LED = 11GREEN_LED = 7GPIO.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 readii) If you change the Pibrella for different board

then you only need to change the pin definitions at the beginning of your code