This is the fourth video in our five-part Get Started With Pi series.
Purchase these parts to follow along with Cabe and Jenny as they show you how set-up your first Raspberry Pi project.
Using External Controls and your Pi
Although this project does require some programming, you don’t need to know how to program to follow along with this video. The code you need is available for download below on this page.
|You’ll need to take the Raspberry Pi out of its case to access the GPIO Pins. GPIO stands for “General Purpose Input Output” and you’ll find this method of interacting with the Pi is used in a lot of projects that can be found on element14 in our Raspberry Pi Projects.|
GPIO pins are “generic” – they can be used as input or output devices – and can be controlled with programs running on your Raspberry Pi.
Pi Cobber Kit (Adafruit)
We’ll be connecting to the GPIO pins with a – available on the element14 site from our friends at Adafruit. The Pi Cobbler Breakout Kit has three main pieces:
Adafruit Pi Dish
The Adafruit Pi Dish includes this clear acrylic board, some mounting posts to hold the Pi securely in place and the breadboard itself.
There’s a great video on the Adafruit site that shows how to assemble all the pieces.
Some people call these solderless breadboards since you can connect a variety of things without having to solder them all together. Each of these little holes in the breadboard is a socket where you can push in things like led lights, buttons or jumper wires.
Breadboards have two columns of sockets in the middle – ours has one side labeled A through E and the other side labeled F through J. All of the sockets in each row are “connected.”
On the outside of both edges are power rails – usually the blue line is used for ground and the red line is used for power.
Connect one End of the Ribbon Cable to the Pi and the other End to the Breadboard
The ribbon cable has a white line down one side. (Black ones usually have a red line; grey ones usually have a red line.) That line represents the side of the ribbon that should be connected to Pin one. On the Pi – Pin one is in the corner of the board so plug in ribbon with the line on the corner.
Be careful – it can be plugged in backwards!
The other end of the ribbon cable has a little tab on it – that helps line it up and snap in to the PCB.
Plug the PCB into the Breadboard
|There are two rows of pins on the bottom of the PCB – each row has to be plugged in to a different column A-E or F-G on the breadboard.|
Connect the Jumper Wires on the Breadboard
The ribbon has been disconnected in the photos below to better illustrate the wiring.
Now Power up your Raspberry Pi
The following strings are to be entered in LX Terminal. Be sure your Raspberry Pi is connected to the Internet.
Updating and Upgrading
Remember, it's always a good idea to keep your Raspberry Pi Operating System (in this case, Raspbian) up-to-date.
sudo apt-get update
This command downloads the latest version of the OS to your Pi – it may take a while, depending on your network speed.
sudo apt-get upgrade
This command installs the upgraded code (that you just downloaded above) on to your Pi. After you press enter, it will calculate how much space will be required for the new version. It will say something like. . .
After this operation, xxxMB of additional disk space will be used.
Do you want to continue [Y/n]?
Press Y to continue.
Install Python Development Kit and the RPI.GPIO
sudo apt-get install python-dev
Type this in to install the Python Development Kit
sudo apt-get install python-rpi.gpio
Type this in to install the RPI.GPIO
. . . you’ll be prompted to enter “Y” to confirm the install
Create the Python File
One way to create a Python file is to use nano - this command will open an editor screen.
Open file manager on your Pi, copy cow.mp3 to your Pi directory. Then select pi-audio-button.py.txt and double-click to open it.
Select all the text and then toggle to the nano editor running in LXTerminal and right click to select Paste.
Once the text is in nano, press control-x (you'll be prompted to save your work). Press Y and then press enter to save it as the same name.
chmod +x pi-audio-button.py
|Run this string to make the file executable – if you don’t do this step the program won’t run.|
sudo python pi-audio-button.py
This command string will run the program.
When you see the text “When you’re ready, please press the button. . . “ press the button you installed on the breadboard. The audio file should play. And you’ve created your first pi project!
Remember – everything in Linux is case sensitive so make sure you don’t inadvertently capitalize a word. Python is also quite particular about its indentation – that’s how it understands which commands work together – so be sure to set up the file exactly like it’s displayed. Download this as a text file: pi-audio-button.py.txt
import time, os
#Make sure we can import the GPIO library
print ("Unable to import GPIO library. Make sure that this is being run as root and that RPi.GPIO is properly installed!")
#Pins will be referenced by the processor's numbering scheme
#Tell the program you want to use pin number 22 as the input
#Set up a function to check the state of the GPIO pin
#Run as long as loop is nonzero and check the state of the button ten times per second
#If the button is pressed
#Cleanup GPIO since it is no longer needed
#Do not loop again
#Say the cow is coming
print ("Here comes the cow!")
#Actually play the sound
os.system("omxplayer -o hdmi cow.mp3")
#call function, this will run forever
#Restore GPIO to default state
|140PCS, 22AWG Tinned-copper PVC insulated jumper wire, 10pcs. Each of 14 different lengths|
|26 Pin ribbon cable, GPIO / I2C / SPI interface|
|Attach/Detach Pi, 2 mounting orientations, empty space for project|
|22Kohm carbon film 500mW resistor|
|SPST momentary action with flush, round, keyed, or square actuator. Through hole mounting and long operating life - MJTP1230, 50mA|