In this resource, you can make a jelly baby burp whenever you give it a squeeze.
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.
There are 40 pins on the Raspberry Pi (26 pins on early models), and they perform various different functions.
If you have a RasPiO pin label, it can help to identify what each pin is used for. Make sure your pin label is placed with the key-ring hole facing the USB ports, pointed outwards.
If you don't have a pin label, then this guide can help you to identify the pin numbers:
You'll see pins labelled as 3V3, 5V, GND, GP2, GP3, and so on:
3V3 | 3.3 volts | Anything connected to these pins will always get 3.3V of power |
5V | 5 volts | Anything connected to these pins will always get 5V of power |
GND | ground | Zero volts. Used to complete a circuit |
GP2 | GPIO pin 2 | These pins are for general-purpose use and can be configured as input or output pins |
ID_SC/ID_SD/DNC | Special purpose pins |
-
Take a male-to-female jumper wire and push the pin end into the jelly baby.
-
Do the same with the other wire, making sure they are close enough inside the jelly baby but not touching
-
Now attach the free ends of the jumper leads to
GPIO 17
and anyGND
pin.
So far, you've created your input device and have your Raspberry Pi set up and running. You now need to find a burping sound file and move it into a new folder. This can all be achieved in a terminal window, which can be opened by pressing Ctrl
+ Alt
+ T
.
-
Create a new folder called
jellybaby
with the following command:mkdir jellybaby
-
Enter the folder with
cd jellybaby
We're going to need a burping sample sound file for this project. You could find your own one online, or use the one provided in the next step.
-
Download a copy of a burp sound effect with the following command:
wget http://rpf.io/burp -O burp.wav
-
Now test that you can play the sound file using
aplay
by typing:aplay burp.wav
aplay
will play the sound file and you should hear it from the speakers or headphones connected to your Pi.
The final step to make your jelly baby burp is to write a program in Python. The program will detect when you press the jelly baby input device, and will output the burp sound.
-
Go to
Menu
>Programming
>Python 3 (IDLE)
. -
Once IDLE 3 has opened, click on File and New File. This will open a blank file. Click on File and Save As and name the file
burp.py
. -
Begin your program by importing the modules and libraries needed to make it work. Type the following:
from gpiozero import Button import pygame from time import sleep
This allows you to find out when the button has been pushed using
gpiozero
, and allows you to usepygame
to play a sound. Thetime
library is used to pause the program for a while. -
Next, you need to tell your program which GPIO pin the jelly baby is attached to:
jelly_baby = Button(17)
-
You also need to initialise
pygame
and import the sound into your program:pygame.init() pygame.mixer.init() burp = pygame.mixer.Sound("burp.wav")
-
Lastly, you can use an infinite loop to wait for the jelly baby to be pushed and then play the sound:
while True: jelly_baby.wait_for_press() burp.play() sleep(2) burp.stop()
-
Save the file by clicking on File and Save.
-
Finally, run the program by clicking on Run and Run Module.
Congratulations! Now when you press the jelly baby, the wires will touch and the burp sound file will play.
- Try using a real button or switch connected to a breadboard.
- Can you change the sound that plays when the device is pressed?
- You could even create a whole music box with our GPIO Music Box tutorial.