This instructable describes the building of a USB powered and controlled set of lava lamps from a USB keyboard and two Think Geek USB lava lamps using a simple transistor relay switch circuit driven by the keyboard LEDs. It illustrates a simple way to control up to three high-latency switches (at up to 120volts, 0.5 amps) over a USB connection, for a minimal amount of money (0$ to 40$ depending on your junk collection), and without having to program a USB serial control driver or a microcontroller. Potentially you could also use the keyboard keys to add over 100 inputs that would type characters that could be used to control a program (as described by randofo and by David Merrill, but this approach was not used for this project.
This project was conceived as a present for my friend Chris Lasher to commemorate his moving and beginning a graduate program in bioinformatics. Chris will be doing a lot of programming, and is an adherent of Pragmatic / Agile Programing, one of the principals of which is unit testing. While the unit tests of a program are running, it is nice to know if they are succeeding or failing, and which is predominant. Famously, some pragmatic programmers made a feedback system by controlling full-sized lava lamps from a unit testing framework by plugging together some off-the-shelf home control devices (it cost them over $60). The lamps allowed them to see whether tests were passing or failing, and the gradual heating up on one lamp allowed them to see which events were predominant. This is a cool idea, but I wanted to make Chris something that:
1) would control two small USB-powered lava lamps,
2) would be powered off Chris's fancy new laptop, and interface via USB (no legacy ports on newer computers, particularly laptops),
3) would be a bit more homemade and hacker-friendly,
4) would be allot cheaper.
Driving relays from a USB to serial port converter would seem to be a simple solution. There are many circuits available on-line for serial port control projects. One excellent project that I adapted was at http://www.windmeadow.com/node/4. Unfortunately, almost all USB to serial devices do not allow for low level bit banging (the manually toggling of control lines) that is required to use these circuits. Another approach would be to buy a USB interface circuit, or build one from a microcontroller. Either way, the project would require a good deal of microcontroller code and drivers (even using the pre-existing human interface drivers or a pre-made controller).
I chose to solve this problem with a salvaged USB keyboard, since they are relatively cheap, and contain everything that is needed for three simple on/off outputs, in the form of the number, caps, and scroll lock LEDs. Under Linux there are a number of mechanisms to toggle your LEDs from a program or command line (one is even referenced in Cryptonomicon). Setled is the simplest, and is pre-installed on almost all machines, ledd is built on a client-server model, and there is even a module in the Ruby programing language. Windows and Mac OSs, and other programming languages, should have comparable mechanisms - post them in the comments if you know how to toggle keyboard LEDs in other OSs or programming languages!
I gave Chris his present, and now he can not only see if his unit tests are passing, all the programmers in his new department can know what a cool DIY maker he is.
Step 1: Parts List
2 USB lava lamps (from Thinkgeek or froogle ~$10 each)
1 USB keyboard
(an apple keyboard would also allow you to salvage two USB jacks, making the hackery listed in the "poor man's USB jack" step unnecessary)
$0 for a scraped keyboard with a dead key or two, to $4 on-line (http://froogle.google.com/froogle?q=usb%20keyboard) to $30 at best buy)
2 reed switch relays (Radio Shack #275-233 $3.00, Mouser #677-OMR-112H $1.54)
all other electrical components should only cost a few dollars.
2 transistors (like a 2N2222) (Radio Shack #276-1617, Mouser #511-2N2222A)
2 resistors (Radio Shack #271-1124, Mouser #291-4.7k, yellow-violet-red-gold bands)
4 diodes, something like 1N404 (Radio Shack #276-1103, Mouser #512-1N414B)
1 power switch (SPST)
a little bit of wire
1 perfboard (Radio Shack #276-1395)
1 Altoids tin
some cardboard, a sheet of transparency/overhead film
soldering iron and / or wire wrap tool and wire
dremel tool or hand-tool and patience
drill and bits
a digital multimeter
red and green sharpie marker
Knowledge and skills:
Basic soldering and electronics knowledge. I highly recommend Forest Mimm's book if you are an electronics beginner and would like to know how circuits work.
This project took two evenings and an afternoon, including troubleshooting.
Step 2: Keyboard Evisceration
Take your keyboard apart as nondestructively as possible. The keyboard I had lying around was a Dynex multimedia USB keyboard, model number DX-MKB101. As you disassemble, be careful not to break the thin wires that are often used. You should see something like the second picture after opening the keyboard. You want to salvage the body of the circuit and the board with the LEDs (often on the same circuit board) and lose the bulky keyboard. Hopefully you can detach the thin plastic film that acts as the key switches for the keyboard, or in some other way cut off the keyboard switches without damaging the main circuit. I had to cut through some plastic to liberate the board, but after finishing you should be able to plug the keyboard in, have it recognized, and toggle the bare LEDs with another, functional keyboard's lock keys pluged into the same computer. You can have multiple keyboards, USB and not, plugged in simultaneously with most modern operating systems and they will trigger each other's caps lock and other lock keys. At this point I should say that it is possible to damage or destroy your computer by plugging bare circuits into it. Use caution and proceed carefully. If you are worried test with a cheap USB hub. On the other hand, I tried all sorts of things while troubleshooting this circuit, including rewiring live circuits, and my laptop remains unfried.
Step 3: Poor Man's Usb Plug
I did not want to destroy the USB plugs on the lava lamps so that they could be used separately, and so that the USB lava lamp controller was modular (other devices could be used switched on and off instead of the lamps). If you disassembled an apple keyboard in the last step you would have have two female USB-A jacks that you can use, and so can skip this step. You could also purchase the female USB-A jacks from mouser or salvage ones from a spare USB hub. I chose however to go with a cheap and simple option - a simple USB jack made from some striped wire wrap (any thin wire would suffice), a bit of perfboard, and some paper and transparency film for insulation. Simply cut a bit of perfboard to go INSIDE the USB plugs, add wire through the holes so that inserting the plug will not pull them out and will allow them to contact the +5V and ground connectors in the plug (see the pinouts on the "Make circuit" step for which connection is which). Then cut some transparency film to cover one side of the board so the perfboard wire will not short out against the metal body of the plug. Jam this insulated board into the USB plug, and confirm with your multimeter that the wires make a good contact and are not shorting. These are how the USB lava lamps are connected to the circuit. For this project, make two of these poor man's plugs head-to-head on the same bit of perfboard (see the image). During the assembly step, you will see how the case holds both of the USB plugs on this connector.
Step 4: Cut Enclosure and Perfboard
Figure out the layout of your project in it's enclosure. I used the ever-popular Altoids tin. Drill a hole in the back of the Altoids tin for the large on/off SPST switch, one in the front for the keyboard USB cable, and two holes in the sides for the USB lava lamp plugs. Cut keyhole slits with a dremel cut-off wheel or handsaw down to the front and side holes so the USB cables can fit in (see picture). Line holes with duct tape to prevent cutting of cables. Decide on the shape of your perfboard for your circuit and cut out that shape with a dremel tool. This is going to be a pretty tight fit . . .
Step 5: Make Circuit
The heart of this project is simply using software to toggle the keyboard LEDs, and hijacking the LEDs to switch larger currents. I should mention here that some LEDs are switched on by applying a positive voltage to the anode, while other circuits (like the one in this keyboard) float the LED anode high (so the LED is off) and tie the cathode of the LED to ground when it is activated (pull-down switching). You could modify this circuit to work with either, but I will diagram the negative tie-down method. The LED being turned off (set high) turns on a transistor switch, which closes a relay, and finally the lava lamp is activated. This means that the software toggle for the lamp will be reversed from the LED. I adapted the circuit from windmeadow, but it is based on a simple transistor switch as described in most electronics texts (p. 50 in Mimm's book, or on wikipedia). I have included a ktechlab file so you can see how the transistor switch works, and a full schematic (in svg and png formats). Be sure to use the original schematic or the svn file for printing out, as the smaller images are not clear.
You will need to remove the LEDs from the keyboard circuit board and determine their polarity and which side is switched with a multimeter. Run one wire from each LED that will be involved in a switching circuit (these are labeled SCROLLLOCK and CAPSLOCK on the schematic). Desolder the +5v and ground from the keyboard circuit to provide power to the perfboard, and then run wires to return power to the board (this is the +5V and grounds on the schematic). Then follow the schematic to make the circuit, using the pinouts of the components to identify connections. Assemble this circuit on a breadboard first, and test the circuit. This was the step where I determined that the LEDs were pull-up instead of pull-down switched, and generally reached the point of maximum chaos on my workbench. When the circuit works (testable by hitting the Caps Lock Key or Scroll Lock Key and seeing if the lava lamps toggle), figure out how to pack the components onto the perfboard. Wirewrap can be used, but the simplest assembly method is to simply shove components through holes, wrap the leads together, and solder them together. Test that the assembled circuit works on the perfboard.
Step 6: Color USB Lava Lamps
Make the clear USB lava lamps colored by coloring a patch of the overhead film with the red or green markers, then cutting out a circle to insert in the base of the lamp between the light and the "lava" chamber. Reassemble lava lights.
Step 7: Final Assembly
Altoids tins make great enclosures. They are good at shielding, but they unfortunately are conductors and will short unprotected circuits. To prevent this, cut cardboard and transparency film to the size of the tin, and to fit between the keyboard circuit board and your perfboard. Insert switch in the back hole in your tin. Insert USB plugs mated to the poor man's USB connector into the keyhole cuts in the side so that the tin holds the plugs into the connector. Cram in perfboard and salvaged keyboard circuit, insulate with the cardboard and transparency film. Try to close the tin (ours barely fit).
Step 8: Code
The lava lamps will be controlled by system calls to the "setled" program that will turn on and off the capslock and scroll lock LEDs. Test it out by running the following from a command line (possible as root, depending on your configuration) to make your lamps blink:
setleds -L -caps +scroll < /dev/tty0 ;sleep 3s ;setleds -L +caps -scroll < /dev/tty0; sleep 3; setleds -L </dev/tty0
breaking this down:
#To turn on the red lamp:
setleds -L -caps +scroll < /dev/tty0
#to turn on the green lamp:
setleds -L +caps -scroll < /dev/tty0
#to return to the state where the LEDs display the keyboard settings
setleds -L </dev/tty0
You can include these system calls in your own program, or use the a phython unit test module that Chris wrote based on the PyUnit unit testing framework built into python. Just unpack the files from the tar archive (tar -xzvf lava_unittest.tar.gz from a console) and try the following commands from a console:
python lava_unittest.py test_lava_unittest-pass
python lava_unittest.py test_lava_unittest-fail
To run from a terminal emulators, uncomment the warning-flagged lines and try running it
as root from a terminal emulator.
With this code, all one needs to do is swap instances of "unittest" with "lava_unittest" in Python scripts written for unit testing. No re-coding is necessary.
Step 9: Pretty Packaging
To finish this project, make and print out your own label or use the included image (open the full-size secong image or the xcf file with the gimp). Simply paste on the cover of your amazing USB controlled lava lamps.