Introduction



The Raspberry Pi has a GPIO (General Purpose Input Output) connector to which you can attach external hardware, like LEDs, I2C, SPI and Serial Rx and Tx. Some of the pins can also be used for PWM (pulse Width Modulation) for power control and another type of pulse generation for controlling servo motors called PPM (Pulse Position Modulation).

A popular way of connecting these pins is with the Adafruit Pi Cobbler
cobbler.jpg
Image from learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup

Adafruit has produced a collection of code to help you connect your Pi to other electronics. This collection includes simple Python libraries for a large number of modules, including displays, sensors and PWM controllers etc.

To fetch this code, you need software called git, which comes pre-installed on Occidentalis, your distro.

To ensure that you have the latest version of the distro, open LXTerminal and type:
sudo apt-get update
The update may take a while, especially if this is the first time you have run it on your Pi. When you get the command prompt you can type the following command to install git:
sudo apt-get install git
Once git is installed you can check out the Adafruit Pi Python repository onto your Pi using the following commands:
git clone http://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git
cd Adafruit-Raspberry-Pi-Python-Code
ls
Remember everything in Linux is case sensitive.

The GPIO pins can be used as both digital outputs and digital inputs. As digital outputs, you can write programs that turn a particular pin HIGH or LOW. Setting it HIGH sets it to 3.3V setting it LOW sets it to 0V. To drive an LED from one of these pins, you need a 1kΩ resistor in series with the LED as the GPIO pins can only manage a small amount of power.

If you use a pin as a digital input, then you can connect switches and simple sensors to a pin and then be able to check whether it is open or closed (that is, activated or not).

To program the GPIO ports in Python, you need to install a very useful Python 2 library called Rpi.GPIO. This module gives you a simple to use Python library that will let you control the GPIO pins.

The installation process for this is the same whether you are using Raspbian or Occidentalis. In actual fact, some versions of Raspbian include this library, but these instructions will also have the effect of updating to the latest version, which is worth doing:
sudo apt-get update
To install RPi.GPIO, you first need to install the Python Development toolkit that RPi.GPIO depends on.
sudo apt-get install python-dev
Then to install Rpi.GPIO itself type:
sudo apt-get install python-rpi.gpio
With the Pi connected to the Internet and SSH'ed in install the alsa audio drivers and MP3 Player
sudo apt-get install alsa-utils
sudo apt-get install mpg321
sudo apt-get install lame
Reboot the Pi ($ sudo reboot) and when it comes back up, load Sound Drivers and Setup for 3.5mm Jack Output
sudo modprobe snd_bcm2835
sudo amixer cset numid=3 1
To test for output:
aplay file_name.wav
speaker-test -t sine -f 440 -c 2 -s 1
mpg321 "file_name.mp3"
Set up your breadboard:

Source: Image by Mikey Sklar found on Adafruit site

You will need:
  • (3) 10k pull-up resistors
  • (3) Momentary push-button switches
Use GPIO pins 23, 24 and 25 where

Each button connection looks like:
3.3v --> 10k Pull-up Resistor --> GPIO --> Button --> GND

Source: Image by Mikey Sklar found on Adafruit site

Find a wav file:
find . -regex ".*\.\(wav\|aif\|wave\|aiff\)"


Find an mp3 file:
find / -iname "*.mp3" -print


Enter this command or use nano:
emacs raspi-audio-button.py
#!/usr/bin/env python
 
from time import sleep
import os
import RPi.GPIO as GPIO
 
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)
GPIO.setup(24, GPIO.IN)
GPIO.setup(25, GPIO.IN)
 
while True:
        if ( GPIO.input(23) == False ):
                os.system('mpg321 name_of_file.mp3 &')
        if ( GPIO.input(24) == False ):
                os.system('mpg321 name_of_file.mp3 &')
        if ( GPIO.input(25)== False ):
                os.system('mpg321 name_of_file.mp3 &')
        sleep(0.1);
$ chmod +x raspi-audio-button.py 
Run the python program as an administrator (with sudo). Press the button keys to hear the mp3 files play. Make sure you have speakers or headphones hooked up to the 3.5mm jack. sudo python raspi-audio-button.py



Configuring