The Raspberry Pi platform offers several camera modules, each with its own features and capabilities. Currently, the Raspberry Pi Foundation provides three camera modules:
- Camera Module 2: Released in April 2016 as a replacement for the original camera module, the Camera Module 2 features an 8-megapixel Sony IMX219 sensor. It offers improved image quality, low-light performance, and color fidelity compared to the previous module. Compatible with Raspberry Pi 1, 2, 3, and 4, it easily connects to the CSI port of any Raspberry Pi. The Camera Module 2 supports video modes such as 1080p30, 720p60, and VGA90.
- Camera Module 2 NoIR: Similar to the regular Camera Module 2, the NoIR variant lacks an infrared filter. This makes it suitable for night photography and capturing videos in low-light conditions. It shares the same specifications as the regular Camera Module 2 but without the infrared filtration.
- Raspberry Pi High-Quality Camera: This camera module features a 12.3-megapixel Sony IMX477 sensor and supports C- and CS-mount lenses. The high-quality camera allows for even more detailed imaging and is compatible with all Raspberry Pi models.
With a Raspberry Pi and a camera module, you can embark on exciting projects involving image processing, video processing, and even machine learning. Whether you're interested in photography or exploring advanced computer vision applications, the Raspberry Pi camera module opens up a world of possibilities.
To get started, you'll need a Raspberry Pi (any model from 1 to 4) and the corresponding camera module. The standard camera module is suitable for capturing images in well-lit environments. However, if you plan to capture images in low-light conditions, the NoIR camera module is recommended, along with an additional infrared light source. For this article, we will focus on working with the standard camera module. Remember that the camera module should come with a flexible ribbon cable for easy attachment to the CSI port. In the case of Raspberry Pi Zero, a smaller cable is required for camera module integration.
How to connect the camera module
All Raspberry Pi models feature a Camera Serial Interface (CSI) port, which is where the camera module attaches. It's important not to confuse the CSI port with the display port, as they serve different purposes. The CSI port on Raspberry Pi is located in the following position:
[Include an image or description indicating the location of the CSI port on Raspberry Pi]
To install the camera module, follow these steps:
- Locate the CSI port on your Raspberry Pi.
- Gently remove the plastic clip or cover of the CSI port.
- Take the ribbon cable of the camera module and insert it into the CSI port. Ensure that the connectors at the bottom of the cable are facing the contacts in the port.
- Carefully place the plastic clip or cover back onto the CSI port to secure the ribbon cable in place.
- The camera module attached to the Raspberry Pi should now be securely connected.
The setup should resemble the image below:
[Include a description or image illustrating the camera module attached to the Raspberry Pi]
Enable Raspberry Pi camera
After attaching the camera module, the camera functionality is not enabled by default. To enable it, follow these steps:
- Boot up your Raspberry Pi.
- From the main menu, navigate to the Raspberry Pi configuration tool. Alternatively, you can open the configuration tool from the terminal by typing
By following these steps, you can access the Raspberry Pi configuration tool to enable the camera module functionality.
In the configuration tool, select the interfaces tab and make sure the camera is enabled.
If not enabled, enable the camera and reboot Raspberry Pi.
Taking pictures from the command line
Once the camera module is installed and enabled, it’s time to test the module. The best place to test the camera module is the Raspberry Pi’s command line i.e., the Bash shell. Open the terminal window by clicking the terminal icon in the taskbar.
For taking a picture, type the following command and press Enter.
raspistill -o Desktop/image.jpg
The provided command captures a still picture using the camera module and saves it to the Desktop. When executed, the command opens the camera preview for a duration of five seconds before capturing the still picture. Once the picture is captured, the camera preview window closes. Please note that you can save the pictures to any desired location on the Raspberry Pi, as long as the specified folder exists within the Raspberry Pi's directory structure.
Capturing a video from the command line
When this command is executed, it captures a video recording and saves it to the specified folder, in this case, the Desktop. If the recording process is interrupted, the video recorded until that point will be saved. It is worth noting that videos can be saved in any desired location on the Raspberry Pi, as long as the specified folder exists within the Raspberry Pi's directory structure. To play the saved videos, you can use a VLC player or any other compatible video player of your choice.
Controlling the camera module with Python
The camera module can be controlled using Python by utilizing the picamera library. In most cases, the picamera library is already installed on Raspberry Pi 3/4 along with the IDLE or Thonny Python IDE. However, if the library is missing, you can install it using the following commands:
$ sudo apt-get install python-picamera
After installing the
picamera library, it is recommended to upgrade the installation by executing the following commands:
$ sudo apt-get update $ sudo apt-get upgrade
Alternatively, you can also install the
picamera library using PIP:
$ pip install picamera
To utilize the
picamera library in a Python script, you need to import it with the following command:
from picamera import PiCamera
To use the camera module in your Python script, you need to create an instance of the
PiCamera class as shown below:
camera = PiCamera()
To test the camera module with Python, you can run a code that displays the camera preview for a certain period. Make sure to connect a monitor to the Raspberry Pi for the camera preview to work. Here's an example Python script that demonstrates the camera preview functionality:
from picamera import PiCamera from time import sleep camera = PiCamera() camera.start_preview() sleep(5) camera.stop_preview()
Please note that you should save the Python script with a .py extension. Avoid saving it as
picamera.py to prevent conflicts. If you want to stop the camera preview while it is running, press Ctrl+F2 to safely exit from the script execution.
Taking still pictures with Raspberry Pi Camera using Python
To capture still images using the
picamera library, you can use the
capture() method of the
PiCamera class. This method requires a file path to store the captured image with the appropriate image format. Here's an example code that captures a still image using Python: