Version 25

    This is the fourth video in our 2013 five-part series Get Started With Pi for Raspberry Pi 2..



    Episodes in this Series


    Video 1: Unboxing and Setting up the Peripherals

    Video 2: Exploring the Raspberry Pi

    Video 3: Getting Your Pi Online

      Your First Pi Project

    Video 5: Exploring Accessories and Projects


    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.


    GPIO Pins


    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 Pi Cobbler kitPi Cobbler kit – available on the element14 site from our friends at Adafruit.  The Pi Cobbler Breakout Kit has three main pieces:


    . . . a 26-pin ribbon cable
    . . . a connector that attaches to the ribbon.


    The one pictured on the bottom connects to the PCB.  You can tell the difference because the one on the bottom has that little tab – that helps it line up properly on the PCB.

    . . . a PCB board


    The PCB is printed with information about the pins that stick into the breadboard.

    • The labels show you which pins
    • Are Ground
    • Carry a 5.0 volt power supply (Use caution – 5.0 volt power can damage the Raspberry Pi)
    • Carry a 3.3 volt power supply
    • Can serve as input or output pins


    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


    PCBheader.gifThere 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.


    First Power the board.

    Find a pin labeled 3.3v and run a jumper wire from the row of pins in columns A-E with the power – or red rail on the same side (green wire).

    Then find a pin labeled GND and run a jumper wire from that row of pins to the blue rail on the opposite side (orange wire).

    Next, run a jumper wire from Pin 22 to any open row above (yellow wire).

    Next add the resistor from the same row as the yellow wire to the power rail.

    Add the button in the center – spanning the gap between the two columns much like the PCB.  The bottom pins of the button should be on the same row as the yellow wire.

    In the same row as the top pins of the button, add another wire to the blue (ground) line.


    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.

    Save these two files to a thumb drive (in Windows, right click link and select “Save target as. . .”):
      and then connect the thumb drive to your Raspberry Pi.

    Open file manager on your Pi, copy cow.mp3 to your Pi directory.  Then select 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

    Run this string to make the file executable – if you don’t do this step the program won’t run.

    sudo python

    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:


    import time, os


    #Make sure we can import the GPIO library
    import RPi.GPIO as GPIO


    except RuntimeError:

    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
    GPIO.setup(22, GPIO.IN)


    #Set up a function to check the state of the GPIO pin
    def waitcheck():


    print ("When you're ready, please press the button. . . ")
    while (loop):


    #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
    #Exit this program if control-c is pressed

    except KeyboardInterrupt:

    #Restore GPIO to default state