Using a camera with Raspberry Pi
Which ribbon cable your camera needs, how to plug it in the right way round, and how to check it works from the command line and from Python.
The cameras
Every official Raspberry Pi camera plugs into the same kind of ribbon connector and uses the same software, so the steps below apply to all of them.
| Camera | Sensor | Resolution | Notes |
|---|---|---|---|
| Camera Module 3 | Sony IMX708 | 12 MP | Autofocus; standard or wide lens, with or without an IR filter (NoIR) |
| High Quality Camera | Sony IMX477 | 12.3 MP | Takes interchangeable C/CS or M12 lenses |
| Global Shutter Camera | Sony IMX296 | 1.58 MP | Captures the whole frame at once, so fast motion doesn't smear; C/CS lenses |
| AI Camera | Sony IMX500 | 12.3 MP | Runs vision models on the camera itself (see our AI Camera guide) |
| Camera Module 2 | Sony IMX219 | 8 MP | The previous generation; focus is set by hand |
Which cable you need
Raspberry Pi boards use two sizes of camera connector:
| Board | Connector | Cable |
|---|---|---|
| Raspberry Pi 4, 3 and earlier | 15-pin (standard) | The standard–standard cable that comes with the camera |
| Raspberry Pi 5 | 22-pin (mini), two of them | Standard–mini camera cable |
| All Raspberry Pi Zero models | 22-pin (mini) | Standard–mini camera cable |
| Compute Module IO boards | 22-pin (mini) | Standard–mini camera cable |
Every camera arrives with the standard–standard cable already fitted. For a Pi 5 or a Zero, take that cable off and fit a standard–mini one instead: the wider 15-pin end goes into the camera and the narrow 22-pin end into the Pi.
Plugging it in
Shut the Pi down and unplug the power first. Then find the camera port:
- Pi 5: two ports marked CAM/DISP 0 and CAM/DISP 1, on the long edge between the micro HDMI sockets and the Ethernet port. Either one will do.
- Pi 4 and earlier: the port marked CAMERA, between the HDMI socket(s) and the audio jack. Don't use the DISPLAY port; it can't power or talk to a camera.
- Pi Zero: on the short edge, at the opposite end from the microSD slot.
Each connector has a small plastic latch. To fit the cable:
- Peel off any protective film, then pull the latch gently outwards until it stops.
- Slide the ribbon in straight and all the way, with its metal contacts facing away from the latch.
- Press the latch back down until it clicks.
The camera end works the same way. Its connector is on the back of the board, behind the lens, and the contacts face the circuit board. Avoid folding the ribbon into a sharp crease.
Checking it works
Raspberry Pi OS finds official cameras by itself, so there's normally nothing to configure. Bring the system up to date, then reboot:
sudo apt update
sudo apt full-upgrade
The camera tools are the rpicam apps. Start by asking which cameras the Pi can see:
rpicam-hello --list-cameras
You should see your camera's sensor name (for example imx708 for a Camera Module 3). Then try these:
| Command | What it does |
|---|---|
rpicam-hello -t 0 |
Live preview until you press Ctrl+C (without -t 0 it stops after five seconds) |
rpicam-still -o photo.jpg |
Takes a full-resolution photo |
rpicam-still --encoding png -o photo.png |
The same, saved as a PNG |
rpicam-vid -t 10s -o clip.mp4 |
Records ten seconds of video on a Pi 5 |
rpicam-vid -t 10s --codec libav -o clip.mp4 |
Records ten seconds of video on a Pi 4 or earlier |
If the Pi has no screen attached, add -n to skip the preview window. A Pi 5 encodes video in software, which can add a little delay; for live streaming, add --low-latency.
Older tutorials use raspistill and raspivid. Those belong to the old camera system and don't work on current Raspberry Pi OS; use the rpicam commands instead.
Using the camera from Python
Picamera2 is the Python library for the camera. It's already installed on Raspberry Pi OS with the desktop. On Raspberry Pi OS Lite, install it with apt (not pip, which can pull in a version that doesn't match the rest of the camera system):
sudo apt install -y python3-picamera2 --no-install-recommends
This script takes a photo:
import time
from picamera2 import Picamera2
camera = Picamera2()
camera.configure(camera.create_still_configuration())
camera.start()
time.sleep(2) # give the exposure a moment to settle
camera.capture_file("photo.jpg")
camera.stop()
For quick jobs there are one-line shortcuts: camera.start_and_capture_file("photo.jpg") takes a photo, and camera.start_and_record_video("clip.mp4", duration=5) records five seconds of video.
If the camera isn't found
- Check the ribbon at both ends: pushed fully home, sitting straight rather than at an angle, contacts the right way round.
- On a Pi 4 or earlier, make sure it's in the CAMERA port, not DISPLAY.
- Update Raspberry Pi OS as above.
- Use a good power supply. A camera adds roughly 200–250 mA to what the Pi draws.
- A third-party camera may need
camera_auto_detect=0and adtoverlay=line in/boot/firmware/config.txt; follow its maker's instructions. On a Pi 5, add,cam0to the end of that line if the camera is in port 0, as the overlay assumes port 1 otherwise.
Parts in this guide
Raspberry Pi
Raspberry Pi Camera Module 3
Raspberry Pi
Raspberry Pi Camera Cable Standard - Mini
Raspberry Pi
Raspberry Pi Camera Module 3 Wide
Raspberry Pi
Raspberry Pi High Quality Camera
Raspberry Pi