Skip to content

Raspberry Pi Camera

Published: at 05:06 PM

Introduction

Setting up a Raspberry Pi camera is an interesting project that opens up many possibilities, from simple photography to advanced home automation and security systems. If you’re a beginner exploring the capabilities of your Raspberry Pi or an experienced maker looking to add a new dimension to your projects, this guide will walk you through the entire process. We’ll cover setting up the Raspberry Pi camera module, configuring the software, and capturing your first image or video. By the end, you’ll have a fully operational camera system ready to capture photos, videos, and timelapses.

TL;DR

You can find a shorter cheat sheet version of this article here.

Table of contents

Open Table of contents

Requirements

You will need:

Software Installation

First, let’s prepare your Raspberry Pi, the easiest way to install the operating system on the microSD card is to use the Raspberry Pi Imager. Raspberry Pi documentation has a great guide how to install the OS on your Raspberry Pi. Remember to customize your installation by configuring Wifi and enabling SSH service so you can access the device over the network (no need to connect display, keyboard etc).

rpicam-apps

Raspberry Pi provides a small set of CLI applications, called rpicam-apps, which are built on top of libcamera to capture images and videos from a camera.

Recent versions of Raspberry Pi OS come with the five rpicam-apps listed above, allowing you to capture images and videos using a camera even on a fresh Raspberry Pi OS installation.

Photo

Take a full resolution JPEG image, and save it to a photo.jpg file in Pictures directory use the command:

rpicam-still -o ~/Pictures/photo.jpg

You can also disable preview:

rpicam-still -n -o ~/Pictures/photo.jpg

The rpicam-still command can save images in multiple formats, for example png or bmp. To capture a full resolution PNG image, use -e png option:

rpicam-still -n  -e png -o photo.png

Flip/rotate

Depending on the camera position, you may want to flip the image horizontally and/or vertically using --vflip and --hflip parameters:

rpicam-still -n -o ~/Pictures/photo.jpg --vflip 1 --hflip 1

You can also use the --rotation 180 to rotate the image:

rpicam-still -n -o photo.jpg --rotation 180

Note that currently only two rotation values are supported: 0 and 180.

Output file names

To use a current date as a file name use --datetime parameter:

rpicam-still -n --datetime

The output file name will be: MMDDhhmmss.jpg - 2 digit month number, 2 digit day number, hours (24-hour format), minutes and seconds.

If you prefer a Unix timestamps, use --timestamp option:

rpicam-still -n --timestamp

To have more control over file name, you can use an environment variable to get the time and combine it with file name:

DATE=$(date +"%Y-%m-%d_%H%M")
rpicam-still -n -o photo-$DATE.jpg

Timelapse

You can capture images at the specified time interval for a given period of time:

rpicam-still -o image%04d.jpg --timeout 100000 --timelapse 5000

This command will capture an image every 5 seconds for 100 seconds.

Timelapse with crontab

Create timelapse-cron.sh file in chosen directory, for example: ~/bin/:

#!/bin/bash

DATE=$(date +"%Y-%m-%d_%H%M")
rpicam-still -o ~/Pictures/timelapse/$DATE.jpg -n

Edit cron for current user, use crontab -e and type the line below:

* * * * * ~/bin/timelapse-cron.sh 2>&1

This will execute our timelapse-cron.sh script every minute.

To take an image more often, use the --timelapse option in the bash script, for example, take a picture every 10 seconds:

#!/bin/bash

DATE=$(date +"%Y-%m-%d_%H%M")
rpicam-still -n -o ~/Pictures/timelapse/${DATE}_%04d.jpg --timeout 50000 --timelapse 10000

Video

The rpicam-vid command allows you capture a video on Raspberry Pi devices.

To create a 30 seconds video, using H.264 encoding:

rpicam-vid -t 30s -o test.h264

To use motion JPEG format, execute command:

rpicam-vid -t 10000 --codec mjpeg -o test.mjpeg

Conclusion

Congratulations! You’ve successfully set up your Raspberry Pi camera, opening the door to a wide range of creative and practical projects. Whether you’re using it for photography, video recording, time-lapse projects, or even integrating it into a larger home automation system, you now have the tools and knowledge to explore these possibilities. Remember, this is just the beginning — there’s a vast community of Raspberry Pi enthusiasts out there, continually developing new ways to use this versatile device. Keep experimenting, learning, and most importantly, have fun with your Raspberry Pi camera!

References