How to create your very own Raspberry Pi "Harry Potter Sorting Hat" - Pibrella Version
In this lesson, students will create a Raspberry Pi powered Harry Potter style sorting hat. Students will also learn how to connect a physical button to the Pibrella and program it to control the sorting hat.
Note: Although I have written this as a lesson, it would probably work better as a project for a small group / after-school club.
Learning Objectives:
Note: Although I have written this as a lesson, it would probably work better as a project for a small group / after-school club.
Learning Objectives:
- Understand and use sequence in an algorithm
- Understand and use iteration in an algorithm (FOR and WHILE loops)
- Understand and use selection in an algorithm (IF, Else and Else if)
- Understand how to control Input & Output devices
Curriculum Mapping:
KS2:
KS3:
KS2:
- Design, write and debug programs that accomplish specific goals; solve problems by breaking them into smaller parts. Select, use and combine a variety of software on a range of digital devices to design and create a range of programs.
- Use sequence, selection and repetition in programs; work with variables and various forms of input and output
- Use logical reasoning to explain how some simple algorithms work; detect and correct errors in algorithms and programs
KS3:
- Use two or more programming languages, at least one of which is textual, to solve a variety of computational problems.
What you will need (For each group of students):
Note:
* This lesson assumes that students already have some experience of using the Raspberry Pi and that they have already installed the NOOBS Operating System. To find out how, click here
** Most D&T departments will have some of these lying around and may let you borrow them if you ask nicely (I find taking a box of sweets usually helps!)
*** Obviously, you will also need a hat. Here are some ideas to get you started:
- 1 x Raspberry Pi (with monitor, keyboard and mouse)
- 1 x 8gb SD Card (With NOOBS installed *)
- 1 x Push button / switch ** (example: Large Red Push Button)
- Male-to-female jumper leads ** (example: Jumper Lead Cables for Arduino/Raspberry Pi)
- 1 x Pibrella
- 1 x speakers
- Hat *** (See options below)
Note:
* This lesson assumes that students already have some experience of using the Raspberry Pi and that they have already installed the NOOBS Operating System. To find out how, click here
** Most D&T departments will have some of these lying around and may let you borrow them if you ask nicely (I find taking a box of sweets usually helps!)
*** Obviously, you will also need a hat. Here are some ideas to get you started:
|
3. DIY (Do It Yourself) Sorting Hat
Create your very own sorting hat. (Above is the version created by team Python - Led by yours truly at PiCademy) Pros:
Cons:
|
Setting up the Pi:
1. First, students must attach their Pibrella to the Raspberry Pi (See instructions here: Setting up your Pibrella)
WARNING! Always shutdown your Raspberry Pi before connecting add-ons.
WARNING! Always shutdown your Raspberry Pi before connecting add-ons.
2. Next, students will need to connect their push button / switch to their Pibrella.
Connect the push button / switch using two jumper cables.
Important! Make sure you connect the leads to Input A (See below)
Connect the push button / switch using two jumper cables.
Important! Make sure you connect the leads to Input A (See below)
Tip: You can connect the switch directly to the Pibrella using a couple of paperclips (See image below)
3. Next, students will need to start their Raspberry Pi and launch the graphical desktop environment. To launch the graphical desktop environment:
At the command prompt type the following:
startx
At the command prompt type the following:
startx
Installing additional libraries
In order to create the sorting hat, students will need to install some additional library packages required for Python:
Start by launching LXTerminal to start a command line:
Double click on LXTerminal (You will normally find this on the desktop)
Start by launching LXTerminal to start a command line:
Double click on LXTerminal (You will normally find this on the desktop)
Students will need to install the following libraries in order for the sorting hat to work:
Tip: Before installing new libraries, it's always a good idea to update the raspbian operating system first:
To upgrade the operating system:
Next, students must install the Pibrella library package
At the command prompt, type in:
sudo apt-get install python-pip
followed by:
sudo pip install pibrella
Finally, students need to install the the mpg123 library
Whilst still at the command prompt, type in:
sudo apt-get install mpg123
- Pibrella (This allows you to communicate with the GPIO pins via the Pibrella on the Raspberry Pi)
- mpg123 (This will allow you to play the audio files)
Tip: Before installing new libraries, it's always a good idea to update the raspbian operating system first:
To upgrade the operating system:
- type sudo apt-get update in the command line
- type sudo apt-get upgrade in the command line
Next, students must install the Pibrella library package
At the command prompt, type in:
sudo apt-get install python-pip
followed by:
sudo pip install pibrella
Finally, students need to install the the mpg123 library
Whilst still at the command prompt, type in:
sudo apt-get install mpg123
Programming the Pi
Note: In order for the sorting hat script to work, students must launch idle as Super User (sudo) - without this, the students will not be able to communicate with the GPIO header via the Pibrella.
1. (If students haven't already done so) Double click on LXTerminal to start a command line
Note: In order for the sorting hat script to work, students must launch idle as Super User (sudo) - without this, the students will not be able to communicate with the GPIO header via the Pibrella.
1. (If students haven't already done so) Double click on LXTerminal to start a command line
2. Type in the following at the command line to start the Python environment:
sudo idle3 &
(Important! Make sure you add the ampersand '&' at the end)
3. Select File > New Window from the menu to start a text editor
4. Enter the code as follows (Note: Case is important)
sudo idle3 &
(Important! Make sure you add the ampersand '&' at the end)
3. Select File > New Window from the menu to start a text editor
4. Enter the code as follows (Note: Case is important)
## Sorting Hat version 2 - Uses Pibrella
# http://www.raspberrypi.org/learning/python-picamera-setup/
import pibrella
import time
import random
import os
def randomgenerator():
number = random.randint(1,4)
if number == 1:
os.system ('mpg123 Gryffindor.mp3')
time.sleep(1)
elif number == 2:
os.system ('mpg123 Hufflepuff.mp3')
time.sleep(1)
elif number == 3:
os.system ('mpg123 Ravenclaw.mp3')
time.sleep(1)
else:
os.system ('mpg123 Slytherin.mp3')
time.sleep(1)
while True:
# wait for the button to be pressed
while True:
while pibrella.input.a.is_high():
# Slight pause for dramatic effect
time.sleep(0.1)
randomgenerator()
# http://www.raspberrypi.org/learning/python-picamera-setup/
import pibrella
import time
import random
import os
def randomgenerator():
number = random.randint(1,4)
if number == 1:
os.system ('mpg123 Gryffindor.mp3')
time.sleep(1)
elif number == 2:
os.system ('mpg123 Hufflepuff.mp3')
time.sleep(1)
elif number == 3:
os.system ('mpg123 Ravenclaw.mp3')
time.sleep(1)
else:
os.system ('mpg123 Slytherin.mp3')
time.sleep(1)
while True:
# wait for the button to be pressed
while True:
while pibrella.input.a.is_high():
# Slight pause for dramatic effect
time.sleep(0.1)
randomgenerator()
5. Select File > Save
Give your file a sensible name e.g. "sorting hat.py" (Note: Make sure you save )
6. Select Run > Run Module
Give your file a sensible name e.g. "sorting hat.py" (Note: Make sure you save )
6. Select Run > Run Module
Important! Students MUST also copy the following audio files into the same folder as their python script:
ravenclaw.mp3 |
slytherin.mp3 |
hufflepuff.mp3 |
gryffindor.mp3 |
Here's one I made earlier:
Troubleshooting
I can't hear any sound from the speakers.
I can't hear any sound from the speakers.
- Make sure that the audio files (above) are in the same folder as the python script.
- If you use a HDMI lead to connect to your monitor, the Raspberry Pi will automatically output the sound via the HDMI and NOT the headphone jack. To find out how to change the audio output settings, click here.
- Make sure you have installed mpg123 (sudo apt-get install mpg123)
Extension Task
- Connect multiple LEDs (One for each Hogwarts House / Group) and make each one flash depending on which house / group is picked
- Record your own audio (These could be houses in your school, colours or numbers)
- Add a second switch which will power up the Raspberry Pi and launch your python script on startup (Click here for help)
You may also be interested in:
- Raspberry Pi Sorting Hat (GPIO version) - Coming soon
- Raspberry Pi Sorting Hat (Scratch GPIO version) - Coming soon
Licence:
Unless otherwise specified, everything in this repository is covered by the following licence:
Unless otherwise specified, everything in this repository is covered by the following licence:
Raspberry Pi "Sorting Hat" lesson is licenced under a Creative Commons Attribution 4.0 International License.