Raspberry Pi Zero W as a headless time-lapse camera

ReadItLater Article

Raspberry Pi Zero W as a headless time-lapse camera

tl;dr: There are many ways to capture time-lapse videos. But this one is cheap, completely wireless, and mine. If you want to skip the post and go straight for the glory, grab a copy of my Time-lapse app for the Raspberry Pi.

Time-lapses transform subtle, slow processes into something beautiful, and often make us think about things in new ways. For example, have you ever thought about just how heavy a wet snow is? The trees in your yard might know a thing or two about that! Check out a time-lapse I recorded this morning some mighty oak tree branches, as they relaxed upward as if in relief from the wet snow falling off:

Quality time-lapse photography used to require expensive equipment special knowledge, so it remained the domain of the specialist. Fast forward to 2017, and we have a $10 computer and a $30 camera that can create a wireless time-lapse device that can produce footage in any modern resolution—even up to 4K! Sure there are many cameras with built in intervalometers and lenses you can use to make more creative images... but would you be willing to leave your expensive equipment in places or situations where you risk damaging them? $50 of equipment, maybe... $1000+, no way! (At least, not for fun and exploration.)

The Raspberry Pi has freed me to think in new directions for computing (for example, check out my Raspberry Pi Dramble project), and it's also allowing me to expand my photography horizons. And if you want to tinker as well, all you need is this little guy:

Raspberry Pi Zero with Camera Cable and microSD card
The Raspberry Pi Zero W.

For this year's Pi Day (3.14), I decided to grab a Raspberry Pi Zero W from Micro Center, write up a small app, and record time-lapses. And I've decided to open source all my work, and share this blog post so you, too, can enjoy watching life in fast-forward!

Requirements

There are some programs you can download that are more automated, but (a) they usually require the use of a keyboard and monitor (which we don't want to use), and (b) they usually consume more power (meaning less battery life) and are less customizable than I desire.

Here are the things I wanted to have:

So for starters, I made sure I had the right hardware on hand. I'll run through my parts list, then explain the setup process below.

Parts needed

Raspberry Pi Zero W Time-Lapse portable AUKEY battery pack

Setting things up

I'll assume you're using a Mac for the setup process, but you can do the same thing on Windows if you use Ubuntu Bash or some other command line emulator that allows SSH access... and if you use Linux, you're golden—just pop open a Terminal window and follow along!

In this guide, we'll set up the Raspberry Pi headless—you don't need an extra monitor, keyboard, and mouse to plug into the Pi (nor do you need all the extra adapter cables to get it all hooked up!)

Put Raspbian on the microSD card

  1. Mount the microSD card (I use a microSD to SD adapter, then plug that into my USB SD card reader) on your Mac.
  2. Download Raspbian Lite (either as a .torrent or the direct .img download).
  3. Open Terminal, run diskutil list to see all connected volumes.
  4. Note the microSD card path (usually /dev/disk2 or /dev/disk3).
  5. Unmount the microSD card: diskutil unmountDisk /dev/disk2
  6. Write the image you downloaded to the card: sudo dd if=~/Downloads/2017-03-02-raspbian-jessie-lite.img of=/dev/rdisk2 bs=1m (should take a minute or less if you have a decent microSD card)

Initialize SSH and WiFi configuration.

  1. Open the boot volume on your Mac (should auto mount after the disk image is finished writing).

  2. Create an ssh file to tell the Pi to enable SSH when it boots up by default: touch /Volumes/boot/ssh

  3. Create a wpa_supplicant.conf file to tell the Pi to connect to your WiFi network on boot. Create a new file with that title in the boot volume, with the contents below:

    network={
        ssid="YOUR_SSID"
        psk="YOUR_PASSWORD"
        key_mgmt=WPA-PSK
    }
    
  4. Eject the microSD card, and stick it in the Raspberry Pi.

At this point, the Pi is fresh and new, and will boot up and connect to your WiFi network, allowing you to administer it via SSH.

Connect to the Pi

Assuming your WiFi network uses DHCP to assign IP addresses to devices (this is almost universally true), you need to figure out the IP address your Pi acquired when it booted. Use one of the following two options:

# Use nmap.
$ sudo nmap -sP 10.0.1.1/24

# Use Fing.
$ brew install fing
$ sudo fing 10.0.1.1/24

(Note: Check your computer's local network IP address—if it's something like 192.168.0.x, then you need to use 192.168.0.1/24 instead of 10.0.1.1/24.)

Either of these options will scan the network for a bit, then output a list of Host addresses and MAC/Hardware addresses. nmap additionally prints human-readable manufacturer labels, so it's even easier to identify devices labeled with (Raspberry Pi Foundation) on your network!

Once you have found the Pi's IP address, log into it: ssh pi@[IP-ADDRESS-HERE] (the default password is raspberry). Since this is the first time the Pi is being used, it needs to be configured:

  1. sudo raspi-config
  2. Set a new password (first option in the list).
  3. Set a hostname (e.g. pi-zero-timelapse).
  4. Go to 'Interfacing Options', then 'Camera', then choose 'Yes' to enable the camera interface.

Raspberry Pi Zero raspi-config Terminal SSH configure hostname
Aren't the graphics amazing?

Create your own Time Lapse script

Now that the Pi is set up, and you're connected to it, you need to install a few libraries so you can call them in your Python time-lapse script:

sudo apt-get install -y python-picamera python-yaml imagemagick

Then create a script named timelapse.py (nano timelapse.py to open it in the nano text editor), with the contents:

from picamera import PiCamera

camera = PiCamera() camera.capture('image.jpg')

This is basically how the guide in Raspberry Pi's official documentation works (Time-lapse animations with a Raspberry Pi). But at this point, we want to go a bit deeper, and have a more flexible way to control the timelapse—the length, the exposure settings, color temperature, etc.

To that end, I've built a little Python app (really, it's a glorified script... but nowadays everyone calls anything resembling software an 'App', so I might as well, too) that you can download from GitHub to have greater control over your time-lapses!

