Notes.

Raspberry Pi Workshop

I recently ran a workshop on Raspberry Pis at the Australian National University as part of an Engineering course.

We covered setting up Pis with Raspbian, poking around on their desktop environment and interfacing with hardware. Due to the nature of the course, there’s a strong focus on applications.

The materials are archived here for posterity.

Materials

The presentation itself is available as a PDF and as the LaTeX sources.

The hardware demo board:

circuit diagram for Raspberry Pi demo

Notes:

  • Pin 6 and 25 are both ground pins.
  • Pin 5 has a built-in pull-up, hence the absence of a pull-up resistor.
  • The design assumes that only one LED per set (red/green, red/orange/green) will be on at once. A more flexible/robust design would give each LED its own current-limiting resistor.

We covered two code samples, which are included below. My apologies for the somewhat spartan nature of the code, and lack of comments.

In lieu of comments, some notes:

  • Because the switch is on a pull-up, it’s normally 1, and goes to 0 when pressed.
  • Nothing is cleaned up properly, so you’ll get warnings the second time you run any of the samples.

The first sample is a simple chaser:

chaser.pyDownload
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
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
switch_pin = 5
traffic_red_pin = 26
traffic_yel_pin = 24
traffic_grn_pin = 23
ped_red_pin = 22
ped_grn_pin = 21
GPIO.setup(switch_pin, GPIO.IN)
pins = [traffic_red_pin, traffic_yel_pin, traffic_grn_pin, ped_red_pin, ped_grn_pin]
[GPIO.setup(pin, GPIO.OUT) for pin in pins]
pin = 0
forward = False
while True:
    GPIO.output(pins[pin], GPIO.LOW)

    if not GPIO.input(switch_pin):
        forward = not forward

    if forward:
        pin = pin + 1
        if pin >= len(pins):
            pin = 0
    else:
        pin = pin - 1
        if pin < 0:
            pin = len(pins)-1

    GPIO.output(pins[pin], GPIO.HIGH)
    time.sleep(1)

And one which simulates a pedestrian crossing:

traffic.pyDownload
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
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
switch_pin = 5
traffic_red_pin = 26
traffic_yel_pin = 24
traffic_grn_pin = 23
ped_red_pin = 22
ped_grn_pin = 21
GPIO.setup(switch_pin, GPIO.IN)
pins = [traffic_red_pin, traffic_yel_pin, traffic_grn_pin, ped_red_pin, ped_grn_pin]
[GPIO.setup(pin, GPIO.OUT) for pin in pins]
[GPIO.output(pin, GPIO.LOW) for pin in pins]
mode = 0
forward = False

while True:
    GPIO.output(traffic_grn_pin, GPIO.HIGH)
    GPIO.output(ped_red_pin, GPIO.HIGH)


    while GPIO.input(switch_pin):
        continue

    time.sleep(2)
    GPIO.output(traffic_yel_pin, GPIO.HIGH)
    GPIO.output(traffic_grn_pin, GPIO.LOW)
    time.sleep(2)
    GPIO.output(traffic_yel_pin, GPIO.LOW)
    GPIO.output(traffic_red_pin, GPIO.HIGH)
    time.sleep(1)
    GPIO.output(ped_red_pin, GPIO.LOW)
    GPIO.output(ped_grn_pin, GPIO.HIGH)
    time.sleep(3)
    GPIO.output(ped_grn_pin, GPIO.LOW)
    for i in range(10):
        GPIO.output(ped_red_pin, GPIO.HIGH)
        time.sleep(0.25)
        GPIO.output(ped_red_pin, GPIO.LOW)
        time.sleep(0.25)
    GPIO.output(ped_red_pin, GPIO.HIGH)
    time.sleep(1)
    GPIO.output(traffic_red_pin, GPIO.LOW)
    GPIO.output(traffic_grn_pin, GPIO.HIGH)

I’m happy to answer questions and help out with Raspberry Pi projects: my contact details are in the presentation.