Connect a Camera to the Raspberry Pi
Raspberry Pi Camera
- up to 3280 x 2464 pixel static images
- 1080p30, 720p60, and 640x480p90 video
These cameras are both supported in the latest version of Raspbian and there is a pure Python interface to the Raspberry Pi camera module for Python 2.7 (or above) and Python 3.2 (or above) making it very easy to integrate into your Raspberry Pi camera projects.
Connecting a Pi Camera
This tutorial assumes that you have already done the basic setup of your pi and are running the latest version of Raspian. The first step is to enable the camera, this can be done via the command line using the following command
select option 5 , “interfacing options”, and then option “P1 Camera”, then to complete the process perform a reboot.
The camera will now be enabled.
Alternatively if you have a monitor, keyboard and mouse connected you can use the desktop options, from the Pi symbol in the top left and select “Preferences” and then “Raspberry Pi Configuration”. When the popup menu appears select the Interfaces tab and then check the “enable” button for the camera as shown below and reboot.
With the camera enabled the next step is to add the Python Picamera module using the following commands,
sudo apt-get update
sudo apt-get install python-picamera
If you need the Python 3 version then use this package.
Now that we have everything we need we can start writing some code to use the camera. We will start by using the basic preview and capture functions to show us what the camera sees and then take a photo.
from picamera import PiCamera from time import sleep camera = PiCamera() # Start a camera class instance camera.start_preview() sleep(5) # Allow camera to set light levels camera.capture('/home/pi/Desktop/image.jpg') # Take Picture camera.stop_preview()
This simple program will activate the camera and show a preview on the screen (assuming you have a monitor connected) and then after 5 seconds it will take a photo an save it to your desktop.
Using your Pi Camera to capture video uses a very similar process. The following code will create a 10 second video and save it to your desktop.
from picamera import PiCamera from time import sleep camera = PiCamera() camera.start_preview() sleep(5) # Allow camera to set light levels camera.start_recording('home/pi/Desktop/video.h264') camera.wait_recording(10) camera.stop_recording() camera.stop_preview()
To play back the video that you just made execute the following command
It should be noted that in the above code I have used the command camera.wait_recording(10) instead of a simple sleep(10) command, this is because the wait_recording() command will continually check for recording errors (e.g. an out of disk space condition) while it is waiting, if you use the sleep() command then any problems won’t be discovered until the camera.stop_recording() command is executed.
The Python PiCamera module has a wide range of different settings that you can configure and adjust using a python script, below are just some of the available controls.
camera.sharpness = 0
camera.contrast = 0
camera.brightness = 50
camera.saturation = 0
camera.ISO = 0
camera.video_stabilisation = False
camera.exposure_compensation = 0
camera.exposure_mode = ‘auto’
camera.meter_mode = ‘average’
camera.awb_mode = ‘auto’
camera.image_effect = ‘none’
camera.color_effects = None
camera.rotation = 0
camera.hflip = False
camera.vflip = False
camera.crop = (0.0, 0.0, 1.0, 1.0)
The module comes with such a wide range of options that they are to broad to discuss here fully, if you want to see more information and more advanced Python examples check out the official PiCamera documentation, there i’m sure that you will find what you need to take you projects to the next level.
With your camera connected why not put it into action by checking out these tutorials
Web Cam Viewer – Use Flask to put your Pi data on the net
If you have any thought’s about this article, improvements or errors let me know in the comments below and if you found this helpful, why not share it with others.