Chapter 6 Physical computing with Scratch and Python 141
THE OFFICIAL RASPBERRY PI BEGINNER’S GUIDE
Scratch project: Traffic Lights
Now you know how to use buttons, buzzers, and LEDs as inputs and outputs, you’re ready to
build an example of real-world computing: traffic lights, complete with a button you can press
to cross the road. For this project, you’ll need a breadboard; a red, a yellow, and a green LED;
three 330 Ω resistors; a buzzer; a push-button switch; and a selection of male-to-male (M2M)
and male-to-female (M2F) jumper wires.
Start by building the circuit (Figure 6-6), connecting the buzzer to the GPIO 15 pin (labelled
GP15 in Figure 6-6), the red LED to the GPIO 25 pin (labelled GP25), the yellow LED to GPIO 8
(GP8), the green LED to GPIO 7 (GP7), and the switch to GPIO 2 (GP2). Remember to connect
the 330 Ω resistors between the GPIO pins and the long legs of the LEDs, and connect the
second legs on all your components to the ground rail of your breadboard. Finally, connect the
ground rail to a ground pin (labelled GND) on the Raspberry Pi to complete the circuit.
3V3
GP2
GP3
GP4
GND
GP17
GP27
GP22
3V3
GP10
GP09
GP11
GND
DNC
GP5
GP6
GP13
GP19
GP26
GND
5V
5V
GND
GP14
GP15
GP18
GND
GP23
GP24
GND
GP25
GP8
GP7
DNC
GND
GP12
GND
GP16
GP20
GP21
5 Figure 6-6: Wiring diagram for the Traffic Lights project
Start a new Scratch 2 project, then drag a when clicked block onto the scripts area.
Next, you’ll need to tell Scratch that the GPIO 2 pin, which is connected to the push-button