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
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:
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:
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:
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
Servo Motors
The position of a servo motor is set by the length of a pulse. A servo expects to receive a
pulse roughly every 20 milliseconds. If that pulse is high for 1 millisecond, then the servo angle
will be zero, if it is 1.5 milliseconds, then it will be at its center position and if it is 2 milliseconds
it will be at 180 degrees.
Image from Adafruit's Raspberry Pi Lesson 8. Using a Servo Motor by Simon Monk
Adafruit and Sean Cross have created a module called
PWM and Servo
The
PWM and Servo Module uses a file type of interface, where you control what the output pin
(i.e. the servo) is doing, by reading and writing to special files. This makes it
easy to interface with using Python.
The files involved in using the module to drive a servo are listed below. All the files can be
found in the directory
/sys/class/rpi-pwm/pwm0/
on the Raspberry Pi.
File | Description |
active | This will be 1 for active 0 for inactive. You can read it to find out if the
output pin is active or write it to make it active or inactive. |
delayed | If this is set to 1, then any changes that you make to the other files will
have no effect until you use the file above to make the output active. |
mode | Write to this file to set the mode of the pin to either pwm, servo or
audio. Obviously we want it to be servo. Note that the pin is also used
by the Pi's audio output, so you cannot use sound at the same time as
controlling a servo. |
servo_max | Write to this file to set the maximum value for a servo position. We will
set this to be 180 so that we can easily set the servo to anu angle
between 0 and 180. |
servo | The value that you write to this file sets the servo pulse length in
relation to servo_max. So if we set it to 90, with a servo_max of 180, it
will set the servo to its center position. |
GPIO pin 18 is the only pin that can produce PWM. Servos require 4.8-6V DC power to the
motor, but the signal level (pulse output) can be 3.3V, which is how its OK to just connect the
signal line directly to the GPIO output of the Pi.
Image from Adafruit's Raspberry Pi Lesson 8. Using a Servo Motor by Simon Monk
Here is the Python program to make the servo sweep back and forth is listed below:
# Servo Control
import time
def set(property, value):
try:
f = open("/sys/class/rpi-pwm/pwm0/" + property, 'w')
f.write(value)
f.close()
except:
print("Error writing to: " + property + " value: " + value)
def setServo(angle):
set("servo", str(angle))
set("delayed", "0")
set("mode", "servo")
set("servo_max", "180")
set("active", "1")
delay_period = 0.01
while True:
for angle in range(0, 180):
setServo(angle)
time.sleep(delay_period)
for angle in range(0, 180):
setServo(180 - angle)
time.sleep(delay_period)
The while loop will just just continue forever or until the program is interrupted by pressing
CTRL-C.
If you want to make the servo move faster, try changing delay_period to a smaller value:
0.001. Then to slow it down try increasing it to 0.1.