Our family recently grew with the addition of two sister gerbils, Snuggles and Gingy. A week later an exercise wheel was bought, and the Arduino possibilities started to multiply.
The objective of this little project was to see how far and fast the gerbils could run; hundreds, or thousands of meters? Could they outrun us bipeds?
Update (Version 2.0):
Now the gerbils’ daily stats are posted at midnight on some clever ePaper, using Pimoroni’s Inky pHat and a Raspberry Pi. Total distance and the top speed from the last 24 hours are displayed. A handy histogram shows when Snuggles and Gingy have been at their most active.
There are a couple of ways to do this, but this project uses an infrared break beam sensor (because I had one handy). Each time the exercise wheel rotates, a Lego brick glued to its periphery breaks the beam and a rotation is logged. Measuring the time between each rotation means speed as well as distance can be calculated.
The results are posted to both the Arduino IDE serial monitor and an LCD screen. A little extra coding was required to avoid false readings that suggested either Snuggles or her sister had a top speed of 30mph (48kph)!
Update (Version 2.0):
The Arduino is now coded to post data to a Raspberry Pi every hour via serial communication. An Arduino is much better at timing critical tasks than the Pi. Whereas the Pi will enable some pretty cool internet of things possibilities; tweeting stats and virtual Gerbil runs…
I used some old Technic Lego to construct a mounting for the IR LED and sensor. The LED and sensor housing included some handy holes for screwing them into a couple of two-stud Lego bricks. A third two-stud brick was glue gunned to the outer diameter of the exercise wheel.
The code uses an interrupt routine to capture each instance of the IR beam being broken. This calls a function, BeamBreak(), that calculates the distance for each rotation based on the circumference of the wheel and the speed using the time since the last full rotation. The speed is compared to the previous maximum to determine Snuggles’ top speed. The results are posted to the serial monitor, printing distance, speed and maximum speed.
The columns of data printed to the serial monitor are labelled periodically by the Header() function. Distance and maximum speed are also posted to a 2 x 16 LCD by employing an easy to use Adafruit library.
The IR sensor I have used pulls the output low (0 volts) when the beam is broken. So to read this input, the sensing pin should be pulled high (pinMode(IR_SENSOR, INPUT_PULLUP)). The interrupt pin is then configured to trigger on a falling signal.
The first iteration of the code resulted in some incredulously high speeds. This was cause by some bounce in the signal. The sensor was being activated twice in a fraction of a second. Probably due to vibration of the gerbil cage as Snuggles accelerated to her maximum velocity! Cutting and pasting the data from the serial monitor to Excel and plotting the data helped with finding a solution.
The errant readings created large spikes in the speed recorded, that were easily discernible from the more believable data. The sensible looking chunks of data showed that the gerbils’ maximum readings was 3mph. By adding an ‘if’ statement to filter out jumps in speed greater than 3mph, the false readings were eliminated.
The Arduino is coded to respondto data request from a Raspberry Pi over a serial connection. Each hour, the Pirequests the distance and max speed recorded by the Arduino, which poles forserial commands. At the start of each day, the Pi commands the Ardunio to resetthe distance and maximum speed variables. The distance run each hour is storedin an array so that it can be plotted on histogram.
The day’s stats are displayed on a Pimoroni Inky pHat tri colour ePaper. The gerbil graphic is part of aback ground png file on top of which distance and maximum speed are printed.Python Image Library is used to create the histogram with a series of draw.line commands. The Python code is included in a dollared-out section at the end of Arduino code below.
So how fit are my gerbils? Well, between them, they are currently running over 1km a day and reaching a top speed just shy of 7mph. Which one runs the furthest and fastest remains a mystery. Has anyone written some gerbil face recognition code?