October 2016

Using a Raspberry Pi for Timelapse Photography

A couple of years back my daughter picked a science project that involved growing beans in varying magnetic fields. I thought it might be interesting to film the beans growing over a period of weeks. An obvious way to do this was to take advantage of the Raspberry Pi camera. High resolution photos could be taken at a regular interval (like once every two minutes) and pushed to the cloud over WiFi as they were taken. They could then be composed into a timelapse movie.

So this mostly broke down into how to aim the Raspberry Pi camera nicely and how to code the software. At the time, I'd been using a nice injection-molded case for the Pi which has a mount for the camera board on the front and two screw slide slots on the rear. The best setup was thus to make the Pi mountable on a tri-pod. This was accomplished by using a small block of wood where I used two tapered wood screws on one side to match up with the slots on the back side of the case. I then used a 1/4" coupler in combination with a 1/4" bolt to fashion a connector to the tripod screw on the other side. The 1/4" bolt is counter sunk into the wood so that it doesn't interfere with the case mount. This worked quite well for allowing the camera to be positioned precisely and to be held very still with a tripod for the several weeks it was filming.

The simple Python routines I used on the Pi are on GitHub. The timelapse.py program did the capture -- making a connection to AWS to prepare to store each photo in an S3 bucket. The program just invokes raspistill to take the photo and then codes the day/time into the key name as it's pushed to S3. The loop then pauses until it's time for the next shot (in my case, 2 minutes).

When we were growing the beans we used grow lights that were on for 12 hours and off for 8 (on the first go around we were using a Quirky WiFi power strip for the lights but at that time their cloud service had frequent outages such that it wasn't sufficiently reliable and we went back to a low-tech timer setup). We ended up taking photos while the lights were off as well (a modification to the program might sync with the lights so as not to do that). As a result, we cleaned up the images by removing the ones with a small file size (which corresponded to the essentially pitch black night images). Since we used ffmpeg to convert the frames to an mpeg video, we also used code to produce the sequential frame-numbered files that we then read in to make the mpeg file.

Overall it worked fairly reliably but occasionally over the period of weeks the Pi might hang up and need to be restarted. I used the Pi's watchdog capability to try to catch such circumstances and force a reboot (the program was designed to start again on launch). This was not entirely successful and still froze at least once with the watchdog in place. These days, an easy way to get a notification if it's not working would be to tell S3 to post an event to an SQS queue when a photo is successfully taken. Then craft an AWS Lambda script to periodically trigger and ensure that the SQS queue has some entries before clearing it. If no entries are present in the queue, send yourself a notification that the Pi has stopped transmitting to the cloud.

We went through a couple of 2 week trials of the bean growing experiment -- and strong magnets did have some effect on the growth patterns. But my daughter also wished she'd picked something that produced results a bit quicker. Watering and measuring the beans became a daily chore for weeks. A video of our first timelapse trial is included below (jump to about 1:00 to see the growth beginning).