Off-The-Shelf Hacker: ‘Hello World,’ Raspberry Pi Style
Last week we covered a simple input interface to a LibreOffice Impress slideshow using a couple of general purpose input/output (GPIO) pins on the Raspberry Pi.
This time, we’ll look at the output side with the traditional “Hello, World” program of microcontrollers and Nanolinux devices, blinking an LED. In our case, we’ll actually light up an LED with Python. The LED is housed in an old up-cycled stove-hood bulb and gives an interesting Steampunk “ozone tube” effect. Graphic 1 shows the tube as part of my Steampunk name badge. The LED is hooked up to the Pi using a 40-pin connector salvaged from an old PC hard-drive cable.
Throughout this series, I’ll encourage budding hardware hackers to learn new skills and try new things.
One important skill is getting into the habit of collecting parts. I use various bins and storage containers to collect screws, bolts, washers, nuts and other kinds of small hardware. Another bin might contain old circuit boards. Another might hold Steampunk parts like old gears, watches, copper fittings, brass hinges, and so on. You get the idea. With a well stocked “junk box”, getting that perfect “look” for some one-off, attention-grabbing project is just so much easier.
Building An “Ozone Tube”
I build projects to support my tech conference speaking habit, frequently using a Steampunk theme. The “ozone tube” was built from an old up-cycled stove-hood bulb and creates an old-timey, scientific aesthetic.
The first step is to clean out the bulb.
Use a grinder or side cutters to remove the center solder spot and glass insulator on the socket end of the bulb. Don’t remove the entire metal screw part of the bulb, because it makes it much harder to get everything solidly back together.
Next, use a small screwdriver to gently break the glass filament support out of the inside of the socket end, trying not to damage the bulb part. I used a leather glove to hold the bulb, so I wouldn’t cut my hand, just in case. Use long-nose pliers to crack and pull out parts of the glass center support. Once, the filament is out, you can use a little carbide burr, in a Dremel tool, to enlarge the hole. You’ll need a big enough hole to slide the LED into the glass envelope.
The LED has four leads, three that go to digital input/output pins on the Raspberry Pi and the other one that goes to ground. I soldered a 100 ohm resistor between each LED lead and GPIO pin wire to limit the current to about 20 milliamps. The LED was then glued to a sliver of quartz to create a Steampunk “radioactive ozone crystal.” Graphic 2 shows the “crystal,” resistors and support tube.
The whole works are then attached to a little brass tube and inserted into the hollowed out glass bulb. It’s easy enough to fashion a bracket for the bulb out of 1/4-inch wide brass flat stock and some small screws. The wires lead to the 40-pin connector. See graphic 3 for the wiring diagram.
Once the ozone tube is ready, we can turn our attention to lighting it up.
Light the Tube with Python
Controlling output pins on the Pi is much the same as reading input pins. Here’s a small Python program I modded from a script by Alex Eames at RasPi.tv.
# !/usr/bin/env python2.7
# script by Alex Eames http://RasPi.tv
# Using PWM with RPi.GPIO pt 2 - requires RPi.GPIO 0.5.2a or higher
# always needed with RPi.GPIO:
import RPi.GPIO as GPIO
# pull in the sleep function from time module
from time import sleep
# choose BCM or BOARD numbering schemes. Use BCM
# set GPIO 17 (pin 11) as output for blue led
# create object blue for PWM on port 17 at 100 Hertz
blue = GPIO.PWM(17, 100)
# start blue on %25
# now the fun starts, we'll vary the duty cycle to
# dim/brighten the leds, so one is bright while the other is dim
pause_time = 0.2 # you can change this to slow down/speed up
for i in range(0,21): # 101 because it stops when it finishes 100
blue.ChangeDutyCycle(20 - i)
for i in range(20,-1,-1): # from 100 to zero in steps of -1
blue.ChangeDutyCycle(20 - i)
blue.stop() # stop the blue PWM output
GPIO.cleanup() # clean up GPIO on CTRL+C exit
The program begins with referencing the libraries and initializing the general purpose input/output (GPIO) pins connected to the LED. In this example, I just used the color blue to keep the script simple. Change the pin number to activate the red (GPIO 22, pin 15) or the green (GPIO 27, pin 13) segments of the tri-color LED.
I chose to start the blue LED at 25 percent brightness and set the LED change rate at 0.2 seconds.
Next, a loop starts that steps the LED down and up through its duty cycle.
Lastly, if we get a from the keyboard, the program resets the GPIO pins and exits.
Now you have one example of pulsing an LED up and down with a Raspberry Pi. Varying the on and off time, as well as speed, will give different effects. The frequency (100 Hz) is slow enough that the blue light flickers slightly, which is perfect for my “radioactive ozone crystal” Steampunk effect.
You could also control a motor through a motor driver circuit, close a relay to handle a bigger electrical load, or even run servos.
Next week, we’ll do a quick tutorial on soldering.