Getting Started with Raspberry Pi Cameras

Raspberry Pi Camera Version 2.1.
Raspberry Pi Camera Version 2.1

The Raspberry Pi single-board computers are a great starting point for machine vision research.  One of the many reasons why is the official Raspberry Pi Camera Module.   The newer module: the Raspberry Pi Camera v2.1 uses a Sony IXM219 with an 8-megapixel sensor that can record high definition images and video.

Together, these systems are capable of running a growing number of Linux distributions.  Using the Raspberry Pi Operating System (formerly Rasbian OS) and Python, it’s possible to do object detection, object tracking, and optical character recognition, just to name a few possibilities.

Notice of Non-Affiliation and Disclaimer: As of the publication date, we are not affiliated, associated, authorized, endorsed by, compensated by, or in any way officially connected with the Raspberry Pi Foundation, Arducam, HDMI Founders, HDMI Forum, or their owners, subsidiaries or affiliates.

Objectives

Today, I will cover how to get the Raspberry Pi Camera working out of the box, and a few pitfalls to avoid during your setup.

  • Unboxing the Raspberry Pi Camera
  • Attaching the Camera
  • Still Image Capture Test
  • Video Capture Test

Requirements

In a previous article, we configured the new Raspberry Pi 4 to run the Raspberry Pi OS.  Today we will use the Raspberry Pi OS and the terminal to enable, configure, and test the camera.

  • Raspberry Pi 3 or Pi 4 (Raspberry Pi 4 with 4 GB is featured here)
  • Raspberry Pi Camera v2.1
  • A working image of Raspberry Pi OS or Raspbian OS
  • Internet Connection
  • 5-volt Power Source for Raspberry Pi 4
  • (Recommended) Anti-static work area

Getting Started

As with every electrical build, I recommend working at an anti-static workstation, or using a safely grounded wrist strap.  This will bleed away your electrostatic charge and hopefully protect the life of your project.  If you do not have these available, at the very least consider handling your Raspberry Pi and camera by the edges.

The Raspberry Pi Camera v2.1 is included with the required cable.
Figure 1: The Raspberry Pi Camera v2.1 with Flat Cable

The Raspberry Pi Camera v2.1 ships with the required flexible flat cable and anti-static packaging.  The camera lens will be covered with protective, semi-transparent tape that I will leave on for now.

Figure 2: The Raspberry Pi Camera v2.1 and Mounting Holes

Like the Raspberry Pi itself, the Pi Camera has 4 mounting holes that fit the metric M2 screw size.  These are non-threaded holes, so you should plan on using locking nuts to keep everything in place.

Attaching the Camera

Your camera ships with a flexible flat cable that you should attach to the camera connector on the Raspberry Pi.  Only one side of the ribbon end will have metallic contacts.

The camera connector is midway between the HDMI connector and the audio port.
Figure 3: The Ribbon Cable Must Be Facing the Correct Direction

Locate the beige socket marked “camera.”  Unlatch it by gently lifting up the black locking tab until you see a gap that the ribbon can fit into.  Insert the ribbon cable with the metal ends facing the nearby HDMI connector.

The opening for the ribbon connector will only be visible after lifting up on the locking tab.
Figure 4: The Camera Socket Will Not Accept a Cable Until it is Unlocked

Once the ribbon is in, push it down into the connector until the metal contacts are barely visible.  Then latch the connector by gently pushing the black locking tab back down on both sides. 

A fully-seated ribbon cable will only have a small portion of the metal conductor visible.
Figure 5: Unlocked vs. Locked Sockets for Comparison

Boot into Raspberry Pi OS

Start the Raspberry Pi OS using your preferred means, and then open the terminal.

As a rule, we should update everything to the latest version.  Enter the following, and then prepare to confirm the upgrades if needed…

sudo apt-get update

sudo apt-get upgrade

I should note my remote session disconnected during the upgrade.  If this happens to you, just log back in and attempt to do another sudo apt-get upgrade.  If you get an error message saying: “could not get lock,” this is a good indication your upgrade is still happening in the background.  Give it about 10 minutes and keep retrying. 

Next, it’s time to configure the Raspberry Pi to enable the camera and other important functions.

sudo raspi-config

Image to show the Raspberry Pi operating system environment.
Figure 6: The Raspberry Pi Operating System as it Appears through Windows 10 Remote Desktop

Changing the Default Password

If you are still using the default username and password (pi and raspberry), then now would be a good time to change it.

Select “Change User Password”, then follow the on-screen instructions until you have your new password confirmed and ready to use.

Figure 7: Raspberry Pi Configuration Tool

Enabling the Camera

Enable the camera by selecting:

Interfacing options –> Camera –> Yes (Enable)

(Recommended) Expand the File System

To get the most out of your microSD card storage, you can expand the file system by navigating to:

Advanced Options –> Expand Filesystem

Exit Raspi-Config

Now navigate to “Finish.”  Reboot if you are asked to, and then give your system a minute or two to restart.

Taking a Single Still Photo

Reopen the terminal.

Eagle-eyed readers might notice I’ve left the protective tape covering the Raspberry Pi lens.  The tape is transparent enough to collect images without exposing the lens to dust, so I will leave it on for now.  Just know that removing the tape will improve your photo quality from what you see here.

Back in the terminal, enter the following command…

raspistill -o singleFrame.jpg

