|Story location: Home / computing / raspberry_pi /|
Several years ago I had the idea of using a camera or light sensor to measure how fast a hamster runs in a wheel. I never actually did anything about it but I recently saw a fidget spinner in action and decided to measure its speed using LEDs and a photodiode.
The circuit was incredibly simple. At first I thought I might need to connect the photodiode to an amplifier or schmitt trigger or something like that but when I pointed the LED directly into the photodiode and attached the voltmeter to the latter, the voltage went close to zero when I blocked the light and went over 3v when I let the light through. It looked like I could just connect the photodiode directly to a GPIO pin of the Raspberry Pi to measure when the light path was blocked.
The next step was to write some code to detect when the GPIO pin changed state and to measure the frequency. When I logged into the Pi, I found a script which actually did exactly that. I'm not entirely sure where this came from. I may have adapted it from a C exmaple from the pigpio project but I can't actually remember. It uses NumPy to calculate the average.
Wiring it up
Connecct the LED to the 3.3V output of the Pi, through a 220 ohm resistor. Connect the photodiode 'in reverse': the long leg to GPIO 26 of the Raspberry Pi and the short leg to the 3.3v supply. When the light level is low, the photodiode will act like a normal diode and block the current. When the light level is high, the current will flow and the GPIO pin will detect a 'high' signal.
I fired up the edge.py script, started the figet spinner and lowered it into into the light beam. I had to be careful to avoid it touching the breadboard, the LED or the photodiode so I almost certainly didn't manage to spin it as fast as I could. I ran the script a few times and got numbers between 30-50 Hz. Since the spinner had 3 lobes, this means that it was only spinning between 10 and 15 times per second.
Matt Parker has a video where he uses a spectrum analyser to measure the frequency of the spinner. He uses a compressed air can to reach much higher speeds.
This afternoon we had a go at assembling the 'Nanoblock' Koala which Emma bought in Japan. It was very small and fiddly and we both felt relieved when we finally finished it.
We had the secret santa present givaway this morning. Last week we had to draw a name out of a hat and buy a present for that person. I got a Bedlam Cube. I made the mistake of taking it out at lunchtime. I can't get it back in the box now!