How to build projects using the Raspberry Pi camera

No readers like this yet.
author photos with filters

The Raspberry Pi Foundation. CC BY-SA 4.0. 

The Raspberry Pi camera module is a great accessory for the Pi—it's great quality, and can capture still photos and record video in full HD (1080p). The original 5-megapixel camera module was released in 2013, and a new 8-megapixel version was released in April this year. Both versions are compatible with all Raspberry Pi models. There are also two variations—a regular visible light camera, and an infra-red camera—both available for US$ 25.

The camera module is high spec and much better quality than a basic USB webcam. Its feature-packed firmware fully utilizes the power of the VideoCore GPU in the Raspberry Pi SoC, allowing recording 1080p video at 30fps, 720p at 60fps, and VGA resolution (640x480) at 90fps—perfect for slow-motion playback.

Get started

First, with the Pi switched off, you'll need to connect the camera module to the Raspberry Pi's camera port, then start up the Pi and ensure the software is enabled. Locate the camera port on your Raspberry Pi and connect the camera:

Dave Jones, CC BY-SA

Ensure the camera software is enabled in the Raspberry Pi Configuration tool:

screenshot

Test your camera by opening a terminal window and entering raspistill -k. This will show you a camera preview on the monitor. If you're connected via SSH or VNC, this will be shown on the Pi's monitor, not yours. Press Ctrl + C to exit the preview.

Python

Although you can control the camera using the command-line interface raspistill, using the Python picamera module is much easier and allows you to change the camera controls dynamically in real time—ideal for projects.

Open the Python 3 editor, IDLE, create a new file and type the following code:

from picamera import PiCamera
from time import sleep

camera = PiCamera()

camera.start_preview()
sleep(3)
camera.capture('/home/pi/Desktop/image.jpg')
camera.stop_preview()

Now run the code and it should show the preview for three seconds before capturing a photo. The photo will be saved on your desktop, and you should see an icon with a thumbnail appear right away. Double-click the icon on your desktop to see the picture.

You can manipulate the camera object in various ways. You can alter the brightness and contrast with values between 0 and 100: camera.brightness = 70 camera.contrast = 40 You can add text to the image with: camera.

annotate_text = “Hello world"

You can alter the image effect with:

camera.image_effect = “colorswap"

Also try out effects, such as sketch, negative, and emboss. A list of effects is provided in camera.

IMAGE_EFFECTS, which you can loop over and makes a great demo:

camera.start_preview()
for effect in camera.IMAGE_EFFECTS:
    camera.image_effect = effect
    camera.annotate_text = effect
    sleep(5)
camera.stop_preview()

There are many more attributes you can alter, such as resolution, zoom, ISO, white-balance modes, and exposure modes. See the picamera documentation for more details.

Video

Recording video is just as easy—simply use the methods start_recording() and stop_recording():

camera.start_preview()
camera.start_recording('/home/pi/video.h264')
sleep(10)
camera.stop_recording()
camera.stop_preview()

Then play back using omxplayer. Note the video may play back at a higher frame rate than was recorded.

Infrared

The Raspberry Pi infrared camera (Pi NoIR) was made especially because people were buying the regular camera and taking it apart to remove the infrared filter—with varying success—so the Foundation decided to produce a special camera without the infrared filter. The API works exactly the same, and in visible light, pictures will appear mostly normal, but they can also see infrared light, allowing capturing and recording at night.

Pi camera

This is great for wildlife cameras, such as the Naturebytes kit, projects like the infrared bird box, and various security camera projects. The IR camera has even been used to monitor penguins in Antarctica.

Also the camera can be used to monitor the health of green plants.

Pi Zero

When the $5 Pi Zero was announced last year, it did not feature a camera connector due to its bare bones minimalist nature; however, last month a new version of the Zero was announced, which added a camera port.

The connector is smaller than the regular one. In fact, the same connector is used on the compute module, but a cable can be used to connect a camera. Both spins—visible and infrared—and both versions (V1 and V2) work with the new Pi Zero.

More ideas

There's plenty more to read up on what you can do with the camera module, and why not tie in with some GPIO for more physical computing projects?

User profile image.
Ben is a software engineer for BBC News Labs, and formerly Raspberry Pi's Community Manager. He's into Linux, Python and all things open source! Follow Ben on Twitter @ben_nuttall.

3 Comments

Thanks for the article! -- can the pi zero also record 1080p 30fps? I can't seem to dig that info up anywhere.

I am investigating the Pi platform for a DIY security system... any reason not to use the Pi Zero (+USB wireless) instead of the Pi 3?

Thanks for any ideas!

Yes it can.

No reason your project wouldn't work on a Pi Zero, assuming you don't need the additional CPU power.

In reply to by CC (not verified)

This will be too time-consuming to be fully addressed here so I am looking for assistance in an automotive application to replace external GoPro style devices as well as dash cams. Here is my synopsis. If you have external cams mounted on the vehicle and are on public streets, in my area at least, law enforcement takes an interest in you like you are on Street Outlaws, and i have seen people in urban downtown Seattle run off the side walk, rip them off the vehicle and good luck because about zero chance is what you have for seeing thar pricey black edition GoPro ever again.

Similarly, if you are involved in an accident, a dashcam or GoPros may save your ass or slit your throat, Unfortunately, it will not be your choice since they will likely be seized at the scene, and along with your car's 'black box' they will know if you were wearing your seat belt, speed, steering angle, if you attempted to brake before the accident, and newer cars with heads up displays have eye tracking, and services like OnStar are always listening so anything you say can be held against you in court.

I want to get a system of small CMOS cameras, 2 in the grill(1 daylight & 1-night vision or infra-red) + one in each door facing the opposing window to record what goes on if pulled over etc, and lastly a day/night set in the bumper or spoiler facing rearward. I want 1080p, RsPBpi or Arduino control, some type of ln-cabin control to activate the system, swap between day/night cams etc, and sdhc or SSD storage for at least 6hrs with all cams active. Any thoughts or resource links? TIA!.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.