Raspberry Pi Camera with Picamera2: the short answer
Raspberry Pi cameras connect with a flat ribbon cable to the CSI camera connector. The Pi 4 uses a 15-pin connector.
- Level
- Intermediate
- Time
- About 6 min
- Topic
- Raspberry Pi
Connect a Camera Module 3, test it from the command line and capture images and video from Python.
Before this: Setting Up a Raspberry Pi
Raspberry Pi cameras connect with a flat ribbon cable to the CSI camera connector. The Pi 4 uses a 15-pin connector. The Pi 5 has two smaller 22-pin connectors, so it needs a 22-to-15-pin cable for the Camera Module 3. The metal contacts on the cable face the contacts in the connector; lift the latch gently, push the cable fully in, and close the latch. Always connect the camera with the Pi switched off.
Test the camera before writing code. rpicam-hello shows a preview for a few seconds, and rpicam-still -o test.jpg saves a photo. If the command says no cameras are available, reseat the cable at both ends and check it is the right way round. The Camera Module 3 has autofocus and a 12 MP Sony IMX708 sensor; the wide version has a 120° field of view.
In Python, use Picamera2, which is preinstalled. A minimal script is: from picamera2 import Picamera2; cam = Picamera2(); cam.start(); cam.capture_file('test.jpg'). For computer vision, capture frames as arrays with cam.capture_array() and pass them to OpenCV or a model. Lower resolutions are much faster to process: 640 × 480 is usually enough for detection or motion.
Configurations decide what you get
Picamera2 works from configurations. A preview configuration is for a live view, a still configuration for a full-resolution photo, and a video configuration for recording. Choosing the one that fits means the camera is not working at a larger size than you need, which saves time and processing.
Give the camera a moment after starting. Auto exposure and white balance need a second or two to settle, so a photo taken instantly can look too dark or have wrong colours.
Getting frames into your code
capture_array gives a NumPy array that OpenCV and other libraries can use directly. Mind the colour order: the array may be RGB while OpenCV expects BGR, which makes faces blue unless you convert. For vision work, a small size such as 640 by 480 is often enough and runs much faster than the full sensor size.
Check the camera is detected with the rpicam tools first. If they cannot see it, the problem is a cable, the connector seating or the config, not your Python.
Light and focus
Image quality depends more on light than on code. Steady, bright lighting beats any processing fix, and some modules have a fixed focus range, so test at the distance you will really use.
- Capturing a photo straight after start, before exposure has settled.
- Passing an RGB array to code that expects BGR.
- Running at full resolution when a small image is enough.
Practise this lesson with Maker
The explanation above is free for everyone. The practice for Intermediate lessons in every domain are part of the Maker plan at ₹499 a month, and Pro includes them too.
With Maker you also get: a worked example, a hands-on task, a 3-question check.
