The Raspberry pi has a connector for a camera. This module will enable you to add images or videos to all your projects with ease. This can be very useful for guiding an autonomous project in the environment: object detection thanks to AI, autonomous car or even making a mini camera.
In this course, we’ll look at how to connect the camera and take a photo with it. The module comes with a ribbon cable. This must be connected to the Raspberry’s connector by gently lifting the little black latch to slide the ribbon through. Check that it’s the right way round.
There are two connectors on the Raspberry: one for an external screen, labelled Display, and the other, labelled camera. You must use the one labelled camera.
Here’s the circuit diagram:
The first step is to activate the camera in the raspberry pi configurations. To do this, type the following command in the raspberry pi terminal:
sudo raspi-config
Select Interface Options from the raspi-config menu:
Once in Interface Options, select Legacy Camera :
Next, we choose to activate the camera:
A message tells us that the camera is activated:
To use the program, you need the camera library. This is normally already installed on the Raspberry Pi. If you don’t have it, you can install it as follows: pip install camera
Here’s the program to take a photo with the camera:
from picamera import PiCamera
import time
# Create a PiCamera instance
camera = PiCamera()
try:
# Capture a photo and save it to a file
camera.start_preview() # Display the preview before taking the photo
time.sleep(2) # Wait a few seconds for camera to stabilize
camera.capture('photo.jpg') # Save photo to file
finally:
# Stop preview and release camera resources
camera.stop_preview()
camera.close()
Once the program is launched, the photo is displayed:
If you have installed linux with the graphical version, you can click directly on the photo to view it. If you have linux in command-line mode, you can copy the image to a USB key and view it from your computer.
Here’s the resulting image: