You will learn how to turn a jelly baby into an input device for your Raspberry Pi. You will then create a program to make it scream when it is pressed.
##Requirements
- Raspberry Pi
- Micro USB power adaptor
- An SD card with Raspbian already set up through NOOBS
- USB keyboard
- USB mouse
- HDMI cable
- A monitor or TV
- Speakers or headphones
- Two metal paper clips or dress pins
- Two female to female jumper wires
- A jelly baby
First check that you have all the parts you need to get your Raspberry Pi set up and working.
- Raspberry Pi
- Micro USB power adaptor
- An SD card with Raspbian already set up through NOOBS
- USB keyboard
- USB mouse
- HDMI cable
- A monitor or TV
###Activity checklist:
-
Place the SD card into the slot of your Raspberry Pi. It will only fit one way so be careful not to break the card.
-
Next connect the HDMI cable from the monitor (or TV) to the HDMI port on the Pi and turn on your monitor.
-
Plug the USB keyboard and mouse into the USB ports on the Pi.
-
Plug in the micro USB power supply and you should see some text appear on your screen.
-
When prompted to login type:
Login: pi Password: raspberry
To begin to turn a simple jelly baby into a switch, you will attach cables to it and then connect them to the GPIO pins on a Raspberry Pi.
-
Take the metal paper clips and unfold them to make a straight wire.
-
Take a female to female jumper cable and push the paper clip wire into one of the ends.
-
Do the same to the other wire so that you have two identical jumper cables with paper clip wires in one end.
-
Insert the paper clips into a jelly baby so that they are close to each other but not touching.
The picture above shows the Raspberry Pi GPIO header pins; the diagram above the pins shows the pin numbers. You will be using pin 3 and pin 25.
-
Take the other end of one of the jumper leads and push it onto pin 3 of the General Purpose Input-Output (GPIO) header which is connected to one of the GPIO channels.
-
Take the end of the other jumper lead and push it onto pin 25 of the GPIO header which is connected to ground.
Warning: You can damage your Raspberry Pi if you do not use the GPIO pins correctly!
-
Make sure your Raspberry Pi has an internet connection, and speakers or headphones so that you can hear your jelly baby screaming. If you are using headphones or a speaker on the Raspberry Pi, you will need to run the following command to redirect sound to the headphone socket:
sudo amixer cset numid=3 1
So far you have created your input device and have your Raspberry Pi set up and running. You now need to download a scream sound file and install some software to be able to play it. This task can be achieved from the command line.
-
After logging into your Raspberry Pi you will see the following text:
pi@raspberry ~ $
The Raspberry Pi is waiting for you to type in a command to do something. This is referred to as the command line.
-
Download an mp3 sound effect to play when the jelly baby is pressed. Enter the following command; it should be typed on one line with the same uppercase and lowercase characters:
wget http://goo.gl/MOXGX3 -O la.mp3 --no-check-certificate
-
Now test that you can play the sound file using
omxplayer
by typing:omxplayer la.mp3
omxplayer
will play the downloaded sound file and you should hear it from the speakers or headphones connected to your Pi.If you cannot hear anything, make sure that your headphones or speakers are connected correctly. If the jack/plug looks like the picture below (notice the three black bands) you may find that it will only work if you pull the plug out by a few millimetres.
The final step to make your jelly baby scream is to write a program in Python; it will detect when you press the jelly baby input device and output the scream sound.
-
To write your Python program you will need to open a text editor window from the command line. To do this type the following command:
nano scream.py
-
Begin your program by importing the modules and libraries needed to make it work. Type the following command:
import time import RPi.GPIO as GPIO import os
The time library will be used to make the program pause for a fixed amount of time. The Raspberry Pi GPIO libraries will be used to connect the Raspberry Pi to other physical devices via the General Purpose Input-Output (GPIO) pins, in this case your jelly baby input device! The
os
library will be used to allow our program to call other programs that run on the Raspberry Pi likeomxplayer
. -
Now you will need to set up the GPIO pins to use GPIO board pin numbers. Leave a line empty by pressing Enter on your keyboard, then type:
GPIO.setmode(GPIO.BOARD)
-
To switch off the "Ports already in use" warnings, press Enter to give you a new line and type:
GPIO.setwarnings(False)
-
Set pin 3 on the GPIO header to be an input with the following command:
GPIO.setup(3,GPIO.IN)
-
Create a loop that runs forever and plays the screaming sound file when the two wires inside the jelly baby are touching by typing the following:
while True: if GPIO.input(3) == False: os.system('omxplayer la.mp3') time.sleep(1);
-
Save the file by pressing
CTRL+X
, thenY
for yes, followed byEnter
. -
Finally, run the program by typing:
sudo python scream.py
Congratulations! Now when you press the jelly baby, the wires will touch and the mp3 file will play.
-
Exit the program when you are finished by pressing
CTRL+C
.
- Using a real button or switch connected to a breadboard
- Changing the sound that plays when the device is pressed
- Can you think of a way to use more inputs?
Unless otherwise specified, everything in this repository is covered by the following licence:
Screaming Jelly Baby by the Raspberry Pi Foundation is licenced under a Creative Commons Attribution 4.0 International License.
Based on a work at https://github.com/raspberrypilearning/screaming-jellybaby