Track Your Hamster’s Daily Exercise with a Raspberry Pi Fitness Tracker
Curious about how much your hamster runs in a day? Our 9‑year‑old daughter sparked this project, and the results were both surprising and insightful. By combining a Raspberry Pi, a laser break‑beam sensor, and a simple script, you can effortlessly measure your pet’s distance traveled and speed.
What You’ll Need
- An internet‑connected Raspberry Pi (or any single‑board computer).
- A laser break‑beam sensor – Adafruit’s Laser Break‑Beam Sensor.
- A breadboard for wiring (Adafruit Pi Starter Kit includes one).
- An LED (optional, also in the starter kit).
- Two pull‑up resistors: 220 Ω and 10 k Ω (also in the kit).
- A hamster cage with a running wheel located outside the main living area to keep the laser away from the hamster’s eyes. Example cage.
- A hamster.
Step 1: Measurement Concept
The system works by detecting each full rotation of the wheel. A small target on the wheel reflects the laser beam once per revolution. The Raspberry Pi counts these events, calculates the distance (wheel circumference) and speed, and streams the data to a real‑time dashboard you can check every morning.
Step 2: Hardware Setup
The Adafruit laser break‑beam sensor is compact and requires no extra weight on the wheel – essential for a 2 oz. dwarf hamster. It contains both transmitter and receiver in a single housing. When the beam is interrupted by the target, the sensor outputs a signal.
- Wire the red lead to 5 V, the black lead to ground, and the blue lead to a Pi GPIO pin.
- Connect a 10 k Ω pull‑up resistor between the sensor output and 5 V.
- Attach an LED (optional) to the same GPIO pin for visual confirmation; use a series resistor (220 Ω) to limit current.
- Mount the sensor on the cage wall, ensuring the beam never points into the hamster’s eye area. If the wheel is inside the cage, a magnetic contact switch can replace the laser for safety.
- Place a small piece of masking tape on the wheel’s rim as the reflective target. Align the laser so it only hits this target during each rotation.
Safety Note: Never shine the laser directly into the living area or the hamster’s eyes. The beam can damage vision.
Step 3: Software Setup
To visualize the data, use Initial State’s real‑time dashboard. Follow these quick‑start instructions to install the Initial State streamer on your Pi.
Create a new Python file, e.g., hamster_fitness.py, and paste the following code:
import time
import initialstate
from gpiozero import Button
# Replace with your own client key
client_key = "PUT YOUR CLIENT KEY HERE"
bucket_name = "Hamster Fitness Tracker"
# Create a new stream in the specified bucket
stream = initialstate.Stream.create(bucket_name, client_key)
# Set up the sensor pin (change 17 to your chosen GPIO)
sensor = Button(17)
# Calculate wheel circumference (example: 10 cm radius)
wheel_radius_cm = 10
circumference_cm = 2 * 3.14159 * wheel_radius_cm
rotations = 0
start_time = time.time()
while True:
sensor.wait_for_press()
rotations += 1
elapsed = time.time() - start_time
distance = rotations * circumference_cm
speed = distance / elapsed # cm per second
# Send data to Initial State
stream.publish({"rotations": rotations, "distance_cm": distance, "speed_cm_per_s": speed})
print(f"Rotations: {rotations}, Distance: {distance:.1f} cm, Speed: {speed:.2f} cm/s")
time.sleep(0.1) # debounce
Each time the script runs, a new bucket named Hamster Fitness Tracker is created under your Initial State account, and all subsequent data points are stored there. The dashboard will update live, giving you instant insight into your hamster’s activity.
With this setup, you’ll discover just how active your furry friend really is, even while you sleep!
Manufacturing process
- Build a Raspberry Pi Temperature Logger with a $5 I2C Sensor
- Control an LED with a PIR Motion Sensor on Raspberry Pi
- Build a Low‑Cost Raspberry Pi Soil Moisture Sensor for Smart Irrigation
- Track Your Hamster’s Daily Exercise with a Raspberry Pi Fitness Tracker
- Build Your First IoT Project with Raspberry Pi, DHT11, and ThingSpeak
- Build a Raspberry Pi Home Temperature Monitor with MCP9808, InfluxDB & Grafana
- Transform Your RC Car Into a Bluetooth‑Controlled Vehicle
- Advanced Hiking Tracker with Arduino: GPS, Sensors & Battery Pack
- Transform Your IoT Idea into a Market‑Ready Product
- From Concept to Reality: Turning Creative Designs into Tangible Products