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 update

The update command refreshes the package lists. Then upgrade installed packages:

sudo apt-get upgrade

Read 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-config

Choose 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.jpg

This 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 -l

Use 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-captures

The 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.h264

This 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-captures

Allow 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.h264

If the file is in another directory, use its full or relative path:

omxplayer ~/camera-captures/short-room-test.h264

Raw 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

Task: Still-photo capture with raspistill
Tool: raspistill
Example output: picture.jpg
Output format: JPEG
Notes: Saves one still image in the current directory when no path is supplied.

Task: Timed video recording with raspivid
Tool: raspivid
Example output: video.h264
Output format: H.264 elementary stream
Notes: The example uses the default short recording duration.

Task: Video playback with omxplayer
Tool: omxplayer
Example output: No new file; plays video.h264
Output format: Compatible H.264 input
Notes: Raw H.264 may not play in every desktop media application.

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 -l

If 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-captures

Before 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

Check: Camera connected to CSI port
What to verify: The cable is attached to the camera CSI connector, not a display connector.
Expected state: The camera cable is fully inserted and secured.

Check: Ribbon cable contact orientation
What to verify: Exposed silver contacts face toward the HDMI side.
Expected state: Contacts face the correct direction and the cable is not twisted.

Check: Camera interface enabled
What to verify: The camera setting was enabled with sudo raspi-config.
Expected state: The operating system can access the camera after rebooting if requested.

Check: System updated
What to verify: sudo apt-get update and sudo apt-get upgrade completed successfully.
Expected state: Package information and installed packages are current.

Check: Sufficient storage space
What to verify: The destination directory is writable and has room for the capture.
Expected state: The expected image or video file can be created.

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.h264

For 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

  1. Shut down and unplug the Raspberry Pi.
  2. Attach the Camera Module to the CSI connector with the exposed silver contacts facing toward the HDMI side.
  3. Power on the Raspberry Pi and open a terminal.
  4. Run sudo apt-get update, followed by sudo apt-get upgrade.
  5. Run sudo raspi-config, enable the camera interface in the applicable settings section, and reboot if prompted.
  6. Capture a test JPEG with raspistill -o picture.jpg.
  7. Run ls -l and confirm that picture.jpg exists in the current working directory.
  8. Record a short test video with raspivid -o video.h264.
  9. Play it with omxplayer video.h264.

For the lesson's related camera setup material, see Record Pictures and Videos.