From charlesreid1

(Redirected from Blink Raspberry Pi)

This page covers a basic blinking hello world circuit on the Raspberry Pi.

You, too, can do this: https://vimeo.com/134905876

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:

PiRTG.jpg

and a close-up of the GPIO cable:

PiGPIORTG.jpg

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

PiSSHRTG.png

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.

The pin numbering can be a bit confusing. There are TWO sets of pin numberings: the first set of pin numbers corresponds to the pin numbers printed on the GPIO breadboard adapter: Pin 4, Pin 17, Pin 22, Pin 24, Pin 25, and so on. But these are NOT the actual pin numbers that you'll use in your GPIO code.

For the pin numbers you'll use in your GPIO code, the pins are numbered in pairs: the first pair at the top are 1 and 2; the next pair down are 3 and 4; and so on.

PiPinoutCorrect.png

That means that the pin labeled "P22" on the GPIO breakout board is not actually referred to as Pin 22 in your GPIO script. It's actually Pin 15.

The reason for all of these pins is this: 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 "P22", or pin 15, 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

The Python Code

Now we're ready for our Python code. This one sets up the GPIO input/output to control a pin, then sends out "SOS" in morse code by setting the high and low voltage on one of the pins:

import time

try:
    import RPi.GPIO as GPIO
except RuntimeError:
    print("Error importing RPi.GPIO! This is probably because you need superuser privileges. You can achieve this by using 'sudo' to run your script")

GPIO.cleanup()

# Set the mode to board,
# so we can use RPi stuff
GPIO.setmode(GPIO.BOARD)

channel = 15
GPIO.setup(channel, GPIO.OUT)

interword_delay = 0.2
word_delay = 0.4

def three(delay):
    # turn the light on and off three times
    for i in range(3):
        GPIO.output(channel,GPIO.HIGH)
        time.sleep(delay)
        GPIO.output(channel,GPIO.LOW)
        time.sleep(interword_delay)

    time.sleep(word_delay)

def s():
    three(0.1)

def o():
    three(0.3)

while True:
    print "SOS"
    s()
    o()
    s()
    time.sleep(word_delay)

GPIO.cleanup()

Building Hello World Circuit

Now we're ready to assemble the circuit.

We'll be turning pin 15, labeled "P22", on and off with the Python script, so that's where we'll start our circuit.

We run a wire from P22 into a resistor, to step down the voltage. We then run that into an LED, and finally, we connect the circuit to ground.

Here's what the breadboard diagram looks like:

PiHelloWorldCircuit BB.png

and the assembled circuit:

PiHelloWorldCircuit Photo.jpg

Putting It Together

Once you've done this, you've got all the pieces you need to run your Hello World script.

To run it:

1. SSH to the Raspberry Pi from your computer

2. Run the GPIO script as root

3. Sit back and enjoy the show

Final Result: Raspberry Pi Hello World

Next Steps

This project was very caveman in nature.

The next step was using a better morse code library. See the RaspberryPi/Morse_Code page. Morse code was used for hardware control via software onboard the Pi.

Flags