In a previous post, I gave a really high-level overview of my Raspberry Pi camera, which I use to capture timelapse video, still images, and real-time video. That post was quite non-technical, just giving an overview of the hardware and the box that it’s mounted in. This post will give an overview of the software that runs on the RasPi itself, including step-by-step instructions that you can follow to build your own. In part 2 of this two-part series, I will discuss the other half of the software, which takes the captured JPEGs and converts them into MP4 video files.
As I mentioned in my earlier post, I plan to eventually create a menu-driven LCD user interface for configuring and starting the camera in the field. This is “Phase I”, though, and it has no explicit user interface, with the exception of using two separate flash drives to command the camera to transfer all captures to the drive, or to shut down. When the system boots up, it goes directly into “shoot-timelapse” mode, capturing one image every 2.4 seconds. I have the system configured to automatically mount flash drives when they are inserted. I have a simple BASH script that waits for a flash drive with a captures directory to be inserted (we will call this our “captures” flash drive). When it detects it, it automatically moves all captured timelapse directories to the drive, and then unmounts the drive. A second BASH script waits for a flash drive with a shutdown.txt file to be inserted (we will call this our “shutdown” flash drive). When it detects it, it savely halts the OS. By using these two flash drives, I can easily and automatically control the most basic operation of the camera in the field, with no monitor or network connection. If I want to shoot still photos or videos, I need to SSH in, but I rarely do that.
What you will need
Not much! Because this phase of the project has no user interface (besides using two flash drives as described above), all you really need is a Raspberry Pi, an SD card, a Raspberry Pi camera module, and a USB battery. Besides that, you’ll just need a case. I believe that there are some commercially made cases that accommodate the camera module, but I just used a second-hand cigar box for mine. My RasPi is a Model B, but you could also use a Model A, and probably get better battery life. The USB battery that I use is a 12,000 mAH model, which has worked wonderfully for me. I’ve used it for 12 straight hours on a few occasions. When you choose an SD card, you don’t need to get a speed demon, but try to get one that is as large as possible, to accommodate several hours of captured JPEG files. I use a 32 GB class 4 SDHC card.
Configuring the software
There’s not a whole lot to the software. I just use a few BASH scripts to command the Raspberry Pi Foundation’s raspistill program. Here’s a step-by-step procedure for configuring things:
- Start with a newly installed and prepared Raspbian installation. In a previous post, I go into some detail on how I provision a new Raspbian install.
- Physically install the camera module, as instructed in this brief video:
- use sudo to bring up raspi-config:
- Use the menus to do the following:
- Enable the camera module
- Change the amount of GPU memory from 16 MB to to 128 MB. This is needed because the camera requires use of the GPU.
- After making these changes, exit raspi-config, and allow it to reboot the RasPi.
- Using sudo, edit /boot/config.txt, and add these lines at the bottom:
start_file=start_x.elf fixup_file=fixup_x.dat # uncomment the following to disable the camera LED: # disable_camera_led=1
- Reboot again.
- Shoot a test still:
raspistill -o testimage.jpg
- Note that there is a 5 second delay, during which the video is displayed on the monitor, then the still image is taken.
- Verify that testimage.jpg gets saved into your current directory. Feel free to delete it.
- Shoot a test 20 second video:
raspivid -o testvideo.h264 -t 20000
- Verify that testvideo.h264 gets saved into your current directory. If you have your HDMI monitor connected, you can quickly play the video:
- For details on the raspistill and raspivid binaries, check out this doc: https://github.com/raspberrypi/userland/blob/master/host_applications/linux/apps/raspicam/RaspiCamDocs.odt
- Our BASH scripts use those two binaries, so it may be a good idea for you to familiarize yourself with them.
Our custom BASH scripts
Now that we know that our camera works, we can discuss our custom bash scripts. You can download all of these scripts from the links at the bottom of this post. Place them into, and execute them from, your /home/pi directory.
- This script creates a new timelapse directory as a subdirectory of /home/pi/captures/ , and invokes raspistill in timelapse mode. raspistill shoots a timelapse as a sequence of separate .JPG files in that new directory.
- Because my camera module is mounted upside down in my case, I prefix all of my JPEG files with “must-rotate-180-“. This naming convention tells my timelapse processing scripts (described in part 2 of this two-part series) to invoke imagemagick to rotate each JPEG file 180 degrees before further processing it.
- We invoke raspistill with settings that shoot one frame every 2.4 seconds. By converting to a 30 fps video, this gives one second of video for every 72 seconds of shooting time. If you wish to have a different shooting rate, experiment with the “-tl” argument to raspistill.
- This script creates a new /home/pi/captures/videos directory (if it does not already exist), and invokes raspivid on a new .h264 file.
- This script shoots video until you terminate it with (ctrl-C)
- This script creates a new /home/pi/captures/stills directory (if it does not already exist), and invokes raspistill to capture a single still JPEG image.
- This script invokes raspivid with a 2fps framerate. This is not nearly as flexible as the raspistill timelapse mode, and it does not save in full resolution, but it can be useful.
- This script enters an endless loop, waiting for the user to insert a flash drive with a captures directory. When detected, the script moves all of the captured timelapse JPEGs to the flash drive, and then unmounts the flash drive for safe removal.
- This script is similar to detect-automount-of-usb-then-move-files-then-umount-it , but instead of simply unmounting the drive and looping, it shuts down the RasPi after the JPEG files have been transferred.
- This script waits for the user to insert a flash drive with a shutdown.txt file. When detected, the script shuts down the RasPi.
- This script, and the detect-automount-of-usb-then-move-files-then-umount-it script, can be used to control the basic operation of the camera in the field.
Final configuration steps
We’re almost ready to start using our camera, but we have just a couple more configuration steps to follow:
- Follow the instructions in this post to create a /mnt/usb mountpoint that gets mounted automatically when a flash drive is inserted.
- Please note that you will need to make two entries in your /etc/fstab file, one for your captures flash drive, and one for your shutdown flash drive. The lines will be identical except for the UUID parameter.
- Please note also that, because we mount both drives to a common mount point, we need to tell udev to mount all removable devices, rather than /mnt/usb . So your /etc/udev/rules.d/99-mount.rules needs to look like this:
KERNEL=="sda", RUN+="/bin/mount -a"
- Add the following lines to your /etc/rc.local file, before the “exit 0” line:
sudo -u pi /home/pi/shoot-timelapse & sudo -u pi /home/pi/detect-automount-of-usb-then-move-files-then-umount-it & sudo -u pi /home/pi/detect-automount-of-usb-with-shutdown-command-then-shut-down &
- These lines are automatically executed at boot time. We are telling the system to run three of our scripts to automatically start shooting a timelapse, and to wait to process one of our flash drives.
- Finally, you will probably want to configure the system to disable the camera board LED. By default, an amber LED illuminates whenever the camera is active. Disable the LED by uncommenting the “disable_camera_led=1” line in /boot/config.txt .
Using the camera
There’s really not much to using the system. Power it up, and it will automatically boot up and start shooting a timelapse. Insert your captures flash drive, and all captured JPEGs will be transferred to it (part 2 of this two-part series will discuss other scripts which will make use of this same flash drive). Insert your shutdown flash drive, and the system will automatically halt in a safe manner. If you wish to shoot real-time video or individual still images, you will need to use SSH to execute the shoot-real-time-video and shoot-still scripts.
Here are the scripts, fresh from my own Raspberry Pi camera. Copy these into your /home/pi directory, and give them execute permissions. See the sections above for descriptions of these scripts.
- basic-mv-jpg-to-flash-drive (this is used by our detect-automount-of-usb-then-move-files-then-umount-it script)