This is the first post at quantixed about Raspberry Pi computing.
Pi Zero is a minimalist Raspberry Pi that can be coupled to a camera. With this little rig, you can make time-lapse footage amongst other things. I’ve set up a couple of these now. One was to make a time-lapse movie of some plants growing through a plastic maze. The results were pretty good and I thought I’d upload the video and a brief how-to guide.
After a delay, you can see four beans sprouting and then one eventually makes it to the top of the maze. This footage was shot over 27 days. The Pi took pictures every 5 min, but I sampled at 10 min in order to make the movie (after discarding the pictures after the sun went down). Everything was automated.
The camera shoots at 3280 × 2464. I downsampled the images to make the video. The camera didn’t focus well on the maze which was a bit too close. Other units are shooting scenery and the autofocus on the unit is great.
How I did it
Pi Zero with camera module (without IR filter) and a case are available for around £40. I bought mine from the Pi Hut. Power supplies and SD cards are readily available. I put together the PiCam with a fresh Raspbian full image on a 16GB SD card. Another option is to use a smaller card and get the Pi to save the images to a server.
I used PiBaker to format the SD Card, load on Raspbian and add a startup script that would connect the Pi Zero to WiFi and enable VNC. That meant I could plug it in and start using it headless. Well in theory! It turns out that VNC via Mac does not work with the UNIX style password which is the default on the Pi. I needed to connect to a monitor to rectify this by changing to VNC password in the VNC GUI. After this I could log in and use the Pi Zero remotely.
A few more minor steps were needed for full functionality:
- I enabled ssh and camera port in Raspberry Pi Configuration, disabled bluetooth and set the correct timezone (this can probably be done in PiBaker but I forgot).
- Since I have several Raspberry Pis on the LAN. I needed to give this one its own identity to prevent network conflicts.
- I needed to set up SMB sharing on the new Pi.
Instructions for how to do these things are just one google search away.
Now the Pi was ready to start taking images. I built a little stand for it out of Lego and set up the plant maze.
Taking pictures with the Pi
I wrote a shell script to take pictures using raspistill.
I made a directory called camera in home/pi
Then made a camera.sh file home/pi that looked like this:
#!/bin/bash DATE=$(date +"%Y-%m-%d_%H%M") raspistill -o /home/pi/camera/$DATE.jpg
Then I made it executable
chmod +x camera.sh
Using CRON, I execute the shell script on a schedule. I wanted to take pictures every 5 minutes. You can consult cronguru for your needs.
*/5 * * * * /home/pi/camera.sh 2>&1
That’s it! The Pi Zero will happily take pictures until you tell it to stop. Or there’s a…. crash.
Dealing with crashes
If you are going to do long-term time-lapse imaging, you need to defend against a crash that will prevent images from being acquired. In the worst case, the Pi could go offline and you wouldn’t know until you checked up on it. The first one I set up crashed quite often. I couldn’t determine the cause immediately. So I did the next best thing.
I set up a watchdog to monitor for crashes and then reboot the Pi if/when it happens. Many guides online suggest bcm2708_wdog but this doesn’t work for a Pi Zero. Instead this worked for me:
sudo modprobe bcm2835_wdt sudo nano /etc/modules
Adding the line “bcm2835_wdt” and saving the file
Next I installed the watchdog daemon
sudo apt-get install watchdog chkconfig chkconfig watchdog on sudo /etc/init.d/watchdog start sudo nano /etc/watchdog.conf
I uncommented two lines from this file:
- watchdog-device = /dev/watchdog
- the line that had max-load-1 in it
Save the watchdog.conf file.
There are guides online that describe how to set up the Pi so that it sends you an email or SMS when there’s a crash/reboot. I figured I didn’t need this – as long as it reboots OK.
Well, you wait for it to take photos! You can log in via VNC and check that the images are being acquired, or go in via ssh and watch the camera directory fill up. The size of the images is 3280 × 2464 and they are around 4.5 MB each, so the disk can quickly fill.
After a while you’ll want to assemble a movie. I wrote a shell script on my Mac in order to to pull down the images, take a copy of the ones I want and then make a movie file and upload it to Dropbox so I could look at it on the go.
#!/usr/bin/env bash # move to the location of the images cd /local/disk/folder2/ # pull down all images to a local folder - only new images are copied rsync -trv /Volumes/HOMEPI/camera/ /local/disk/folder/ # overnight images are dark and less than 1.5 MB # copy the ones we want to keep rsync -trv --min-size=1000K /local/disk/folder/ /local/disk/folder2/ # or you could filter on size like this - delete <2MB find . -name "*.jpg" -size -2000k -delete # scale the images down to 480 px wide and make movie ffmpeg -framerate 30 -pattern_type glob -i '*.jpg' -c:v libx264 -pix_fmt yuv420p -vf scale=480:-2 out.mp4 # move to dropbox mv out.mp4 /My/Dropbox/Folder/out.mp4
This script means that I had to manually delete the pictures from the Pi once they’d been copied but that was OK. My plan is to write a script to do this for the longer running projects so that it is automated.
While it is possible to make the movies on the Pi itself, I did it on the Mac as that computer is beefier and is not busy taking pictures every 5 min! ffmpeg is a great tool for this and the documentation is impressive. For example if you have set up the camera in the wrong orientation you can do transposition in ffmpeg. If you don’t have ffmpeg, it is a simple install on the command line.
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null brew install ffmpeg
Hopefully this guide is useful to you. The Pi Zero Camera can be used for streaming video as well as taking a series of still images. I’m planning to test this out soon.
The post title “Experiment Zero” comes from the title of the album by Man or Astro-Man?