Last update: June 2020 • 32 • 4.5
The Raspberry Pi is equipped with two DSI and CSI ports (Display Serial Interface and Camera Serial Interface) allowing you to connect a screen or camera specifically designed for it. Indeed, connecting a USB camera will never provide the same quality and image framerate as with the dedicated port.
En plus d'un Raspberry Pi, you will need a compatible camera for this tutorial. There are 4 main models. To help you make your choice, here is a comparison:
Prices given for information only, observed on 01/20/2021.
Now that the module choice is made, let's move on to its use.
Connecting the camera to the Raspberry is very simple. It has its own connector (slot). It is located between the Ethernet port and the HDMI output. Follow the images to properly connect the camera.
Now that everything is properly connected, we need to make some settings on the Raspberry side. First, we need to activate the camera interface. To do this, nothing complicated. Enter the following command in the terminal.
sudo raspi-config
Once in the menu, use the arrow keys and the Enter key to navigate between menus.
Go to Interfacing Options then to Camera. Finally, select Yes when asked for activation confirmation and to restart the Raspberry.
To apply the changes, restart the Raspberry Pi.
sudo reboot
Before continuing, here is a command to check if the camera is properly detected:
vcgencmd get_camera
If the displayed line is not supported=1 detected=1, check:
If detected is still not 1 after these checks, restart the Raspberry Pi and try to update it:
sudo apt-get update
sudo apt-get upgrade
The camera should now be detected.
Let's get to the point! The first thing we're going to do with this camera is take photos. To do this, enter the following commands:
cd
mkdir Camera
raspistill -o ~/Camera/image.jpg -q 100
The first two commands create a folder in the home directory. The third line takes a photo and stores it in the previously created folder.
The raspistill command accepts several arguments, here are the most important:
All arguments can be found by entering raspistill --help in the terminal
To view the taken image, two possibilities:
Taking photos through the terminal is a good start but... it's not great yet. It is much more practical for potential projects to be able to take photos directly inside a Python script. To do this, there is the picamera library. This library is normally installed by default on the Raspberry but nothing prevents checking. So let's run the command:
sudo apt-get install python3-picamera
This time, the library is definitely installed. Let's start by creating a simple Python script that takes and saves a photo (with the date as filename).
The camera.resolution line is not mandatory. However, if not specified, the chosen resolution will be 1920x1080, and the photo quality will be very poor (especially in low light). If you want the best possible photo quality, then specify:
Depending on how your camera is positioned, the captured image may be upside down. To correct this, add this after the line "camera=PiCamera()":
camera.hflip = True
camera.vflip = True
Which gives the following code:
Other options that can be added in the same place are available for photography enthusiasts, here is a list:
Now that we master photo taking, let's look at video. To record a 10-second video (10,000 ms) with the filename video.h264, just enter the following command.
raspivid -o video.h264 -t 10000
The options for this command are the same as for photos (like hf and vf for example).
Problem: most video players won't be able to read the .h264 format. For this reason, we will install the MP4Box library which will allow us to convert videos to MP4 format. To install MP4Box, just enter this command:
sudo apt-get install -y gpac
Once the software is installed, run this to convert the file:
MP4Box -add video.h264 video.mp4
The video file is now playable on all devices.
As with photos, let's see how to record videos in Python. If you haven't installed the picamera library as described above (in the In Python section of Taking a Photo), then go back in the article to follow the procedure. Otherwise, we can directly move to the code.
Here is a script that allows you to capture a video in Python and convert it to MP4 automatically:
If you want your camera LED to stay off even when it is in use, just modify the /boot/config.txt file by adding the line disable_camera_led=1 at the end.
Reminder - Edit a file in the terminal:
sudo nano /boot/config.txt
This is where this tutorial ends. All the basics of use are here, but other tutorials around the camera will surely come out (Video surveillance...). The only limit will be imagination!
Get temperature with a DS18B20 sensor
7" touchscreen LCD: test, installation and settings
Raspberry Pi 5 (8 GB version)
94.48 €
Raspberry Pi camera 5MP 1080p
12.95 €




4.5/5 | 51 votes
sudo apt-get update
sudo apt-get upgrade