Guide to Raspberry Pi Camera V2 Module

This article is an introduction to the Raspberry Pi Camera V2 Module. It explains how to quickly get started with the camera: how to take photos and record video into a file.

We’ll cover the following topics:

  • Raspberry Pi Camera V2 Module features
  • Enable the Raspberry Pi Camera
  • Connect the Camera
  • Take photos with the camera
  • Record video into a file

Don’t have a Raspberry Pi yet? Check the Best Raspberry Pi Starter Kits.

Raspberry Pi Camera V2 Module features

The Raspberry Pi Camera V2 module is very tiny and it looks like this:

The Raspberry Pi Camera V2 features an 8 megapixel Sony IMX219 image sensor with fixed focus lens, it is capable of 3280×2464 pixel static images and supports 1080p30, 720p60, and 640×480p90 video.

The camera is compatible with all Raspberry Pi models.

Enable the Raspberry Pi Camera Module

To use the Raspberry Pi Camera module, you need to enable the camera software in your Raspberry Pi. In the Desktop environment, go to the Raspberry Pi Configuration window under the Preferences menu, open the Interfaces tab and enable the Camera as shown in figure below.

Or, in the Terminal window, type the following command:

pi@raspberry:~ $ sudo raspi-config

You should see the Raspberry Pi software configuration tool. Select the Interfacing Options:

Enable the camera and reboot your Pi:

Connect the camera

Connecting the Raspberry Pi Camera Module is easy. With the Pi shutdown, connect the camera to the Pi CSI port as shown in the following figure. Make sure the camera is connected in the right orientation with the ribbon blue letters facing up as shown in the next figure.

Take photos with the camera

The easiest way to control the Raspberry Pi Camera is using the Python PiCamera package.

I recommend using Python 3 to run this script even though the PiCamera package supports Python 2. Create a new file called take_photo.py:

pi@raspberrypi:~ $ nano take_photo.py

Copy the following code to your newly created file:

from time import sleep
from picamera import PiCamera

camera = PiCamera()
camera.resolution = (1024, 768)
camera.start_preview()

sleep(2)
camera.capture('test_photo.jpg')

View raw code

Press Ctrl+X to save your file, type Y and Enter.

When you run the script:

pi@raspberrypi:~ $ python3 take_photo.py

It takes a photo with the Raspberry Pi Camera and saves it with the test_photo.jpg name:

Recording video into a file

The next Python script also uses the PiCamera package to record video into a file.

Create a new file called record_video.py:

pi@raspberrypi:~ $ nano record_video.py

Copy the following code to your newly created file:

import picamera

camera = picamera.PiCamera()

camera.resolution = (640, 480)
camera.start_recording('test_video.h264')
camera.wait_recording(5)
camera.stop_recording()

print('Finished recording')

View raw code

Press Ctrl+X to save your file, type Y and Enter.

When you run the script:

pi@raspberrypi:~ $ python3 record_video.py

It takes 5 seconds video with the Raspberry Pi Camera and saves it with the test_video.h264 name. You can modify the script to set the camera resolution and extend the video recording duration.

To watch the video on Raspberry Pi with the Raspbian Desktop environment, you can use the omxplayer software.

pi@raspberrypi:~ $ omxplayer test_video.h264

Here’s a screenshot of the recording:

Wrapping up

This post was a quick introduction guide to the Raspberry Pi Camera V2 Module and how to take photos and record video into a file.

If you like this post, you’ll also like this project: Video Streaming with the Raspberry Pi Camera.

We hope you’ve found this post useful.

Thanks for reading!



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

11 thoughts on “Guide to Raspberry Pi Camera V2 Module”

  1. Hello Rui,

    Thank you for the short but useful tutorial.

    How to do, if i.e. I want to take a picture every 5 seconds, and show the picture on the screen.
    Every fifth second a new picture replaces the previous one on the screen.
    No picture must be saved.

    Thank you in advance for lighting me up 😉
    Regards,
    Roger

    Reply
    • Hi.
      In the “Recording video into a file” section, you just need to edit the following line of code:
      camera.wait_recording(5)
      with the desired record length.
      Regards,
      Sara

      Reply
  2. Hi,
    is it possible to have a livestream, do a record and a photo at the same time.
    For example: 24/7 I have a stream and then my PIR-Sensor recognize a motion – the script now should take a photo every 10s (high resolution) and do a video record for the time the motion is detected. I have tried all 3 scripts (streaming, photo, video) and they works perfect, thanks!
    But when the streaming script runs, no other script will work.
    Do you know the solution?

    Regards David

    Reply
    • Hi.
      I think you need to stop the streaming to be able to take pictures.
      After taking the pictures, you can restart the streaming.
      I don’t think you can have both at the same time.
      Regards,
      Sara

      Reply
  3. Hi, the code here only works on older versions of Raspberry Pi OS where the legacy camera system, which has been deprecated for several years now, has been enabled. It won’t work on any systems that are using Bookworm (the current OS at the time of writing), which includes all Raspberry Pi 5s.

    Is there any way the text could be updated to reflect this, so as to minimise the confusion caused to users? Obviously an updated tutorial would be great too! Thanks.

    Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.