Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 37 ยท Sensors and Cameras

Camera Module and picamera2

Connect a Raspberry Pi Camera Module, take photos and video with rpicam-still and rpicam-vid, and capture images from Python with picamera2.

A camera turns the Pi into a security monitor, a time-lapse rig, a wildlife recorder or a machine-vision project. The official Raspberry Pi Camera Modules connect directly to the board through a flat ribbon cable and are supported by an open-source camera stack called libcamera. This lesson shows how to connect a camera, take photos and video from the command line and capture images from Python.

Hardware and connection

Several camera modules exist: the Camera Module 3 (12 megapixels, autofocus, with a wide-angle version), the older Module 2 (8 megapixels, fixed focus), the HQ Camera (interchangeable lenses) and the Global Shutter camera for fast motion. USB webcams also work, but the ribbon-connected cameras are faster and give you more control.

To connect one:

  1. Shut down and remove power.
  2. Find the CSI camera connector (the Pi 5 has two, labeled CAM/DISP 0 and 1; the Pi Zero has a smaller connector and needs a different cable).
  3. Gently lift the plastic latch, slide in the ribbon with the contacts facing the correct way (the board's silk-screen or the connector shows it), and push the latch down.
  4. Boot the Pi.

Cameras are detected automatically on current Raspberry Pi OS, with no raspi-config step needed. Check the camera is seen:

Terminal
rpicam-hello --list-cameras
rpicam-hello -t 5000        # a five second live preview on a connected screen

On releases before late 2024 the tools were named libcamera-hello, libcamera-still and so on; the new rpicam-* names replace them.

Camera tools

rpicam-still Take a photo
rpicam-vid Record video
1 / 2

Photos and video from the command line

Take a photo and record a video (read-only; they need a camera):

Terminal
rpicam-still -o photo.jpg
rpicam-still -o photo.jpg --width 1920 --height 1080 -t 2000
rpicam-vid -t 10000 -o clip.h264        # ten seconds of video
rpicam-vid -t 10000 --codec libav -o clip.mp4

-o names the output file, -t is the preview time in milliseconds before capture, and --width and --height set the size. Many other options adjust brightness, exposure, white balance and focus. If you are on a headless Pi, add --nopreview.

Capturing from Python with picamera2

picamera2 is the Python library for the camera, installed by default on the desktop images (sudo apt install python3-picamera2 otherwise). A minimal program (read-only):

Python
from picamera2 import Picamera2
from time import sleep

picam2 = Picamera2()
config = picam2.create_still_configuration(main={"size": (1920, 1080)})
picam2.configure(config)
picam2.start()
sleep(2)                          # let exposure settle
picam2.capture_file("photo.jpg")
picam2.stop()

You can also get frames as a NumPy array for image processing, with picam2.capture_array(), and record video with start_recording. A time-lapse is just a loop with a sleep and a numbered filename:

Python
for n in range(120):
    picam2.capture_file(f"frame{n:04d}.jpg")
    sleep(30)

Join the frames into a movie later with ffmpeg.

Simulating a camera

A website cannot reach a camera, so the stand-in creates a small fake image as a grid of numbers and prints it, then runs a very simple "change detector" that compares two frames. That is the core idea behind many motion-triggered cameras.

Python Stand-in: a fake frame and motion detection

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Estimating storage

Video and images fill a card quickly. The stand-in estimates how much space a time-lapse or a recording needs so you can plan before leaving a camera running.

Python Stand-in: storage estimate

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Tips for good pictures

  • Let the camera run a second or two before capturing so the auto-exposure settles.
  • Use good light: tiny sensors produce noisy images in the dark.
  • Keep the lens clean and remove the protective film.
  • For a Camera Module 3, use AfMode or --autofocus-mode to control focus.
  • Mount the camera firmly; vibration blurs the image.
  • Remember privacy: tell people when a camera records them, and follow local laws.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which command takes a photo from the Pi camera on current Raspberry Pi OS?

    rpicam-still captures a still image and writes it to the file given by -o.
  2. 2. What should you do before connecting a camera ribbon cable?

    Never connect or disconnect the ribbon while the board has power.
  3. 3. Which Python library controls the camera modules?

    picamera2 is built on libcamera and is the supported camera library.
  4. 4. Why wait a moment before capturing a still photo?

    The first frames can be too dark or too bright until exposure adapts.

Summary

  • Official camera modules attach to the CSI connector; connect them with the Pi powered off.
  • rpicam-still and rpicam-vid take photos and video; picamera2 does the same from Python.
  • Frame differencing is the base of motion-triggered capture.
  • Plan storage, lighting and privacy for camera projects.

Next: The Pi Filesystem and Package Management with apt