How to connect two cameras to the Raspberry Pi 5
Written By: Cherie Tan
Difficulty
Easy
Steps
5
The latest Raspberry Pi 5 brings a groundbreaking feature: dual camera connectors! This means you can connect two cameras simultaneously, opening doors for exciting projects like stereoscopic vision or security monitoring with wider coverage. But how do you harness this power? In this guide, let's dive into connecting and controlling these cameras using both the terminal and Python.
The Raspberry Pi 5 brings the exciting ability to connect two cameras simultaneously. This opens doors for creative projects in computer vision and more!
Let's begin:
Let's begin:
- Make sure your Raspberry Pi 5 is completely turned off and disconnected from the power source.
- You'll need two Raspberry Pi cameras (or compatible models) and the latest Raspberry Pi OS "Bookworm" installed on your Pi.
- If your cameras have the wider 22-pin connector, you'll need adapter cables or cameras designed specifically for the Raspberry Pi Zero, which uses the same connector size as the Pi 5.
- Power Down for Safety: Always prioritise safety! Unplug the power cable from your Raspberry Pi 5 before connecting or disconnecting any cameras.
- Unlocking the Delicate Latch: Locate the tiny latch on the camera connector. Using a fingernail or a plastic tool, gently lift the latch upwards. Be very careful, as these latches are fragile.
- Connecting the Cameras: Take each camera's 15-pin flat flex cable and insert it into the corresponding connector on your Raspberry Pi. Insert the cable with the gold pins on the same side as the camera lens. Also, on the other end of the camera cable where it meets the Raspberry Pi 5's camera port, ensure that the gold pins face the Ethernet port.
- Power Up and Check Connections: Once the cameras are connected, plug the power cable back into your Raspberry Pi and boot it up. Double-check that neither camera is physically touching the Raspberry Pi itself or the GPIO pins.
Now, let's test if both cameras are working properly using libcamera commands in the terminal.
Launch separate terminal windows for each camera.
Launch separate terminal windows for each camera.
- We have two cameras, one connected to CAM0 and the other to CAM1. To test Camera 0, in the first terminal, type:
libcamera-hello –camera 0 -t 0
This command displays a live preview from camera 0. The-t 0
argument prevents the window from automatically closing. - Now it is time to test Camera 1. In the second terminal, type:
libcamera-hello –camera 1 -t 0
This displays a preview from camera 1.
Note: If both terminals show preview windows, your cameras are connected successfully!
Note: If both terminals show preview windows, your cameras are connected successfully!
We can capture images using libcamera commands as well:
In the first terminal, type:
In the first terminal, type:
libcamera-jpeg -o cam0.jpg –camera 0 -t 5000
This captures an image from camera 0 and saves it as "cam0.jpg" with a 5-second delay for framing.
In the second terminal, type:
In the second terminal, type:
libcamera-jpeg -o cam1.jpg –camera 1 -t 5000
This captures an image from camera 1 and saves it as "cam1.jpg" with a 5-second delay.
Note: The captured images will be saved in the directory where you ran these commands.
Note: The captured images will be saved in the directory where you ran these commands.
from picamera2 import Picamera2, Preview from time import sleep picam0 = Picamera2(0) # Camera connected to CAM0 picam1 = Picamera2(1) # Camera connected to CAM1 picam0.start_preview(Preview.QTGL) picam1.start_preview(Preview.QTGL) picam0.start() picam1.start() sleep(10) # Adjust the delay (in seconds) as needed picam0.capture_file("cam0.jpg
This Python code snippet utilizes the Picamera2 library to capture images simultaneously from two cameras connected to your Raspberry Pi 5. Here's a breakdown of what it does:
- Import Libraries: It starts by importing necessary libraries:
picamera2
: This library provides an interface to control camera functions on your Raspberry Pi.Preview
: This provides functionality to display a live preview window from the camera.sleep
: This comes from thetime
library and allows the code to pause execution for a specified duration.
- Create Camera Objects: The code creates two objects,
picam0
andpicam1
, using thePicamera2
function. Each object represents one of the cameras connected. The argument0
is passed to createpicam0
, indicating it controls the camera connected to port CAM0 on your Raspberry Pi. Similarly,1
is used forpicam1
to control the camera on CAM1. - Start Preview Windows: The code then uses these objects to initiate preview windows for each camera using the
start_preview
function. ThePreview.QTGL
argument specifies the type of preview window to be displayed. - Activate Cameras: Next, it calls the
start
function on bothpicam0
andpicam1
objects. This activates the cameras and starts capturing live video feeds, which are likely displayed in the preview windows you created earlier. - Framing Time: The code inserts a pause using the
sleep
function from thetime
library. This delay (set to 10 seconds by default) allows you time to adjust the cameras and frame your shot before capturing the images. - Capture Images: Finally, the code captures images from both cameras simultaneously. It uses the
capture_file
function on each object, specifying the filenames "cam0.jpg" and "cam1.jpg" for the captured images from camera 0 and camera 1 respectively.