Introduction: Track How Far Your Hamster Runs
Step 1: Get a Hamster and a Wheel
Step 2: Gather Materials
1. DB9 Female DSUB Solder Connector
This should plug into the serial port on your computer. There are a number of different types of these connectors but the cheap variety that you can get at your local electronics store should be sufficient. They even make non solder varieties if you want (but I would recommend just learning how to solder).
2. Magnetic Contact Switch
The easiest way to get one of these is to use one that's meant for windows in a home security system. It is important to get one that is "normally closed" so that when you bring a magnet nearby it "closes" and allows current to flow between the screws. Note that they have versions that do both "normally closed" and "normally open" (as mine below) which will have three screws.
Please don't take one off your house. In general I found I had to go to an electronics store like Fry's Electronics to find these. They didn't have them at the Home Depot I checked. These are also available online at http://www.smarthome.com/7113.html.
3. Magnets
You'll need a magnet to set off the contact switch every time the wheel goes around. Two magnets are ideal since you can put them on other side of the wheel and secure them without any glue or tape (better for the hamster). I used hard drive magnets because they're strong (neodymium rare earth metal magnets) and I happened to have a few laying around.
4. Switch Mounting Supplies
I used zip ties a pen (with the ink removed of course) and hot glue to fasten the switch securly and out of reach of the hamster. More details on this in later steps.
Step 3: Gather Tools
1. Soldering Iron (and some solder of course)
2. Wire Cutters
3. Multimeter (to check your solder joints)
4. Helping hands (to hold the wire while you solder)
5. Glue Gun (to keep everything together)
If you really want, you can probably do without the multimeter and the helping hands. However, if you're on this site you could probably find significant use from these later.
If you don't yet know, I would suggest learning how to solder.
Step 4: Choose Some Wire
During my first iteration of this project the hamster chewed through my shoddily protected, thin wire a number of times before I switched to the heavy gauge wire. Now the hamster has managed to strip each wire but since the copper is so thick (like the bars of a cage) the system still works. Don't worry, the voltage going through these wires is very low and the hamster is perfectly safe.
To solder the serial port connector to the wire it may be nice to get some significantly smaller gauge wire to act as an intermediary. I cannibalized an Ethernet cable for this purpose.
Step 5: Assemble the Sensor
If you're using the same materials as me you can use the zip ties combined with some hot glue to attach the the contact switch to the pen case. Once the switch is secured to the pen, attach the wires using the screws.
Step 6: Choose the Right Contacts
Step 7: Solder the Serial Port Connector
Pin 4 on a serial port is also known as "DTR" or Data Terminal Ready. Pin 6 is also known as "DSR" or Data Set Ready. When we write our program we'll be able to set DSR to +5V and check if DTR is +5V. As a result we will be able to determine whether or not the magnet is over the switch. And if we check very quickly then we'll be able to figure out how often and how quickly the wheel turns to determine both number of rotations and speed of each rotation.
Step 8: Measure Your Wheel
circumference = pi * diameter
Use Google to get the circumference into your favorite units of distance.
Step 9: Assemble the Wheel
Make sure that when you attach the sensor assembly and the magnet that they are close enough for the switch to trigger but that the wheel still spins freely. Also ensure that the switch is facing the correct direction. When you purchase the contact switch, many times they will specify the minimum distance to the magnet. If you use a stronger magnet (like a neodymium hard drive magnet) you may have a little more leeway.
If you listen carefully you can usually hear a click every time the wheel goes around. If you don't hear that click it might be worth checking your switch / magnet configuration to ensure everything is aligned correctly.
Step 10: Install the Wheel
If your computer is too new to have a serial port you can buy one for cheap on Amazon or at your local electronics store. If you're running some variety of Linux or Mac I would recommend this one from Keyspan that will work well.
Step 11: Write the Program
Now remember that we've connected DTR (pin 4) to DSR (pin 6) on the serial port through a switch that is activated every time the hamster runs once around the wheel. We can write a pretty quick program in Python that will let us set DTR and monitor DSR for when the switch is activated.
Note that I've chosen Python in this tutorial since its pretty easy to learn and use. If you're comfortable with another language and it has a library for accessing the serial port, I would suggest going with that as it will give you more flexibility with what you want to do later.
Assuming you're using Python you'll need to install a couple of things
- Python : Follow the instructions for installation on the type of system you will be using. If you haven't had too much programming experience it might be worth running through a couple tutorials. However, I have provided some code listed below.
- pySerial : To access the pins on the serial port reference above you'll need to to install this library. This allows you to "import serial" so you can connect to the serial port.
Once you have these installed, here's some code you can use to record the distance your hamster runs. I've tried to comment it as thoroughly as possibly so even beginner programmers know what's going on.
In addition to recording and printing the distance every time the wheel turns, it will write the speed and time of each revolution to a file. If the program ever crashes then you can count the number of rows and multiply that number by the circumference. Once you've downloaded the script you can run the program from the command line like this and it will write each rotation of the wheel to "progress.txt".
python hamster-serial.py progress.txt