Have you ever wondered how far your hamster runs each day? Most hamsters (like mine) love their wheels. What if there was a way to track how far your hamster ran in its wheel? Well there is! And all you'll need are a few dollars in parts that you can pick up at a hardware and electronics store and a computer. Follow this Instructable and you'll be tracking your hamster in no time!
Step 1: Get a Hamster and a Wheel
If you don't already have one get (1) hamster and (1) hamster wheel. I got a Syrian hamster named Lizzie and a "Wodent Wheel". Apparently this type of wheel is better for her back because of the relatively large radius.
Step 2: Gather Materials
Here's the list of things you'll need:
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.
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
You'll need a couple standard tools for this project.
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
The choice of wire for this project is important. No matter where you put the wire assume that your hamster will be able to reach it and therefore chew on it. As a result it is necessary to use fairly thick wire. I suggest 14 or 12 gauge solid electrical wire that you can buy at your local hardware store.
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
The next step is to put the contact switch on the mount and attach the wires to it. A large part of how this looks will depend on what you contact switch looks like and how you're attaching the switch to the wheel.
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
If your switch has three terminals make sure to pick the one that indicates normally closed. On the contact switch I used there was a diagram on the other side that showed which terminal was which.
Step 7: Solder the Serial Port Connector
Now attach the smaller gauge wires to the large wires and solder them to the serial port connector. You'll need to solder the wires to pins 4 and 6. Since we're just completing a circuit, it doesn't matter which wire goes to which pin just make sure you get 4 and 6. The connector you get will be labeled with the pin numbers and you can also see which pins you need to attach to below.
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
Since you're only measuring how many times the wheel has turned, you'll need to know the circumference of your wheel to get distance your hamster runs. In case you've forgotten your high school geometry, remember the equation
circumference = pi * diameter
Use Google to get the circumference into your favorite units of distance.
Step 9: Assemble the Wheel
Now you'll need to attach your sensor assembly to the wheel as well as attach the magnet. Here again I've used zip ties and hot glue to secure the pen casing to the wheel stand.
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
Now that you've completed the wheel, its time to install it. Place it in the cage and connect the connector to a free serial port on your computer. I used a serial port extension cable since the cable I made was too short to reach my computer on the floor.
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