RaspberryPi/Blink: Difference between revisions
From charlesreid1
| Line 112: | Line 112: | ||
There are instructions on installing the library manually over at Raspberry Pi Spy http://www.raspberrypi-spy.co.uk/2012/05/install-rpi-gpio-python-library/ | There are instructions on installing the library manually over at Raspberry Pi Spy http://www.raspberrypi-spy.co.uk/2012/05/install-rpi-gpio-python-library/ | ||
Basically: | Basically, you download the package on your laptop and copy it over to the Pi: | ||
<pre> | <pre> | ||
| Line 118: | Line 118: | ||
[laptop] $ scp RPi.*.tar.gz pi@169.254.113.200:~/. | [laptop] $ scp RPi.*.tar.gz pi@169.254.113.200:~/. | ||
</pre> | </pre> | ||
Then you install it manually on the Pi: | |||
<pre> | <pre> | ||
Revision as of 01:16, 30 July 2015
This page covers a basic Hello World circuit on the Raspberry Pi.
Background
Before we get started, there are a few things to know about the Raspberry Pi - things that are similar to Arduino, and things that are different.
First, what's different? The Raspberry Pi has a full-stack Linux operating system running onboard, and its microcontroller is much more flexible. The Arduino, on the other hand, can only run compiled C code sketches, and of limited size. The Raspberry Pi is thus much more powerful.
What's similar? The Raspberry Pi can also be used for building circuits, using the Pi's GPIO bus. This is the large set of parallel pins that you see onboard the Pi. You can connect those GPIO pins to a breadboard to build a circuit, and you can then use Python scripts to control the GPIO pins.
What You Need
You'll need a Raspberry Pi with the Raspbian image. The Pi should be connected to your laptop via a network cable. It should have Python installed on it. See RaspberryPi/Installing and RaspberryPi.
What we're going to do is remotely connect to the Raspberry Pi and write a Python script that will control the Raspberry Pi's GPIO bus to turn the light on and off.
The Procedure
Put Hello World Code on Raspberry Pi
Step 0: Modify SD Card
Before getting started, we need to modify the boot sequence of the Pi so that we can connect to it remotely via SSH. Plug the SD card into your laptop, mount it, and edit /Volumes/SD CARD/cmdline.txt. This file contains the boot sequence of the Pi. Add to the end ip=169.254.113.200, so that the final file looks like this:
dwc_otg.lpm_enable=0 console=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait ip=169.254.113.200
This IP address is a way of manually assigning the Raspberry Pi an IP address that will work with a crossover cable (a cable connected directly from the Raspberry Pi to the laptop). For more info see RaspberryPi/Installing.
Step 1: Plug Network Cable and GPIO Cable In
You'll want to plug a network cable into the Raspberry Pi and into the laptop.
Next connect a GPIO cable to a breadboard using a breadboard-GPIO adapter. In a pinch, you can also use female-to-male wires (placing the female end on the tip of the particular GPIO pin of interest, and placing the male end into the breadboard), or plug wires into the female end of the GPIO cable.
Your setup should look like this:
and a close-up of the GPIO cable:
Step 2: Boot and Connect to Raspberry Pi
Now connect your Raspberry Pi to power. Give the Pi a minute to finish it's boot sequence. Now you should be able to ssh to your Pi:
$ ping -c 4 169.254.113.200 $ ssh pi@169.254.113.200
I've modified my prompt, so yours will probably look different.
You're now on-board your headless Raspberry Pi!
Step 3: Picking a GPIO Pin
We want to use Python to control the GPIO. Specifically, we want to control a voltage on a pin on the GPIO, the same way we would control the voltage on a pin on the Arduino. We'll connect that pin to the LED and resistors for our Hello World circuit.
DesignSpark has a schematic showing pinouts for the GPIO. It's really confusing and very busy. Compare that with the much cleaner RaspberryPi.org GPIO schematic:
| DesignSpark GPIO Pinout Diagram | RaspberryPi.org GPIO Pinout Diagram |
|---|---|
Basically, what it boils down to is, some of the pins are for power, some of the pins are for signaling, and some of the pins serve special purposes. The layout is a bit scattered. All we need to do is turn a voltage on and off, so we don't need any special pins.
Let's use pin 22, since the diagram shows that it's open and available.
Step 4: The Python GPIO Code
We're finally ready to control the Raspberry Pi GPIO pins from a Python script. We know what pin we want to control. Now we install and invoke the Python GPIO library.
If You Have rpi.gpio
The RPi GPIO library should come standard on Raspbian images, but you can check to make sure you've got it anyway. In aptitude, this package is called python-rpi.gpio:
$ apt-cache show python-rpi.gpio Package: python-rpi.gpio Source: rpi.gpio Version: 0.5.11-1 Architecture: armhf Maintainer: Ben Croston <ben@croston.org> Installed-Size: 174 Depends: libc6 (>= 2.13-28), libgcc1 (>= 1:4.4.0), python (>= 2.6.6-7~), python (<< 2.8) Homepage: http://sourceforge.net/projects/raspberry-gpio-python/ Priority: optional Section: python Filename: pool/main/r/rpi.gpio/python-rpi.gpio_0.5.11-1_armhf.deb Size: 45508 SHA256: b8e69b7eead05797378628b814f36e2af788a1abfaf32f1a175233766d3bea20 SHA1: fea86e3a5aebde912066c9d42039ff11a4b42a62 MD5sum: 074cbe6da329fec1f4a4993319987285 Description: Python GPIO module for Raspberry Pi This is the RPi.GPIO Python module, for supporting GPIO on a Raspberry Pi
If You Don't Have rpi.gpio
If you don't have the python-rpi.gpio package, you won't be able to get it with apt-get, since the crossover cable does not allow you to share an internet connection with the Raspberry Pi. (Unless you want to make things really complicated.)
There are instructions on installing the library manually over at Raspberry Pi Spy http://www.raspberrypi-spy.co.uk/2012/05/install-rpi-gpio-python-library/
Basically, you download the package on your laptop and copy it over to the Pi:
[laptop] $ wget https://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.5.11.tar.gz [laptop] $ scp RPi.*.tar.gz pi@169.254.113.200:~/.
Then you install it manually on the Pi:
[pi] $ cd ~ [pi] $ tar xzf RPi*.tar.gz [pi] $ cd RPi* [pi] $ sudo python setup.py install