We don’t think about our five senses (taste, smell, touch, hearing, and sight), but without them we wouldn’t be able to feel, know, and interact with the world around us. Think of your Pi as a person who, until now, has had a limited set of senses. So far, your Pi has only been able to respond to keyboard keys being pressed and mouse clicks.
Like a mad scientist bringing something to life, in this chapter you’re going to embark on a project to wire up a new sense of touch for your Pi. Okay, maybe it won’t be as crazy as creating a bionic creature, but a button gives your Pi a sense of touch. You’ll wire a couple buttons to the Pi’s GPIO pins (recall that GPIO stands for general-purpose input/output, so this is how your Pi can sense and affect the environment). Then you’ll program your Pi to react to button presses. Exciting times are ahead!
This project is a small glimpse of all the different senses you could possibly give your Pi. Electronic components that can detect the environment around them are called sensors. A button is one of the simplest sensors, because it can detect touch. What other sensors could you add? How about some of these ideas:
All this is possible with a Pi, some determination to figure it out, and a bit of fearlessness about trying new things.
In this chapter, you’ll turn your Pi into DJ Raspi—a musical computer that plays different sounds when you press buttons. You’ll wire up two mini pushbuttons on your breadboard and figure out how to write the code to make the buttons play sounds. Later, if you want, you can add other sensors to your Pi and program them. This project will give you an example of how to work with input from sensors. Figure 8.1 shows the parts you’ll need.
Gather the parts and get ready for some fun. You’ll notice that some of them are the same as in chapters 6 and 7, but you’ll also need a few new items. Most of these are included in Raspberry Pi starter kits, but you can find them at online electronics retailers as well. You’ll approach this project in two parts: building the circuit (the hardware) and writing the program (the software). Let’s go!
To start, let’s get your Pi ready to play sounds. A Pi can output sounds through the headphone jack (also called the 3.5 mm audio port) or through HDMI. Before you start, plug in your headphones, powered computer speakers, or, alternatively, a TV with built-in speakers connected via an HDMI cable.
If you wanted to leave a secret message for someone, you could choose several different ways to make the message into a secret code. You could use different symbols to represent words, or you might substitute letters or shift letters around. There are many different ways to encode something.
Similarly, people have come up with many different ways to store sounds (or audio files). These ways (called formats) are different ways of compressing or encoding the information in a sound to make it easy to store on a computer or music player. Sometimes sounds are encoded so they will only work on certain music players.
Here are some common formats:
Each format uses a different method to compress or shrink a sound and make it smaller to store. The Pi has many different software applications for playing audio. Each one can play different formats. Check the Raspberry Pi forums if you want to learn more about the different players and what they’re best for.
You’ll be focusing on playing MP3s from your Pi, because that is a common audio file format. What can you use to play them?
When you watch movies or listen to music on a computer, you may use iTunes or Windows Media Player. Raspbian has its own equivalent called OMXPlayer that can play sounds or videos. Lucky for you, it’s capable of playing MP3 files (or MP3s)—one of the most common audio formats.
OMXPlayer is a video and audio player that was created for Raspberry Pi.
If you don’t have an MP3, you can test OMXPlayer using one of the sounds already on your Pi. There are quite a few MP3s in the folders included with the Scratch software. Open File Manager, and go to this folder to see some of them: /usr/share/scratch/Media/Sounds/Vocals/. In the folder, you’ll see both MP3 and WAV format files (see figure 8.2).
To play an MP3 using OMXPlayer, open Terminal, and enter
pi@raspberrypi ~ $ omxplayer /usr/share/scratch/Media/Sounds/Vocals/ Oooo-badada.mp3
You should hear a short music clip of a woman singing. Enjoy the song!
In Terminal, pressing the up and down arrows cycles through previous commands. Press the up arrow once and then press Enter to run the last command again.
Fantastic! Your Pi can speak to you now.
What if you have speakers or headphones plugged in but don’t hear anything? OMXPlayer should automatically detect whether to output the sound to the 3.5 mm audio output or HDMI. If it doesn’t, try this command for the headphone jack (3.5 mm audio output):
pi@raspberrypi ~ $ omxplayer –o local /usr/share/scratch/Media/Sounds/ Vocals/Oooo-badada.mp3
–o is a special switch or flag that lets OMXPlayer know that you want to tell it something. In this case,–o stands for output, and it tells OMXPlayer where you want to output the sound. In this case, you set it to -o local, which outputs sound to the 3.5 mm (headphone jack) output.
Switches, such as–o for output, act like options or special controls for a program. They’re common when using the command-line interface. You can usually get a list of what switches a program has by making the command print out its help information. Most programs that you can run at the command line will give you a list of all switches or flags when you type the name of the program and then –h. The –h switch- stands for help. Try it with OMXPlayer:
pi@raspberrypi ~ $ omxplayer –h
You’ll see a long list of options you can use to control how video and audio files are played. Try -h with other command-line programs to see what results you get.
If you need to specify sending the sound to speakers in your monitor, then use the –o switch and specify hdmi for output to the HDMI port:
pi@raspberrypi ~ $ omxplayer –o hdmi /usr/share/scratch/Media/Sounds/ Vocals /Oooo-badada.mp3
Now that you know you can play music, let’s build the circuit and write some code to create your DJ Raspi!
Building time! You’re building a circuit on your breadboard to detect or listen to buttons. When a button is pressed, your circuit will send electricity flowing to a GPIO pin on your Pi. You’ll start by connecting the Pi’s GPIO pins to the breadboard using the ribbon cable and GPIO breakout board. Refer back to chapter 6 (section 6.1) if you need to recall how to set this up.
Like finding a seat in a stadium, we’ll refer to the holes on a breadboard using the prefix BB. So the hole located in row 25, column a, is BB25a. Similarly, we’ll refer to the Pi’s GPIO pins using the prefix GP and then the pin number. So GPIO pin 24 is called GP24 for short.
Let’s get busy wiring the buttons. There are many different types of buttons, but you’ll be using a mini pushbutton (see figure 8.3). These buttons commonly come in Raspberry Pi kits along with jumper wires, resistors, and LEDs. If you need to purchase them, you can find them at many online electronics retailers in packs of 10 or 20 for less than the cost of a cheeseburger. With the parts gathered, let’s assemble the circuit.
The circuit diagram for the DJ Raspi is shown in figure 8.4. To listen to whether a button is being pressed, you’ll have electricity (+3.3 V) flow from your Pi to the button. When the button is pressed, the electricity will flow through the button and then split. A small amount of electricity will flow to GPIO pin 6 (GP06) and the rest will flow through the 10K ohm resistor and then to ground (0 V). Let’s put it together on the breadboard.
Let’s build the button circuit on the breadboard and program your Pi to know when the button is being pressed. You’ll give your Pi the ability to feel the button being pressed, by wiring up the button in this order:
1. Add the mini pushbutton to the breadboard.
2. Connect a jumper wire from 3.3 volts to the button. You’ll use the positive power bus (+) that runs along the side of the breadboard.
3. Add the resistor from the button to the negative power bus (-), also called ground.
4. Connect the second jumper wire from the button to GP06 (GPIO pin 6).
The completed circuit for one button will look like what you see in figure 8.5.
Don’t forget, nothing will happen when you press the button. You have to program your Pi to react to this new-found sense of touch. Let’s go through the steps to build the circuit:
Let’s look at how pushbuttons work before we go on. If you had X-ray goggles, you would see that the left and right legs at the top of the button are connected. Similarly, the left and right legs along the bottom of the button are connected. The top and the bottom of the button aren’t connected.
But when you press the button, figure 8.6 shows what happens. Pressing the button pushes down a small metal bar so that the top and bottom are connected. We say the switch is closed. When you let go of the button, the spring in the button pushes the metal bar back up, and the switch is open again. Grab your mini pushbutton, and let’s insert it into the breadboard.
You’ll need to push the button into the breadboard very firmly. If the button legs aren’t lined up with the breadboard holes, you may accidentally bend some of the button legs. Don’t worry—you can bend them back and try again. If a leg breaks off, use a new button.
You’re going to put the button in rows 23 and 25 along columns d and g on the breadboard. Connect the legs:
When the button is inserted, it will look like figure 8.7. Double-check that it’s pushed down into the breadboard so that all the legs will make a good connection. Good job—you just completed the trickiest part!
You need to connect the button to a source of electrical current. You’ll use the positive power rail along the edge of the breadboard as the source of power (you could also directly connect the jumper to the 3V3 pin on the breakout board).
Connect the jumper wire from the positive power bus (+) to BB25a. Remember, you can connect the jumper to any hole along the power rail (it has a red line next to it). When you’ve added the wire, it will look like figure 8.8.
Fantastic! Now you have electricity reaching the bottom legs of the button.
Time to connect your 10K ohm resistor. It has bands of brown, black, and orange followed by a fourth gold or silver band. Remember that electricity will flow either way through a resistor, so it doesn’t matter which way you place it.
You’re connecting the resistor from the top of the button to the negative power bus (-). This is the set of holes with a blue stripe next to it running along the edge of the breadboard.
Insert one end of the resistor into BB23i and the other end into the negative power bus (-). You can choose any hole along the blue line. Once the resistor is added, you’ll have something that looks like figure 8.9. Now you’re ready for the final step.
A small amount of electricity needs to reach a GPIO pin (you’ll use GP06), so you need a jumper wire from the top of the button to a hole next to the GPIO pin. To make this connection, add a jumper from BB23a to BB16a. Figure 8.10 shows how it looks.
When the button is pressed, a small amount of electricity will flow to GP06 and through the resistor to ground. Nothing happens yet, but next you’ll write a Python program to detect that electricity and play some sounds.
Let’s add a second button to the board. Figure 8.11 shows what it will look like when it’s done.
To add another button, you’ll create the same circuit but place the button in rows 28 and 30 on your breadboards. You’ll wire the button to GP19.
Insert the button so that the top legs are in BB28d and BB28g and the bottom legs are in BB30d and BB30g.
You need to connect power from the positive power bus to the bottom of the button. The power rail is the line of holes with a red line running next to it. Insert a jumper from anywhere along the positive power bus (+) to BB30a.
To prevent too much electricity from flowing when the button is pressed, you need to add a resistor. As before, you’ll add a 10K ohm resistor (color bands are brown, black, and orange) to connect the top of the button to the negative power bus (-).
Insert one end of the resistor into BB28i and the other end into the negative power bus (-). Any hole along the blue line will work.
Finally, when the button is pressed, you need electricity to flow to a GPIO pin. For the second button, you’re using GP19. Connect a jumper wire from BB28a to BB18a (GP19).
Terrific! The second button is connected, and you’ve completed the button circuit. Let’s call the first button Button 1. It’s wired to GP06. The second button, Button 2, is wired to GP19. Now that everything is wired up, let’s write code for it!
Your project is to turn your Pi into an awesome music player that is controlled by buttons. Here’s how it will work:
You’ll need one of the following to hear the sounds:
Let’s think through how this program will work. Figure 8.12 shows a quick diagram of the logic.
Let’s write the code in this order:
1. Set up your Pi to listen to input coming from the buttons.
2. Gather a list of music and vocal sounds.
3. Program a loop to check the buttons. If they’re pressed, then play random sounds.
You’ll try to use functions along the way to simplify your code.
Let’s begin! Open IDLE by choosing Python 3 under Menu > Programming. In the Python Shell, start a new program by pressing Ctrl-N or selecting File > New Window.
In the IDLE text editor, you’ll start by loading the Python libraries you’ll need to use. You’ll also set up a couple of the Pi’s GPIO ports to listen for electrical signals coming in from the buttons being pressed. In the flow diagram, this is the first step of initializing the buttons (see figure 8.13).
When you set up the GPIO ports, you use GPIO.IN to tell the Pi that you plan to use that port as an input. To prepare your Pi for input to the GPIO pins, you need to tell it which pins you plan to use. Based on the circuit, you’re using these pins as inputs:
The following listing shows how you can use the GPIO.setup command to set a GPIO pin to input.
You may notice that you import a new os module. We’ll talk about why you need that in the next section when you gather your lists of sound files.
Lists are everywhere around you. You make lists of things you need to do, gifts to buy, places you want to visit, and favorite things, such as your top-10 movies or books.
Your DJ Raspi needs a list of sound files: one for music clips (or loops) and one for vocals. Based on the design, you need to get a list of files from a folder on your Pi, and then you need to select a random sound file from the list and play it (see figure 8.14).
In Python, you can create lists or groups of things easily. Let’s look at some examples.
Let’s create a list of basketball player names. Open IDLE to the Python 3.x Shell by choosing Python 3 under Menu > Programming. In the Python Shell, make a list:
>>> basketball_players = ["Kevin Durant", "LeBron James", "Chris Paul", "John Wall"]
Print out the list like this, and you’ll see what’s inside:
>>> print(basketball_players) ['Kevin Durant', 'LeBron James', 'Chris Paul', 'John Wall']
To make a list of the items, put them in a set of square brackets ([]) and separate each item with a comma (see figure 8.15). For lists of strings, each item in the list has to have quotation marks around it. Pretty simple! That’s the Python way.
Try creating a list called favorite_numbers, like so:
>>> favorite_numbers = [22, 27, 49, 121, 2, 25]
Display the contents of the list using print:
>>> print(favorite_numbers) [22, 27, 49, 121, 2, 25]
When making a list of numbers, you don’t use any quotation marks.
Enjoy making lists of some of your favorite things!
There are lots of things you can do with lists! Let’s try a few.
You make a list longer by adding more items to it. To do this, use the append method. Let’s add the name Stephen Curry to the list of basketball_players. Here is how you can use append to do that:
>>> basketball_players.append("Stephen Curry")
Use print to see the result:
>>> print(basketball_players) ['Kevin Durant', 'LeBron James', 'Chris Paul', 'John Wall', 'Stephen Curry']
Excellent! To remove an item from a list you can use the remove method. If you wanted to take John Wall out of the list, write
>>> basketball_players.remove("John Wall")
Print the list again to see if it worked:
>>> print(basketball_players) ['Kevin Durant', 'LeBron James', 'Chris Paul', 'Stephen Curry']
Wonderful! If you need to put a list in order alphabetically or from lowest to highest, you can use the sort method like so:
>>> favorite_numbers.sort()
Check that it worked by printing the list to the screen:
>>> print(favorite_numbers) [2, 22, 25, 27, 49, 121]
The numbers are all sorted! This works on lists made of strings as well. If you sort the list of basketball_players, it puts them in alphabetical order based on the first letter of each string. Python has many built-in methods for lists.
Check the online Python documentation[a] for more things you can do with lists. Then sit back and enjoy thinking about all you can do with them in your future programs.
Go to the Python website for more information on things you can do with lists: https://docs.python.org/3.4/tutorial/datastructures.html.
For your DJ Raspi, let’s see how to
Let’s start with a fresh list of basketball players:
basketball_players = ["Kevin Durant", "LeBron James", "Chris Paul", "Stephen Curry"]
As you’ve seen, lists store information. What you might not know is that each spot in a list is given a number called the index. The index of the first item in the list is zero (0). The second item has an index of 1. The third item’s index is 2, and so on. To get the third item in the basketball_players list, you’d type
>>> print(basketball_players[2]) Chris Paul
If you want to search a list and have Python tell you the index of where an item first appears in the list, you use the index method:
>>> basketball_players.index("Kevin Durant") 0 >>> basketball_players.index("Stephen Curry") 3
If the item isn’t in the list, Python will give you an error saying so:
>>> basketball_players.index("Me") Traceback (most recent call last): File "<pyshell#40>", line 1, in <module> basketball_players.index("Me") ValueError: 'Me' is not in list
Remember that the index for lists starts counting at 0, not 1! For example, basketball_players[1] gives you "Lebron James", the second item in the list.
Figure 8.16 shows examples of the indexes for a list and how you can get a specific item in a list.
Finally, there are times when you’ve loaded information into a list and you need a way to check how long the list is. Use the len() function to do that:
>>> yummy_snacks = ["chips", "popcorn", "donuts", "cheese", "pretzels", "spam"] >>> print(len(yummy_snacks)) 6
Great job—you know the basics of lists. Now let’s see how you can create lists of MP3s.
To make the DJ Raspi project work, you need to
1. Grab two lists of sound files from folders on your Pi.
2. Make OMXPlayer play sound files from Python as part of the DJ Raspi program.
Let’s learn how.
The Pi has both these abilities through a Python module called the os module (OS stands for operating system). With it, you can run operating system commands (things you can type in the Terminal window) from your Python programs. This is fantastic, because it means you can get lists of files and also call OMXPlayer to play a certain file—exactly what you need!
Your Pi has some sound files on it already, as you saw in section 8.1. You’ll use the files in these two folders:
The os library provides a built-in function, os.listdir(some_path), to get a list of files at some_path. To get a list of Scratch music loops and vocals, use these commands:
# Folders with sound files path_music = "/usr/share/scratch/Media/Sounds/Music Loops/" path_vocals = "/usr/share/scratch/Media/Sounds/Vocals/" # Creating two lists with the files in the folders sounds_music = os.listdir(path_music) sounds_vocals = os.listdir(path_vocals)
If you print the lists, you’ll have something that looks like this:
print(sounds_music) ['Cave.mp3', 'Techno.mp3', 'HipHop.mp3', 'Triumph.mp3', 'Medieval2.mp3', 'HumanBeatbox2.mp3', 'DripDrop.mp3', 'Xylo3.mp3', 'GuitarChords1.mp3', 'DrumSet2.mp3', 'Xylo2.mp3', 'DrumSet1.mp3', 'Garden.mp3', 'GuitarChords2.mp3', 'Jungle.mp3', 'Xylo1.mp3', 'Eggs.mp3', HumanBeatbox1.mp3', 'Drum.mp3', 'DrumMachine.mp3', 'Techno2.mp3', 'Medieval1.mp3', 'xylo4.mp3'] print(sounds_vocals) ['Oooo-badada.mp3', 'Singer1.wav', 'BeatBox1.wav', 'Ya.wav', 'BeatBox2.wav', 'Come-and-play.mp3', 'Hey-yay-hey.mp3', 'Doy-doy-doy.mp3', 'Singer2.wav', 'Got-inspiration.mp3', 'Join-you.mp3', 'Sing-me-a-song.mp3']
Wow—you have nice-looking lists! But wait: it looks like sounds_vocals has WAV (.wav) files and MP3s. Let’s filter out the WAVs so you only have MP3s.
To filter a list, you can use Python’s list-comprehension feature. List comprehension is a quick way of creating lists. When you use it, you can include certain conditions or operations that are applied to the items in the list, such as making sure all the files in the list end with .mp3. Let’s look at how you can use list comprehension to create a new list from your old list, but only keep the files in the list that end with .mp3:
sounds_music = [sound for sound in sounds_music if sound.endswith('.mp3')] sounds_vocals = [sound for sound in sounds_vocals if sound.endswith ('.mp3')]
The list comprehension has a for loop inside it. In this case, Python is looping through the list of sound files in your original list of sounds. For each item in the list, Python only adds it to the new sounds list if it matches the condition of being a file ending with .mp3.
Next in your plan is to write the code that will play a random sound from your lists when a button is pressed. You’ll need this to be in a loop so the buttons are repeatedly checked to see whether they’re being pressed (see figure 8.17). Let’s start by creating the game’s title and creating the main game loop.
First let’s add some code to display a title screen and the DJ Raspi instructions. Feel free to make the title screen fancier!
Now let’s write the code to loop over and over again to check whether either button is being pressed. When a button is pressed, the GPIO pin will give you a response of True, and you can then call a function to play a random MP3.
The code repeatedly checks whether Button 1 or Button 2 is pressed. If Button 1 is pressed, the code plays a random music sound. If Button 2 is pressed, the code plays a random vocals (singing) sound. If neither is pressed, the code loops around and checks them again. The loop never ends, so you’ll need to press Ctrl-C to exit the program.
You’re ready to play your sounds! The os module will let you run operating system commands (ones you normally run using Terminal). To play the first sound in the sounds_music list, you could write
os.system("omxplayer -o local '" + path_music + sounds_music[0] + " &")
Later in this chapter, we’ll explain why the end of that command has an ampersand (&). The result of this command would be the same as typing this at the Raspbian command line:
omxplayer –o local "/usr/share/scratch/Media/Sounds/Music Loops/Cave.mp3"
Remember, if you’re outputting the sound to HDMI (if your TV has speakers), you need to change –o local to –o hdmi.
Excellent! Let’s review what you’ve learned so far:
Let’s think about how you can write the functions for DJ Raspi. You’ll want to create two functions:
Figure 8.18 shows where these functions fit into the flow diagram.
Why not make this a single function? One reason is that you only need to load a list of sound files once, near the beginning of the program. You play the sound files every time a button is pressed. Listing 8.4 shows the code for the get_MP3_sounds and play_random_sound functions.
Remember to put these functions near the beginning of the program. They must be added before they’re used for the first time.
At the end of this listing, you use (or call) get_MP3_sounds twice to get your lists of music and vocal sound files.
You may have noticed that a few extra things are added to this line:
os.system("omxplayer -o local '" + sound_path + "/" + sound_filesound_files[random_sound_index] + "' &")
This line joins the command to run OMXPlayer with the path to your sound files (sound_path) and the random sound file you want to play (sound_filesound_files[random_sound_index]). At the end, you add an ampersand (&). The ampersand tells Raspbian to run the command in the background. This is so you can quickly press one button and then the other.
When you play sounds, you want to be able to press the buttons quickly, like a DJ, and have the sounds overlap to create interesting music. Normally, your Pi would play one sound, and, when it was finished playing, let you play another. Not what you want. Here is an example of playing two sounds. You can’t run the second command until the first one is finished:
omxplayer /usr/share/scratch/Media/Sounds/Vocals/Oooo-badada.mp3 omxplayer /usr/share/scratch/Media/Sounds/Vocals/Hey-yay-hey.mp3
Luckily, your Pi can do a few things at once. You might have several different windows open at the same time. Each window is connected to some underlying code or set of instructions running on your Pi. These underlying sets of code are called processes or threads. Raspbian, like other modern operating systems, manages these processes and assigns each one its own unique ID number. An ampersand (&) placed at the end of a command tells your Pi to run the command as another process in the background alongside any other processes.
Try these two commands again, but this time with ampersands:
omxplayer /usr/share/scratch/Media/Sounds/Vocals/Oooo-badada.mp3 & omxplayer /usr/share/scratch/Media/Sounds/Vocals/Hey-yay-hey.mp3 &
Notice the ampersand (&) at the end of each command. In this way, the Pi will play your sound, but the code won’t make your Pi wait for the sound to finish before doing something else. Adding an ampersand at the end of the OMXPlayer command makes the button play each sound in the background. Remove the ampersand to play one sound at a time. Because this is a feature of the OS, the concept of using ampersands to execute commands as their own unique processes applies to other Linux commands that you know already or will learn.
Great! You’re ready to test your project!
Save the code as DJRaspi.py, and try running it. Select Run > Run Module (or press F5) from the IDLE text editor to run your program. If you have an older version of Raspbian (prior to October 2015), programs that use the GPIO pins must be run from the Raspbian command prompt as the superuser (or root). Open Terminal, and enter the following command:
pi@raspberrypi ~ $ sudo python3 DJRaspi.py
You should see the title screen display. Test it by pressing the buttons to see if they work.
Remember that any program that uses GPIO pins must be run from the Raspbian command prompt as the superuser (or root).
Believe it or not, it’s rare for a program to work perfectly the first time. If it doesn’t, read through the following “Troubleshooting” section and review your circuit and program to try to figure out how to get it working.
If sounds aren’t playing when you press the buttons, here are some things you can check:
omxplayer –o local /usr/share/scratch/Media/Sounds/Vocals/ Oooo-badada.mp3For TV speakers connected by HDMI:
omxplayer –o hdmi /usr/share/scratch/Media/Sounds/Vocals/ Oooo-badada.mp3
If you’ve enjoyed creating DJ Raspi, check out the button challenges.
Try some of these button activities for extra fun!
Give your program a surprise button combination. See if you can make pressing both buttons at once play a new set of sound effects.
Hint: When you want something to happen only if both conditions are True, you can use the ampersand (&). The if statement will only be True if both the first and second conditions are True. It looks like this:
if GPIO.input(button_pin1) & GPIO.input(button_pin2): print("Both buttons are pressed!")
Here is the path to some Scratch sound effects on your Pi:
path_effects = "/usr/share/scratch/Media/Sounds/Effects/"
There is a great classic toy called the Magic 8 Ball. It’s a ball that displays an answer to a question when you shake it. Ask it a question, and you’ll get some truly magical advice. The Magic 8 Ball has 20 different answers, ranging from “It is certain” to “My sources say no.”
I don’t recommend using the Magic 8 Ball to advise you on major life matters!
Your challenge is to make a Magic 8 Ball program:
To get started, find some short sound files of Yoda sayings. One place to find them is on soundboard.com. Search for “yoda” to see if you can locate some good clips (you’ll need to create an account to download them for your personal use).
Bonus: Try to give your Pi a handy button that plays Monty Python sound clips whenever you press it.
Now that you’ve given you Pi a new sense of touch, you’ll only need to change a few lines of code to make many other projects, such as these:
You can also expand past buttons to sensors, such as passive infrared (PIR) sensors or cameras. For example, PIR sensors detect motion near the sensor. These are great for creating a Pi security system or something that scares people when they come to your door. Maybe you want to trigger a movie to make a frightening zombie head appear or generate a blood-curdling scream. Your only limit is your imagination and mischievous thoughts.
In this chapter, you learned that
You’ve completed a great adventure in learning Python programming and how to use your Raspberry Pi. But there is much more excitement ahead of you. Check out appendix D for even more ideas of projects you can do with your Pi. With your Raspberry Pi, knowledge of Python, and a bit of fearlessness, the possibilities are endless!
18.191.211.66