The Raspberry Pi camera module is one of the most versatile and popular accessories for any Pi project. Whether you want to build a home security camera, a wildlife trail camera, a time-lapse rig, or a computer vision system, setting up the camera correctly from the start saves hours of debugging. This step-by-step tutorial covers everything from initial hardware connection to capturing photos and video using the modern libcamera stack on Raspberry Pi OS Bookworm.
This guide works with Camera Module v2, Camera Module v3, and the HQ Camera. Notes are added where behaviour differs between models.
What You Need
Before starting, gather these components:
- Raspberry Pi board (Pi 3B, 4B, 5, Zero 2 W, or Pi 400 — any model with a CSI port)
- Raspberry Pi Camera Module (v2, v3, or HQ Camera)
- CSI ribbon cable (usually included with the camera)
- MicroSD card with Raspberry Pi OS Bookworm (32-bit or 64-bit, Desktop or Lite)
- Power supply appropriate for your Pi model
- Keyboard, mouse, and monitor (or SSH access for headless setup)
If you are using a Pi Zero or Pi Zero 2 W, note that the CSI connector is smaller (22-pin) than on other Pi models (15-pin). You will need either a camera designed specifically for the Zero or a 15-to-22 pin adapter cable.
Connecting the Camera to Raspberry Pi
Connecting the camera correctly is critical. A reversed or improperly seated ribbon cable is the most common cause of camera failures. Follow these steps carefully.
Step 1: Power Off Your Raspberry Pi
Always shut down and unplug the Pi before connecting or disconnecting the camera module. The CSI interface does not support hot-plugging and you risk damaging the camera or the Pi’s CSI port if you connect it while powered.
Step 2: Locate the CSI Port
On Raspberry Pi 4 and earlier, there is one CSI port: a 15-pin FFC connector located between the HDMI port and the 3.5mm audio jack. On Raspberry Pi 5, there are two CSI ports labelled CAM0 and CAM1, each with 15 pins but using a smaller, denser connector than Pi 4.
Important Pi 5 note: Pi 5 uses a different cable pitch (15-pin, 0.5mm pitch) than Pi 4 (15-pin, 1mm pitch). Camera modules shipped from 2023 onward include both cable types. If your camera only came with one cable, check the pitch before connecting.
Step 3: Open the CSI Connector Latch
The CSI connector has a locking latch. On most Pi boards, you lift the black tab upward to unlock it. On Pi 5, you press the tabs on both sides inward. Be gentle — the plastic latch is fragile, especially on the camera module end.
Step 4: Insert the Ribbon Cable
Slide the ribbon cable into the slot with the metal contacts (silver or gold strips) facing the correct direction:
- On the Pi board CSI port: metal contacts face toward the HDMI port (away from the USB ports)
- On the camera module: metal contacts face toward the PCB (away from the lens)
Push the cable in until it is seated evenly across the full width of the connector, then push the latch back down to lock it. Gently tug the cable — it should not pull out easily if locked correctly.
Step 5: Position the Camera
The camera lens faces away from the PCB. Handle the module by its edges — fingerprints on the lens degrade image quality. If you are mounting the camera in an enclosure or on a mount, ensure the cable has a gentle radius and is not sharply bent, which can damage the fine conductors inside.
Enabling the Camera Interface
On Raspberry Pi OS Bookworm (released late 2023), the camera is enabled by default for the Pi 5. On older OS versions or Pi 4 and earlier, you may need to enable the camera interface.
Using raspi-config (Recommended)
Open a terminal and run:
sudo raspi-config
Navigate to: Interface Options → Legacy Camera → Enable (on older OS versions). On Bookworm, the legacy camera stack has been removed entirely — you do not need to enable anything manually. The libcamera stack is always active.
Verify Camera Detection
After rebooting, run the following command to confirm the camera is detected:
rpicam-hello --list-cameras
You should see output listing your camera model (e.g., imx219 for v2, imx708 for v3, or imx477 for HQ Camera). If you see “No cameras available”, check your cable connection and reboot.
Testing the Camera
The quickest way to test that the camera is working is with a live preview:
rpicam-hello
This opens a preview window for 5 seconds (default). If you see a live video feed from the camera, the hardware and software are working correctly. Press Ctrl+C to stop early.
For a longer preview without saving anything:
rpicam-hello -t 0
The -t 0 flag runs the preview indefinitely until you press Ctrl+C.
Headless/SSH users: If you are accessing the Pi remotely via SSH without a display, skip the preview and go straight to capturing a JPEG file (covered in the next section). Preview windows require a display connected or X11 forwarding enabled.
Capturing Photos with rpicam-still
The rpicam-still command captures still images. Here are the most useful commands:
Basic JPEG Capture
rpicam-still -o photo.jpg
Captures a single JPEG at full resolution and saves it as photo.jpg in the current directory. By default there is a 5-second countdown before capture (so you can get out of frame). Remove the countdown with --immediate:
rpicam-still --immediate -o photo.jpg
Set Resolution
rpicam-still --width 1920 --height 1080 -o photo_1080p.jpg
Capture RAW (DNG)
For maximum image quality and post-processing flexibility, capture in DNG format (RAW):
rpicam-still -r -o photo.jpg
The -r flag saves both a JPEG and a .dng RAW file. RAW files are typically 8–25 MB each depending on the camera.
Time-Lapse Photography
Capture a photo every 10 seconds for an hour:
rpicam-still -t 3600000 --timelapse 10000 -o timelapse_%04d.jpg
Here -t 3600000 is total duration in milliseconds (1 hour), --timelapse 10000 is the interval in milliseconds (10 seconds), and %04d creates numbered filenames (0001, 0002, etc.).
Camera Module v3 Autofocus Controls
If you have the Camera Module v3, trigger autofocus before capture:
rpicam-still --autofocus-mode=auto -o photo.jpg
To focus at a specific distance (in metres):
rpicam-still --lens-position=0.5 --autofocus-mode=manual -o photo.jpg
Lens position is in dioptres (1/metres). For a subject 0.5m away, use --lens-position=2.0.
Recording Video with rpicam-vid
The rpicam-vid command handles video recording.
Basic Video Recording
rpicam-vid -t 10000 -o video.h264
Records 10 seconds of H.264 video. Note that the output is a raw H.264 bitstream, not an MP4. To create a playable MP4:
rpicam-vid -t 10000 --codec h264 -o video.h264
ffmpeg -i video.h264 -c copy video.mp4
Record Directly to MP4
On Pi 5 and newer OS versions, you can use the libav codec to output directly to MP4:
rpicam-vid -t 30000 --codec libav -o video.mp4
Set Resolution and Frame Rate
rpicam-vid -t 30000 --width 1920 --height 1080 --framerate 60 -o video_1080p60.h264
Slow Motion Video
Capture at 120fps (720p) for 10 seconds:
rpicam-vid -t 10000 --width 1280 --height 720 --framerate 120 -o slowmo.h264
Then play back at 30fps for 4x slow motion:
ffmpeg -r 120 -i slowmo.h264 -r 30 -c copy slowmo_playback.mp4
Live Streaming
Stream video over a local network using UDP:
# On Pi (sender):
rpicam-vid -t 0 --inline -o udp://192.168.1.100:5000
# On receiver (VLC or ffplay):
ffplay udp://192.168.1.100:5000
Using picamera2 Python Library
For programmatic control from Python, install and use picamera2:
sudo apt install -y python3-picamera2
Capture a Photo in Python
from picamera2 import Picamera2
import time
picam2 = Picamera2()
config = picam2.create_still_configuration()
picam2.configure(config)
picam2.start()
time.sleep(2) # Allow auto-exposure to settle
picam2.capture_file("photo.jpg")
picam2.stop()
print("Photo saved!")
Capture and Process with OpenCV
from picamera2 import Picamera2
import cv2
import numpy as np
picam2 = Picamera2()
picam2.configure(picam2.create_preview_configuration(
main={"format": "BGR888", "size": (640, 480)}
))
picam2.start()
while True:
frame = picam2.capture_array()
cv2.imshow("Camera", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
picam2.stop()
Autofocus with picamera2 (v3 Camera)
from picamera2 import Picamera2
import time
picam2 = Picamera2()
picam2.configure(picam2.create_still_configuration())
picam2.start()
# Trigger autofocus
picam2.autofocus_cycle()
time.sleep(1)
picam2.capture_file("autofocus_photo.jpg")
picam2.stop()
Troubleshooting Common Issues
“No cameras available” Error
- Shut down the Pi completely, check the ribbon cable is fully seated and locked on both ends
- Ensure the cable orientation is correct — contacts facing the right direction on both connector ends
- Run
dmesg | grep -i camto see if the kernel detected the camera at boot - On Pi 5, ensure you are using the correct cable (Pi 5 needs the 0.5mm pitch cable, not Pi 4’s 1mm pitch cable)
Dark or Overexposed Images
The camera uses auto-exposure by default. If images are consistently dark or blown out, give the camera 2 seconds to adjust before capturing. In Python with picamera2, add time.sleep(2) after start(). You can also set exposure manually: --shutter 10000 --gain 1.0 (shutter in microseconds).
Blurry Photos (v2 Camera)
The v2 camera has a fixed focus preset for subjects at approximately 1 metre. For closer subjects, you need to manually adjust the focus ring on the lens assembly using a spanner tool. The v3 camera avoids this entirely with autofocus — consider upgrading if focus is a recurring issue.
Camera Module Not Responding After pip install picamera
The old picamera library (not picamera2) is deprecated and does not work on Raspberry Pi OS Bookworm. Always use picamera2. If you installed the old package, uninstall it: pip uninstall picamera, then install the correct package: sudo apt install python3-picamera2.
Frequently Asked Questions
How do I enable the camera on Raspberry Pi OS Bookworm?
On Bookworm, the camera is enabled by default — no configuration needed. Simply connect the camera module and run rpicam-hello --list-cameras to verify detection. The legacy raspi-config → Camera → Enable option no longer exists on Bookworm.
Can I use the Pi camera over SSH without a monitor?
Yes. The CLI commands (rpicam-still, rpicam-vid) work over SSH without a display. Skip preview windows (-n flag for no preview) and save outputs directly to files. For live preview over the network, consider tools like motion or mjpg-streamer which stream JPEG frames over HTTP.
How do I use the Pi camera with Python and OpenCV?
Install python3-picamera2 and python3-opencv via apt. Use picamera2 to capture frames as NumPy arrays in BGR888 format, then pass them directly to OpenCV functions. The example in the Python section above shows the exact code pattern.
What resolution does the Camera Module v3 support?
The Camera Module v3 captures stills at up to 4608×2592 (12MP). For video, it supports 1080p at up to 50fps and 720p at up to 100fps on Pi 4. On Pi 5, higher frame rates are achievable at 1080p due to the faster ISP.
Can I record video and stream simultaneously?
Yes, using picamera2’s multi-stream capability. You can configure one stream for recording to file and another lower-resolution stream for previewing or streaming to a network simultaneously. This is a common pattern for home security systems that need local recording and remote viewing at the same time.
Ready to build your Raspberry Pi camera project? Shop the complete range of Raspberry Pi accessories and camera modules at Zbotic.in — with fast delivery across India.
Add comment