Raspberry Jam Worksheets PDF
Raspberry Jam Worksheets PDF
activities
Here are some short activities which are ideal for beginners at your Raspberry Jam event. The
worksheets are included here, and you’ll also find them at rpf.io/jamws where you can browse
them online or print the PDFs. The online versions are available in multiple (spoken) languages.
Worksheets
Introduction
Traffic lights
Learn to control LEDs and code a traffic lights sequence using Scratch, EduBlocks and Python.
You will need:
This project is provided free by the Raspberry Pi Foundation under a Creative Commons licence.
See more at projects.raspberrypi.org and github.com/raspberrypilearning
Minecraft TNT
Navigate the world of Minecraft Pi, learn to create with code and blow things up with TNT.
You will need:
Sense HAT
(or use the wget rpf.io/shcheer -O cheerlights.py
emulator) wget rpf.io/shcheerauth -O auth.py
This project is provided free by the Raspberry Pi Foundation under a Creative Commons licence.
See more at projects.raspberrypi.org and github.com/raspberrypilearning
Traffic lights
with Scratch 1.4
Introduction
Connect the LEDs
LED GPIO
Red 22
Amber 27
GP26 GP20
GP19 GP16
GP13 GND
GP11 GP8
GP09 GP25
GP10 GND
GP22 GP23
GP27 GND
GP17 GP18
Green 17
GND GP21
GP6 GP12
GP5 GND
DNC
GND
3V3 GP24
GND GP15
GP4 GP14
GP3
GP2
3V3
DNC
GP7
GND
5V
5V
Introduction
Control the LEDs
1 Open Scratch from the Programming menu (Scratch, not Scratch 2).
1
2 Click Edit in the menu bar and select 1
3 Click the Control panel. Drag in a when
Start GPIO server: flag clicked block and two broadcast blocks.
Dock them together in sequence and edit
the broadcasts to say config22out and
gpio22on like so:
1
4 Now click the green flag to run your code. You should see the red LED light up.
1
5 Now add a wait 1 secs block before and
after turning the LED off with
broadcast gpio22off, and wrap it in a
forever block to blink continuously:
18
This project is provided free by the Raspberry Pi Foundation under a Creative Commons licence.
See more at projects.raspberrypi.org and github.com/raspberrypilearning
1
6 Click the green flag again and you should see the LED blink.
1
7 Now add some more broadcast blocks to introduce the other 17
two lights, and make them all flash on and off:
1
8 Click the green flag again and you should
see the three lights flash together.
1
9 Can you change the number in wait 1 secs to
speed up or slow down the sequence?
Introduction
Traffic lights sequence
1
2 Now you know how to control the lights individually, and
time the pauses between commands, can you create a
traffic light sequence? The sequence goes:
Green on
Amber on
Red on
Green on
This project is provided free by the Raspberry Pi Foundation under a Creative Commons licence.
See more at projects.raspberrypi.org and github.com/raspberrypilearning
Traffic lights
with Scratch 2
Introduction
Connect the LEDs
LED GPIO
Red 22
Amber 27
Green 17
Introduction
Control the LEDs
1
2 Open the More Blocks panel,
click Add an Extension, and
select Pi GPIO.
1
3 You should then see two new
blocks appear in More Blocks:
1
4 Open the Events panel and drag in a when flag clicked block.
when clicked
1
5 Open the More Blocks panel, drag in a set gpio to output high block and dock it under the
previous block.
This project is provided free by the Raspberry Pi Foundation under a Creative Commons licence.
See more at projects.raspberrypi.org and github.com/raspberrypilearning
1
6 Now click the green flag to run your code. You should see the red LED light up.
1
7 Now add a wait 1 secs block before and after turning the 17
LED off with set gpio 22 to output low, and wrap it in a when clicked
1
8 Click the green flag again and you should see the LED blink. set gpio 22 to output high
wait 1 secs
19 Now add some more set gpio blocks to introduce the other
set gpio 22 to output low
two lights on gpio 27 & 17, and make them all flash on and off:
wait 1 secs
10
1 Click the green flag again and you should see the three lights
flash together.
1
8
11
1 Can you change the number in wait 1 secs to speed up or when clicked 19
slow down the sequence?
forever
wait 1 secs
wait 1 secs
Introduction
Traffic lights sequence
when clicked 1
forever
1 Try turning the lights on and off in sequence:
set gpio 22 to output high
1
2 Now you know how to control the lights individually, and wait 1 secs
time the pauses between commands, can you create a set gpio 27 to output high
traffic lights sequence? The sequence goes:
wait 1 secs
Green on set gpio 17 to output high
wait 1 secs
Amber on
set gpio 22 to output low
Red on
wait 1 secs
wait 1 secs
Green on
set gpio 17 to output low
It’s important to think about timing. How long should the lights wait 1 secs
stay on for at each stage?
Once you have completed the traffic light sequence, you might
want to try adding in a button and a buzzer to make an interactive traffic light for a pedestrian crossing.
18
This project is provided free by the Raspberry Pi Foundation under a Creative Commons licence.
See more at projects.raspberrypi.org and github.com/raspberrypilearning
Traffic lights
with EduBlocks
Introduction
Connect the LEDs
LED GPIO
Red 22
Amber 27
Green 17
GP26 GP20
GP19 GP16
GP13 GND
GP11 GP8
GP09 GP25
GP10 GND
GP22 GP23
GP27 GND
GP17 GP18
GND GP21
GP6 GP12
GP5 GND
DNC
GND
3V3 GP24
GND GP15
GP4 GP14
GP3
GP2
3V3
DNC
GP7
GND
5V
5V
Introduction
Control the LEDs
1
2 Click the gpiozero drop-down,
click General and drag the
from gpiozero import *
block into the workspace.
1
3 Click the Outputs drop-down under gpiozero and click LED. Drag an led = LED(pin) block into the
workspace beneath the import block. Rename the variable from led to red, and change pin to 22.
1
4 Drag in an led.on block, and dock it
beneath the previous block. Change
the on drop-down to blink.
Your code blocks should now look like this:
1
5 Now click the Run button to run your code. You should see the red LED blink.
1
6 Now add some more LED blocks to introduce the other two lights, and make them blink at different speeds.
1
7 Run your code again, and you should see the three lights flashing at different rates.
1
8 If a larger number makes a light blink slower, what number would make it run faster? Try to
make your lights blink faster.
This project is provided free by the Raspberry Pi Foundation under a Creative Commons licence.
See more at projects.raspberrypi.org and github.com/raspberrypilearning
Introduction
Traffic lights sequence
1 The on function 1
2 Try turning the
allows you to turn lights on and off
a light on. You in sequence:
can use sleep to
pause between
commands. Bring
in the import time
block from the Basic
section. Try this
example to turn the
lights on in sequence:
1
3 Try repeating this by putting
the code inside a while loop:
1
14 Now you know how to control the lights individually, and time the pauses between commands,
can you create a traffic light sequence? The sequence goes:
Green on
Amber on
Red on
Green on
It's important to think about timing. How long should the lights stay on for at each stage?
Once you have completed the traffic light sequence, you might want to try adding in a button and a
buzzer to make an interactive traffic light for a pedestrian crossing.
This project is provided free by the Raspberry Pi Foundation under a Creative Commons licence.
See more at projects.raspberrypi.org and github.com/raspberrypilearning
Traffic lights
with Python
Introduction
Connect the LEDs
LED GPIO
Red 22
Amber 27
Green 17
GP26 GP20
GP19 GP16
GP13 GND
GP11 GP8
GP09 GP25
GP10 GND
GP22 GP23
GP27 GND
GP17 GP18
GND GP21
GP6 GP12
GP5 GND
DNC
GND
3V3 GP24
GND GP15
GP4 GP14
GP3
GP2
3V3
DNC
GP7
GND
5V
5V
Introduction
Control the LEDs
1
2 Enter the following code:
red = LED(22)
red.blink()
1
3 Now save your program and press F5 to run your code. You should see the red light flash on and
off continuously.
1
4 Now modify your code to introduce the other two lights, and make them blink at different speeds:
from gpiozero import LED Run your code again and you should see the
15
three lights flashing at different rates.
red = LED(22)
amber = LED(27)
16 If a larger number makes a light blink slower,
green = LED(17) what number would make it run faster?
Try to make your lights blink faster.
red.blink(1, 1)
amber.blink(2, 2)
green.blink(3, 3)
This project is provided free by the Raspberry Pi Foundation under a Creative Commons licence.
See more at projects.raspberrypi.org and github.com/raspberrypilearning
Traffic lights sequence
Introduction
1 The on function allows you to turn a light on. You can use sleep to pause between commands.
Try this example to turn the lights on in sequence:
red = LED(22)
amber = LED(27)
green = LED(17)
red.on()
sleep(1)
amber.on()
sleep(1)
green.on()
sleep(1)
The main controls for LEDs are on, off, toggle and blink.
1
2 Try turning the lights on and off 1
3 Try repeating this by putting the
in sequence: code inside a while loop:
1
4 Now you know how to control the lights individually, and time the pauses between commands,
can you create a traffic lights sequence? The sequence goes:
Green on
Amber on
Red on
Green on
It's important to think about timing. How long should the lights stay on for at each stage?
Once you have completed the traffic lights sequence, you might want to try adding in a button
and a buzzer to make an interactive version.
This project is provided free by the Raspberry Pi Foundation under a Creative Commons licence.
See more at projects.raspberrypi.org and github.com/raspberrypilearning
Traffic Lights
Controller GUI
LED GPIO
Red 22
Amber 27
Green 17
1
2 Open Mu from the main menu and
click the REPL icon.
1
3 Enter the following commands, one by one, into the REPL, and observe the LEDs:
4
1 Now blink the LEDs at different speeds — the two numbers in the () are on time and
off time, in seconds:
lights.blink(2, 2)
lights.blink(5, 5)
lights.blink(0.1, 0.1)
1
5 Try blinking each LED at a different rate:
lights.red.blink(1, 1)
lights.amber.blink(2, 2)
lights.green.blink(3, 3)
This project is provided free by the Raspberry Pi Foundation under a Creative Commons licence.
See more at projects.raspberrypi.org and github.com/raspberrypilearning
Close the REPL and use the main Mu window to type your program
Create a GUI into a file.
app = App()
app.display()
1
2 Add a text label and a second button to turn the red LED off:
Text(app, "Red")
PushButton(app, command=lights.red.on, text="on")
PushButton(app, command=lights.red.off, text="off")
1
3 Now give your GUI app a name, and use the grid layout:
Challenges
14 Write your own function to make the LEDs do the traffic lights sequence.
Include from time import sleep in your program, use def sequence(), and
Hint
set the command to sequence.
This project is provided free by the Raspberry Pi Foundation under a Creative Commons licence.
See more at projects.raspberrypi.org and github.com/raspberrypilearning
Interactive traffic
lights with Python
Introduction
Connect the LEDs
LED GPIO
Button 21
Red LED 25
Amber LED 28
Green LED 27
Buzzer 15
and button
1
2 Enter the following code:
red = LED(25)
button = Button(21)
while True:
if button.is_pressed:
red.on()
else:
red.off()
1
3 Save your code and run it with F5
This project is provided free by the Raspberry Pi Foundation under a Creative Commons licence.
See more at projects.raspberrypi.org and github.com/raspberrypilearning
1
4 Remove the while loop and add two more LEDs
red = LED(25)
amber = LED(28)
green = LED(27)
button = Button(21)
1
5 Get them to come on when the
button is pressed: while True:
if button.is_pressed:
green.on()
6 Run the code and press the amber.on()
button. red.on()
else:
green.off()
amber.off()
red.off()
Traffic lights
Introduction
You can use the built-in TrafficLights class instead of three individual LEDs.
1 Amend the from gpiozero import... line to replace LED with TrafficLights:
button = Button(21)
lights = TrafficLights(25, 28, 27)
while True:
button.wait_for_press()
lights.on()
button.wait_for_release()
lights.off()
This project is provided free by the Raspberry Pi Foundation under a Creative Commons licence.
See more at projects.raspberrypi.org and github.com/raspberrypilearning
21 Try changing the lights to blink:
while True:
lights.blink()
button.wait_for_press()
lights.off()
button.wait_for_release()
As well as controlling the whole set of lights together, you can also control each LED individually.
With traffic light LEDs, a button, and a buzzer, you can create your own traffic lights sequence,
complete with pedestrian crossing!
Green on
Amber on
Red on
Green on
Be sure to turn the correct lights on and off at the right time, and make sure you use
sleep to time the sequence perfectly.
This project is provided free by the Raspberry Pi Foundation under a Creative Commons licence.
See more at projects.raspberrypi.org and github.com/raspberrypilearning
14 Try adding the button for a pedestrian crossing. The button should move the lights to red
(not immediately), and give the pedestrians time to cross before moving the lights back
to green until the button is pressed again.
1
5 Now try adding a buzzer to beep quickly to indicate
that it is safe to cross, for the benefit of visually buzzer = Buzzer(15)
impaired pedestrians:
buzzer.on()
buzzer.off()
buzzer.beep(0.1, 0.1)
16 Your final interactive traffic lights code should start on a green light and then:
Repeat
RASPBERRY
This project is provided free by the Raspberry Pi Foundation under a Creative Commons licence.
See more at projects.raspberrypi.org and github.com/raspberrypilearning
Minecraft
TNT
Introduction
Enter the Minecraft world
1 Open Minecraft Pi from the main menu. Start a game and create a new world.
1
2 Walk around using the WSAD keys on the keyboard. Use Space to jump, and double tap Space to fly.
A S D SPACE SPACE x2
1
3 Press Tab on the keyboard to release your mouse cursor, and open Mu from the main menu.
1
4 Move your windows around so Minecraft and Mu are side by side.
Introduction
Controlling Minecraft with Python
mc = Minecraft.create()
mc.postToChat("Hello world")
1
2 Run the code with F5 and you should see the message "Hello world" appear in the Minecraft window.
1
3 Add the following lines to your code:
x, y, z = mc.player.getPos()
mc.setBlock(x+1, y, z, 1)
This project is provided free by the Raspberry Pi Foundation under a Creative Commons licence.
See more at projects.raspberrypi.org and github.com/raspberrypilearning
1
4 Run the code and you should see a block of stone appear near your player. If it's not in front of you,
try looking around.
1
5 Change the value in the setBlock line from 1 to 2:
mc.setBlock(x+1, y, z, 2)
1
6 You should now see a block of grass appear. Try changing the number again and see which block
gets placed.
1
7 Try changing setBlock to setBlocks to build a 10x10x10 cube rather than a single block:
mc.setBlock(x+1, y, z, 2)
Introduction
TNT
The block ID for TNT is 46. There are two types of TNT: unexplosive TNT and explosive TNT. You want
explosive TNT.
1 Build a solid cube of TNT. To get explosive TNT, you need to add a 1 to the end of your setBlocks line:
1
2 Go up to the cube of TNT and right-click to hit it with your sword. This will activate the TNT.
Stand back and watch the show!
RASPBERRY
This project is provided free by the Raspberry Pi Foundation under a Creative Commons licence.
See more at projects.raspberrypi.org and github.com/raspberrypilearning
Sense HAT
random sparkles
For this activity, you can either use the physical Sense HAT hardware, the desktop emulator in Raspbian,
or the web-based emulator on trinket.io
If you're using the Sense HAT, attach it to your Raspberry Pi before booting.
If you're using the Trinket emulator, open a web browser and go to trinket.io/sense-hat
Using set_pixel
Introduction
First, we'll think up some random numbers and use the set_pixel function to place a
random colour on a random location on the Sense HAT display.
1 If you're using a Raspberry Pi, open Mu. If you're using the web emulator, delete the example code before
you begin.
1
2 In the new file, start by importing the Sense HAT module.
If you're using a physical Sense HAT or the If you're using the desktop emulator, the
Trinket emulator, the import line is: import line is:
1
3 Next, create a connection to your 1
4 Now think of a random number between 0 and
Sense HAT by adding: 7 and assign it to the variable x, for example:
sense = SenseHat() x = 4
1
5 Think of another random number between 1
6 Think of three random numbers between 0
0 and 7, then assign it to y: and 255, then assign them to r, g, and b:
y = 5 r = 19
g = 180
b = 230
1
7 Now use the set_pixel function to place your random colour at your random location on
the display:
sense.set_pixel(x, y, r, g, b)
1
8 Now press the Run button to run your code. You should see a single pixel light up.
This project is provided free by the Raspberry Pi Foundation under a Creative Commons licence.
See more at projects.raspberrypi.org and github.com/raspberrypilearning
1
9 Now pick a new set of random numbers, change them all, and run the program again.
A second pixel should appear on the display!
Introduction
Using the random module
So far you have picked your own random numbers, but you can let the computer choose them instead.
1 Add another import line at the top of your program, below import SenseHat:
1
2 Now change your x = and y = lines to 1
3 Run your program again, and you should
automatically select a random position: see another random pixel being placed
on the display. It will be the same colour as
the last pixel you placed.
x = randint(0, 7)
y = randint(0, 7)
1
4 Now change your colour value lines to: Now your program will automatically select a
random colour.
r = randint(0, 255)
1
5 Run it again, and you should see another pixel
g = randint(0, 255) appear in a random location with a random
b = randint(0, 255) colour.
Run it a few more times, and you should see more of the grid fill up with random pixels.
Introduction
Add a loop
Rather than having to keep running your program, you can add a loop so that it will keep going.
1 First, add an import to the top of your file: You'll use this to pause the program between pixels.
1
2 Add a while True: to your code so that the random lines, set_pixel and sleep are all within
the loop:
while True:
x = randint(0, 7)
y = randint(0, 7)
r = randint(0, 255)
g = randint(0, 255)
b = randint(0, 255)
sense.set_pixel(x, y, r, g, b)
sleep(0.1)
1
3 Run the code and you should see random sparkles in action!
1
4 Try changing the sleep time to make it even shorter.
This project is provided free by the Raspberry Pi Foundation under a Creative Commons licence.
See more at projects.raspberrypi.org and github.com/raspberrypilearning
Sense
HAT smile
Introduction
Test the Sense HAT
1 Open Mu and open the REPL. Type the following commands directly into
the REPL:
Press Enter after each line, and after the third line, the message should appear
on the Sense HAT's display.
1
2 Now try retreiving the sensor values:
sense.temperature
sense.humidity
sense.pressure
sense.accelerometer
sense.gyroscope
sense.orientation
When you press Enter, you will see the sensor's value.
18
This project is provided free by the Raspberry Pi Foundation under a Creative Commons licence.
See more at projects.raspberrypi.org and github.com/raspberrypilearning
Introduction
Faces
1 Now close the REPL and use the main window to type the following program into a file:
sense = SenseHat()
sense.set_pixels(sad)
sleep(1)
sense.set_pixels(normal)
sleep(1)
sense.set_pixels(happy)
1
2 Run the code with F5 and you should see a sad face, a normal face, and a happy face
appear.
Introduction
And breathe...
start_humidity = sense.humidity
while True:
print(sense.humidity)
if sense.humidity > start_humidity + 10:
sense.set_pixels(happy)
elif sense.humidity > start_humidity + 5:
sense.set_pixels(normal)
else:
sense.set_pixels(sad)
sleep(1)
1
2 Run the code again. Now breathe on the Sense HAT and see if you can
make it smile!
18
This project is provided free by the Raspberry Pi Foundation under a Creative Commons licence.
See more at projects.raspberrypi.org and github.com/raspberrypilearning
Sense HAT
cheerlights
1 Open Mu and click the REPL icon to open up a Python shell.
1
2 Import the colorzero library by typing:
1
3 Create a colour object with the word ‘red’:
c = Color('red')
1
4 Inspect the different representations of the colour by typing each of these in turn:
c.rgb
c.rgb_bytes
c.html
1
5 You should see the colour red represented in different ways. Try the same with a
different colour name.
1
6 The Sense HAT library expects RGB values from 0 to 255. Try setting the LEDs to
different colours using:
sense = SenseHat()
color = Color('red')
sense.clear(color.rgb_bytes)
This project is provided free by the Raspberry Pi Foundation under a Creative Commons licence.
See more at projects.raspberrypi.org and github.com/raspberrypilearning
Cheerlights
Introduction
1
2 The starter code simply prints out the tweet contents. Press the Run button and get
someone to tweet #cheerlights red — you should see the word ‘red’ in the output.
1
3 Modify the on_success method in the CheerlightsStreamer class to set the Sense
HAT LEDs to the tweeted colour:
1
5 You might notice that some colour names you try don’t work — and they can cause the
program to crash. Add an exception handler to deal with this, and to let you know when
there’s an error:
Challenges
1 Can you transition from one colour to the next rather than instantly changing it?
12 Can you light up one pixel at a time (in order or at random) by using set_pixels instead
of clear?
13 Can you add support for more colour representations like RGB or hex?
14 Can you add support for keywords like ‘rainbow’ to perform a cycle of colours rather than
a single colour?
This project is provided free by the Raspberry Pi Foundation under a Creative Commons licence.
See more at projects.raspberrypi.org and github.com/raspberrypilearning