After a short delay, you should be able to open the File Manager.  Navigate to the /home/pi directory.  There, you should see the captured image file: singleFrame.jpg. 

A screenshot of a computer

Description automatically generated
Figure 8: A screenshot of the Captured Image (a Keyboard), Terminal, and File System

By default the image will be saved to wherever you are in your directory structure when you run it.  You can check your location in the terminal by typing:

pwd

…for “print working directory.”  If you want your images saved elsewhere, you should include your intended path along with the file name.

Know Your Options

Return to the terminal and simply enter:

raspistill

The application will generate a help report, including a comprehensive list of commands.  The authors were quite generous in enabling automatic adjustments for exposure and white balance by default.  I won’t cover all the options here, but here are two functions that may be generally useful…

Rotating the Image

Now rotate the image 90 ° by entering:

raspistill -o rotatedFrame.jpg -rot 90

A close up of a computer keyboard

Description automatically generated
Figure 9: Frame Rotated by 90 Degrees

Essentially, this has rotated the image clockwise, bringing the top of the scene to the right.  But note it did not change the aspect ratio of the file from 4:3 to 3:4.

For rotations, this application will accept positive integers ranging from 0 to 359.

This version is effective in 90 ° increments.  If you enter a value other than 0, 90, 180, or 270; your entry will be reduced to match one of those four values.  In other words: values 0 to 89 result in no rotation.  Values 90 to 179 result in 90 ° rotation, and so on.

Mirroring the Image

A close up of a computer keyboard

Description automatically generated
Figure 10: A Horizontally Mirrored Image Swapping Left and Right Elements

To mirror the image horizontally or vertically, you can enter the -hf or -vh options.

raspistill -o mirroredFrame.jpg -hf

Alternatively, you can use the slightly more human-readable equivalents: –hflip and –vflip.

Recording Video Clips

You can record a 5-second test video.  Return to the terminal, and enter:

raspivid -o videoTest.h264 -t 5000

The “5000” indicates a recording duration in milliseconds.  This application can use the same rotation and mirroring options as before.

After a few seconds of recording, you can view it by navigating back to the /home/pi .  Then open the .h264 video file by double-clicking it.

sudo apt-get install gpac

Then you can convert your video file (videoTest.h264) into an MP4 file (videoTest.mp4) like this:

MP4Box -add videoTest.h264 videoTest.mp4

This will create an MP4 version alongside the original.  Like before, you can read the help file for raspivid by typing:

raspivid

Commands Used Today

sudo apt-get update
sudo apt-get upgrade
sudo raspi-config
raspistill -o singleFrame.jpg
pwd
raspistill
raspistill -o rotatedFrame.jpg -rot 90
raspistill -o mirroredFrame.jpg -hf
raspivid -o videoTest.h264 -t 5000
omxplayer videoTest.h264
sudo apt-get install gpac
MP4Box -add videoTest.h264 videoTest.mp4

Closing Remarks

Along with rotating and mirroring the video, there are options for adjusting the frame rate.  This is important to consider wherever the Raspberry Camera’s rolling shutter might create bizarre images of fast-rotating equipment. 

Helicopter footage can demonstrate the rolling shutter effect and other visual artifacts.
Figure 11: Helicopter Main Rotor as seen Under the Rolling Shutter Effect (Source: Wikipedia User “Jonen,” CC-BY-SA-3.0)

The camera itself works with all models of the Raspberry Pi, going as far back as the Raspberry Pi 1.  The official website says it will be in production until at least the year 2024.  Third-party cameras such as the ArduCam are definitely worth considering, and the ArduCam has so far been compatible with all my Raspberry Pi Cameras applications so far.

References

[1]Raspberry Pi Foundation, “Raspberry Pi 4 Computer Model B,” [Online]. Available: https://static.raspberrypi.org/files/product-briefs/200521+Raspberry+Pi+4+Product+Brief.pdf. [Accessed 20 Aug. 2020].
[2]S. McManus and M. Cook, “How to Test the Raspberry Pi Camera Module,” Dummies.com, [Online]. Available: https://www.dummies.com/computers/raspberry-pi/test-raspberry-pi-camera-module/. [Accessed 20 Aug 2020].
[3]Raspberry Pi Foundation, “Camera Module V2,” Raspberry Pi Foundation, [Online]. Available: https://www.raspberrypi.org/products/camera-module-v2/. [Accessed 23 Aug. 2020].
[4]A. Rosebrock, “Install OpenCV 4 on Raspberry Pi 4 and Raspbian Buster,” PyImageSearch, 16 Sep. 2020. [Online]. Available: https://www.pyimagesearch.com/2019/09/16/install-opencv-4-on-raspberry-pi-4-and-raspbian-buster/. [Accessed 19 Aug. 2020].

Important Notice: This article and its contents (the “Information”) belong to Unboxing-tomorrow.com and Voxidyne Media LLC. No license is granted for the use of it other than for information purposes. No license of any intellectual property rights is granted.  The Information is subject to change without notice. The Information supplied is believed to be accurate, but Voxidyne Media LLC assumes no responsibility for its accuracy or completeness, any error in or omission from it or for any use made of it.  Liability for loss or damage resulting from any reliance on the Information or use of it (including liability resulting from negligence or where Voxidyne Media LLC was aware of the possibility of such loss or damage arising) is excluded.