Turn your Raspberry Pi into a Hamster Fitness Tracker
Have you ever wondered just how much your hamster runs throughout the day/night? How far? How fast? My 9 year old daughter did, and we decided to find out. Turns out, it was pretty easy, and the results were unbelievable.
Here are the supplies you will need:
- An internet connected Raspberry Pi or your single-board computer of choice.
- A laser break beam sensor (such as this sensor from Adafruit – https://www.adafruit.com/product/2122).
- A breadboard for wiring up a simple circuit (such as the one found in the Adafruit Pi starter kit).
- An LED (optional, also found in the Adafruit Pi starter kit).
- Two pull-up resistors (one 220 ohm resistor and one 10K ohm resistor, also in the Adafruit Pi starter kit).
- A hamster cage with a running wheel that is outside the main living area (to avoid ever shining a laser into your hamster’s eyes). Here is an example cage.
- A hamster.
Step 1: The Simple Measurement System
The measurement system is very simple. The laser break beam sensor will be aimed at a small target on the wheel that will only reflect the beam once per full rotation of the wheel. The Pi will detect each time the wheel turns a full rotation and calculate the distance traveled and speed (the distance traveled is simply the circumference of the wheel). The resulting measurements will be captured and streamed to a data visualization that you can look at when you wake up each morning (because your hamster is probably getting his workout on while you sleep at night).
Step 2: Hardware Setup
The laser break beam sensor from Adafruit is a good choice for this project because of its simplicity to wire up and use. No extra weight will be added to the wheel to make it harder to spin for your 2 oz. dwarf hamster. Both the laser transmitter and receiver are built into the same small plastic housing. If the laser beam reflects off of a target within 1 meter or so, the receiver detects and outputs the break. This is much better than a traditional laser break sensor that requires a separate receiver that must be aligned to the laser (like your garage door sensor that can easily get off track if you touch it). The laser break beam sensor will have three wires to attach. Attach the red wire to 5V. Attach the black wire to ground. The blue wire will be the sensor output. Connect this wire to a 10K ohm pull-up resistor and to an input pin on your Pi as shown below. *Warning* Do not shine the laser into the living area of your hamster!!! You could cause damage to your hamster’s eyes. If your wheel is inside the cage, consider using a magnetic contact switch instead of a laser break beam sensor.
The LED will be used to visually indicate that the laser break sensor has detected a break. This is extremely useful when lining up the laser with the target and making sure that nothing unwanted is causing a light reflection into the sensor. Initially, I taped the laser break sensor to the wall, but enough light was reflecting off of the white wall to cause the sensor to always detect a break. Having the LED helped me find the best place to mount the sensor. You can hook the input of the LED directly to the output of the sensor (make sure you have a pull-up or pull-down resistor on your LED). I hooked my LED up to an output pin on my Pi in case I wanted to use the LED for something else in the project. Turns out, having the LED blink each time a laser break is detected is more useful than I anticipated. The entire hardware setup is shown above.
Create a target on the outside of the wheel for the laser. I used a small piece of masking tape. Align the laser to the target and make sure no other part of the wheel breaks the laser when it spins. Keep the laser out of any area that your hamster’s eyes can enter!!
Step 3: Software Setup
For the software setup, first install the Initial State streamer to give your hamster fitness measurements a destination to go to (go here for instructions on setting up the Initial State streamer; it is super easy and takes less than two minutes). Create a new file on your Raspberry Pi (e.g. “hamster_fitness.py”) and copy+paste the code below into it. You will need to copy+paste the client key associated with your Initial State account in between the quotes on line 6 where it says “PUT YOUR CLIENT KEY HERE”. You can find your client key under your account settings or on the landing page once you log into your account. The code below is all of the code that you will need.
Line 6 sets up the destination “bucket” for our data stream. We will name the bucket “Hamster Fitness Tracker”. Every time you run this script, a new bucket named “Hamster Fitness Tracker” will be created under your Initial State account (identified by your client key), and all data generated from that script run will be contained there. Line 7 streams the first message to the newly constructed stream.
For more detail: Turn your Raspberry Pi into a Hamster Fitness Tracker