Going deeper

With the pi-timelapse app, you can build timelapses like the one at the top of this post pretty easily. Some of the features I've built so far include:

Here's how to set things up:

  1. Change directories into the pi user's home directory: cd ~ (the tilde means 'home', which is /home/pi in this case).
  2. Download the project: git clone https://github.com/geerlingguy/pi-timelapse.git
  3. Change directories into the pi-timelapse directory: cd pi-timelapse
  4. Create your configuration file: cp example.config.yml config.yml (this copies the example file to config.yml).
  5. Configure the time-lapse: nano config.yml (in the nano editor, Ctrl-O saves ('writes out') the file, and Ctrl-X exits).
  6. Start a time-lapse: python timelapse.py

After the number of frames you configured have been captured, there will be a folder named series-[date-and-time] in the directory with the pi-timelapse project. That directory contains all the images that were captured, numbered in a sequence like image00001.jpg, image00002.jpg, etc. And if you configured a gif or video to be created, then you'll see a .gif and/or .m4v video in the pi-timelapse project directory too.

You can use ls to display the contents of the directory, and cd [folder] to go into a folder, or cd .. to go back one directory.

At this point, if you want to view these things, you'll either need to use scp to copy a file from the Pi to your computer, or use an FTP client that works with SFTP (I use Transmit, but Cyberduck is a great, free alternative).

More Examples

Here's a video I shot at 1 frame every 15 seconds of cirrus clouds in the sky in front of my house:

And a shot of an earlier snow melting (as it was falling!) in my backyard:

And here's a gif I captured of myself, showing one of the risks of working at an adjustable-height standing desk:

Standing Desk Problems - Animated Gif

I haven't had time to build any more elaborate time-lapses, mostly because they often take hours (or days!), depending on what I'm trying to capture, and I've still been honing the software in the short term.

Summary

Grab the Raspberry Pi Time-Lapse App from GitHub, and start making some time-lapses of your own!

My next steps are:

And if you're interested in getting into the nitty-gritty, check out the contents of the timelapse.py script, and read the official picamera module documentation for a ton of useful background information.