VMware ESXi and vSphere Cluster Management
Record Photos and Videos with a Raspberry Pi Camera Module
Learn how to install, enable, and use an original Raspberry Pi Camera Module with Raspbian commands for JPEG photos and short H.264 videos.
What you will learn
This lesson shows how to connect an original Raspberry Pi Camera Module, enable its camera interface in Raspbian, and capture media from a terminal. You will create a JPEG still image with raspistill, record a short H.264 video with raspivid, and play that video with omxplayer.
You should already know how to start and safely shut down a Raspberry Pi, identify its main ports, open a terminal, enter Linux commands, and work with files and directories.
Camera Module overview
The Raspberry Pi Camera Module is a compact camera accessory designed to connect directly to a Raspberry Pi. Its electronics are mounted on a small PCB, or printed circuit board. The lens and image sensor are attached to this board, and a flat ribbon cable links it to the Raspberry Pi.
In the hardware context used by this lesson, the module supports 5-megapixel still images and 1080p video. A megapixel represents one million pixels. 1080p means a video frame with a resolution of 1920 by 1080 pixels.
Camera Modules are normally obtained through Raspberry Pi retailers and compatible hardware suppliers. Check that the module and its ribbon cable are compatible with your particular Raspberry Pi board.
Install the camera hardware
Find the CSI connector
The camera connects to the Raspberry Pi's CSI connector. CSI means Camera Serial Interface. On the traditional Raspberry Pi board layout used with this equipment, the CSI connector is near the edge of the board, in the area between or close to the Ethernet and HDMI ports. It is separate from the display connector, even though both connectors use similar flat cables.
The CSI connector has a small locking mechanism. Release the lock gently, insert the ribbon cable fully, and close the lock to hold the cable in place. Do not force the connector or bend the cable sharply.
Orient the ribbon cable correctly
Insert the cable so that its exposed silver contacts face toward the HDMI side of the Raspberry Pi. The contacts must also sit evenly inside the connector. A cable that is reversed, partly inserted, or connected to the display connector can prevent the camera from being detected.
Power off before handling the camera. Shut down the Raspberry Pi and disconnect its power before connecting or disconnecting the ribbon cable. Do not reseat the cable while the board is powered. This reduces the risk of damaging the connector, camera, or Raspberry Pi.
Prepare Raspbian
After attaching the camera, boot the Raspberry Pi and open a terminal. Updating the package information and installed packages helps ensure that the operating system has current camera-related support and compatible system components.
sudo apt-get updateThe update command refreshes the package lists. Then upgrade installed packages:
sudo apt-get upgradeRead the package manager's prompt and confirm the upgrade when appropriate. A network connection and enough free storage are required. If either command fails, resolve the network, storage, or package-manager problem before continuing.
Enable the camera interface
The camera interface must be enabled in the Raspberry Pi configuration before the legacy camera tools can use the module.
Open the configuration utility:
sudo raspi-configChoose the camera setting in the applicable Interface Options section, or in the legacy camera settings section used by that Raspbian release. Enable the camera interface, exit the utility, and reboot if the utility requests it. A reboot is commonly required for the changed setting to take effect.
After the reboot, verify access by running a test capture. A successful test capture demonstrates that the operating system can communicate with the camera. If the tool reports that no camera is detected, use the troubleshooting section below to check the cable, connector, configuration, and reboot status.
Capture a still image with raspistill
raspistill is the legacy Raspberry Pi command-line tool for capturing still images. The -o option means “output” and is followed by the filename or path for the image.
raspistill -o picture.jpgThis command captures one still image as a JPEG file. JPEG is a compressed image format commonly identified by the .jpg extension. Because the command supplies only a filename and no directory, picture.jpg is saved in the terminal's current working directory.
For a first test, the expected result is a file named picture.jpg in the current directory. You can list the directory contents with:
ls -lUse meaningful names and an explicit directory when organizing several captures. For example, first create a writable directory if needed, then provide its path:
mkdir -p ~/camera-captures
raspistill -o ~/camera-captures/desk-test.jpg
ls -l ~/camera-capturesThe filename helps identify what was photographed, while the directory keeps camera output separate from unrelated files.
Record a short video with raspivid
raspivid is the legacy Raspberry Pi command-line tool for recording video. Its -o option selects the output filename.
raspivid -o video.h264This example records for the tool's default short capture duration and saves the result as video.h264. The output is an H.264 elementary stream. H.264 is a video codec, and an elementary stream contains encoded video without the broad compatibility features of a container such as MP4.
To organize the recording, specify a complete output path:
raspivid -o ~/camera-captures/short-room-test.h264
ls -l ~/camera-capturesAllow the default capture interval to finish before checking the file. A timed-video workflow produces a short recording automatically; a still-image workflow produces one JPEG image and ends after the photo is captured.
Play the recorded video
A Raspberry Pi video player such as omxplayer can play a compatible H.264 output file:
omxplayer video.h264If the file is in another directory, use its full or relative path:
omxplayer ~/camera-captures/short-room-test.h264Raw H.264 output is not the same as a broadly compatible MP4 file. Some desktop media applications may not recognize or play it correctly because it is an elementary stream rather than a containerized video. In the applicable Raspberry Pi environment, try omxplayer. For use with other players or applications, conversion or remuxing may be needed.
Capture command comparison
Manage files and storage
Every output filename is interpreted relative to the current working directory unless it includes a path. Use pwd to see that directory and ls -l to inspect its files.
pwd
ls -lIf an expected file is missing, check whether you ran the command from a different directory or mistyped the output path. An explicit path makes the workflow easier to repeat:
raspistill -o ~/camera-captures/front-garden.jpg
raspivid -o ~/camera-captures/front-garden.h264
ls -l ~/camera-capturesBefore recording, confirm that the destination is writable and has enough free space. Video files can consume storage more quickly than a single JPEG. Remove or move old captures when necessary, and avoid saving media into a directory where you do not have write permission.
Camera setup checklist
Troubleshooting
No camera is detected
- Power down the Raspberry Pi before reseating the ribbon cable.
- Confirm that the cable is in the CSI connector rather than a display connector.
- Check that the exposed silver contacts face toward the HDMI side.
- Make sure the camera interface is enabled in
raspi-config. - Reboot after changing the camera configuration.
The expected file cannot be found
Run pwd to identify the directory in which the command ran, then use ls -l to list its contents. Check the spelling of the filename and output path. In future commands, use an explicit path such as ~/camera-captures/picture.jpg.
The video does not play
The file may be a raw .h264 elementary stream rather than an MP4 container, or the selected player may not support that stream. Try:
omxplayer video.h264For other players, conversion or remuxing may be required.
Images are dark or poor quality
Improve the lighting, check that the lens is unobstructed, and aim the camera correctly. A clear lens and adequate light are important even when the camera and software are configured correctly.
System updates fail
Check the network connection first. Also check for insufficient free storage or a package-manager lock caused by an interrupted update. Resolve the underlying package-manager issue before retrying the update and upgrade commands.
Complete first-test workflow
- Shut down and unplug the Raspberry Pi.
- Attach the Camera Module to the CSI connector with the exposed silver contacts facing toward the HDMI side.
- Power on the Raspberry Pi and open a terminal.
- Run
sudo apt-get update, followed bysudo apt-get upgrade. - Run
sudo raspi-config, enable the camera interface in the applicable settings section, and reboot if prompted. - Capture a test JPEG with
raspistill -o picture.jpg. - Run
ls -land confirm thatpicture.jpgexists in the current working directory. - Record a short test video with
raspivid -o video.h264. - Play it with
omxplayer video.h264.
For the lesson's related camera setup material, see Record Pictures and Videos.