Wednesday, October 26, 2011

Measuring How Far Your Hamster Runs

In a previous post I talked a lot about how far my hamster ran. You can measure this yourself too. I've written up an Instructable with step by step instructions but I also wanted to give an overview here.

Theory: Since my hamster did most of her running in her wheel, it made sense to count revolutions of the wheel and multiply that by the circumference. Being an avid cyclist, I knew of a device that already did this, my bike speedometer.

There are two important elements here, the magnet and the sensor. This sensor is rather simple, it's just a reed switch that is triggered every time a magnet comes near it. I just needed to construct something like this on the hamster wheel and hook it up to the computer.

Second, I needed to figure out was how to read the state of this switch from the computer. There are all sorts of ways to do this but I was looking for something quick, easy, and cheap. It turns out that the serial port on your computer has two pins DTR (Data Terminal Ready) and DSR (Data Set Ready) that'll be sufficient. In code, you can control the DTR pin and read the DSR pin. By shorting the two through the reed switch you can check whether or not it's on.

Construction: I found most of what I needed for this around the house.

In addition to a wheel and our hamster, I used:

  1. reed switch (from a home security system)
  2. DB9 female DSUB solder connector (i.e. a female serial port connector)
  3. magnet (from a hard drive)
  4. mounting supplies (pen, zip ties, glue)
  5. wire (thick so the hamster can't chew through it)

Putting this together was rather simple. I just attached the pen to the wheel with some zip ties and glue and then attached the reed switch to the pen.  I broke the magnet in half and used it to keep itself in place. After that I just needed to wire it up and connect each side of the switch to the DTR and DSR pins on the serial port connector.

Now I just needed to write some code. I did this in python but this can be written in any language with a serial port library.

import serial
ser = serial.Serial("/dev/ttyS1")
circumference = 0.000396 # miles
def waitForPinOff():
  while ser.getDSR() == 1:
    1 # Don't do anything while we wait.
def waitForPinOn():
  while ser.getDSR() == 0:
    1 # Don't do anything while we wait.
while 1:
  distance = distance + circumference

All this is doing is connecting to the serial port and turning on DTR (i.e. setting the pin to 5V). It then waits to see DSR turn on (i.e. the switch is closed) and increments the distance by the circumference every time that happens. There's a couple things you I did to clean up the data from there (and here's some better code for that) but that's basically it! Here's the final set up with Lizzie the hamster.

Technologies: Magnet, Reed Switch, Basic Electronics, Python, Hamster

Monday, October 24, 2011

Improved Traffic Light Bike Light

This projects was done a while ago (1/6/2010) but it's never too late to post!

The first iteration of this project worked quite well. However, it wasn't very compact because the blinking was done by an external Arduino board which was overkill, messy, and needed a separate power supply. So for the second iteration of this project, I decided to make the control circuitry contained entirely within the UPS.

Step 1: Make a blinker circuit. It turns out this is really easy to do with a simple 555 timer IC and some help online. I picked my resistors and capacitors to give this a duty cycle similar to other bike lights I owned. This is a picture of the circuit on a breadboard before I soldered it up to protoboard that shows how simple it is.

Step 2: Find power in the UPS. It didn't make any sense to power this thing externally when I was dealing with uninterruptible power supply with plenty of control circuity. The 555 timer I bought works with anywhere between 4.5V and 16V. So I turned the thing on and carefully probed measured various places on the board until I found one that was giving me 12 volts. After that, getting the electronics working was as simple as soldering it all together.

Step 3: Put it back together. Finally I was ready to make it look pretty. I taped up wires, put the 555 circuit in some extra space, and added a couple a couple doses of hot glue. Afterwards you wouldn't suspect that I had been in there are all. Success!

I did actually use this on a few rides to and from work and it worked splendidly if a bit heavy because of the lead acid battery. Especially at night, the traffic light is amazingly bright. A great next step would be ripping out all of the AC circuity and getting this working with some lighter batteries DC only.

Technologies: A/C Electronics, Basic Electronics, soldering iron