0

members

Easy Raspberry basics: Project 02a Raspberry PI camera module V2

of Acoptex.com in Raspberry Pi 3

Raspberry basics: Project 02a

Project name: Raspberry PI camera module V2

Tags: Raspberry, Raspberry camera V2, Raspberry PI camera module, Raspberry PI 3

In this project, you need these parts :

1.Raspberry PI 3 model B 1 pc

2. Raspberry PI camera module V2 1 pc

3. Micro SD card with NOOBS and SD card adapter 1 pc

4. Micro USB power supply (2.1 A, max 2.5 A) 1 pc

 

TV or monitor and HDMI cable
Keyboard and mouse

 

5. TV or monitor 1 pc

6. HDMI cable 1 pc

7. USB keyboard 1 pc

8. USB mouse 1 pc

General

We will learn how to use the Raspberry PI camera module V2, take still pictures, record video, and apply image effects.

Understanding the Raspberry PI camera module V2

The Raspberry Pi Camera Module v2 replaced the original Camera Module in April 2016. The v2 Camera Module has a Sony IMX219 8-megapixel sensor (compared to the 5-megapixel OmniVision OV5647 sensor of the original camera).

The Camera Module can be used to take high-definition video, as well as stills photographs. It’s easy to use for beginners, but has plenty to offer advanced users if you’re looking to expand your knowledge. There are lots of examples online of people using it for time-lapse, slow-motion, and other video cleverness. You can also use the libraries we bundle with the camera to create effects.

You can read all the gory details about IMX219 and the Exmor R back-illuminated sensor architecture on Sony’s website, but suffice to say this is more than just a resolution upgrade: it’s a leap forward in image quality, colour fidelity, and low-light performance. It supports 1080p30, 720p60 and VGA90 video modes, as well as still capture. It attaches via a 15cm ribbon cable to the CSI port on the Raspberry Pi.

The camera works with all models of Raspberry Pi 1, 2, and 3. It can be accessed through the MMAL and V4L APIs, and there are numerous third-party libraries built for it, including the Picamera Python library. See the Getting Started with Picamera resource to learn how to use it.

The camera module is very popular in home security applications, and in wildlife camera traps.

There’s also an infrared version of the camera (called Pi NoIR) which gives you everything the regular Camera Module offers, with one difference: it doesn’t use an infrared filter. This gives you the ability to see in the dark with infrared lighting.

You can read more about camera module here.

Understanding the Raspberry PI 3 model B

The Raspberry Pi 3 is the third-generation Raspberry Pi. It replaced the Raspberry Pi 2 Model B in February 2016.

Specification:

  • Quad Core 1.2GHz Broadcom BCM2837 64bit CPU
  • 1GB RAM
  • BCM43438 wireless LAN and Bluetooth Low Energy (BLE) on board
  • 40-pin extended GPIO
  • 4 USB 2 ports
  • 4 Pole stereo output and composite video port
  • Full size HDMI
  • CSI camera port for connecting a Raspberry Pi camera
  • DSI display port for connecting a Raspberry Pi touchscreen display
  • Micro SD port for loading your operating system and storing data
  • Upgraded switched Micro USB power source up to 2.5A

Signals and connections of the Raspberry PI 3 model B


Signals and connections of the Raspberry PI camera module V2

You can see on the back of ribbon cable one side has a piece of blue plastic and other has some traces on it. Line up traces on the ribbon cable with traces on the Raspberry PI 3 camera port. There is the tab which you can lift up and pull it by its edges. By doing that it will open up so you can insert the ribbon cable end with traces into the camera port. When it's in press tab to fix the ribbon cable.

Step by Step instruction

  1. Connect the Raspberry PI camera module V2 to the Raspberry Pi 3 camera port (CSI camera port). Make sure that Raspberry PI 3 switched off.
  2. Connect Raspberry PI 3 board HDMI port and to your TV or Monitor HDMI port with HDMI cable.
  3. Make sure that your monitor or TV is turned on, and that you have selected the right input (e.g. HDMI 1,  etc).
  4. Plug in your USB mouse and USB keyboard to Raspberry PI 3 USB ports.
  5. Connect Micro USB power supply to Raspberry PI 3 board micro USB input.
  6. The Raspberry PI desktop will start up.
  7. Open the Raspberry Pi Configuration tool from the main menu. Go to Raspberry icon-> Preferences -> Raspberry PI configuration
  8. Ensure the camera software is enabled. If it’s not enabled, enable it and reboot your Raspberry PI 3 board to begin.
  9. Let's start by trying out the camera preview. Open Python 3 from the main menu. Go to Raspberry icon -> Programming -> Python 3 (IDLE).
  10. Open a new file and save it as camera.py. Go to File -> New. The new window opens. Go to File -> Save as. Type file name - camera. Press Enter button. It’s important that you do not save it as picamera.py.
  11. Enter the following code in the file camera.py
  12. Save the file with Ctrl + S and run it with F5. The camera preview should be shown for 10 seconds, and then close. Move the camera around to preview what the camera sees.
  13. The live camera preview should fill the screen like that:
  14. Note that the camera preview only works when a monitor is connected to the Pi, so remote access (such as SSH and VNC) will not allow you to see the camera preview.
  15. If your preview was upside-down, you can rotate it with the following code:
  16. You can rotate the image by 90, 180, or 270 degrees, or you can set it to 0 to reset.
  17. You can alter the transparency of the camera preview by setting an alpha level:
  18. alpha can be any value between 0 and 255.
  19. The most common use for the Raspberry PI camera module V2 is taking still pictures. Amend your code to reduce the sleep and add a camera.capture() line:
  20. It’s important to sleep for at least 2 seconds before capturing, to give the sensor time to set its light levels.
  21. Run the code and you’ll see the camera preview open for 5 seconds before capturing a still picture. You’ll see the preview adjust to a different resolution momentarily as the picture is taken.
  22. You’ll see your photo on the Raspberry PI Desktop. Double-click the file icon to open it:
  23. Let's try to add a loop to take five pictures in a row:
  24. The variable i contains the current iteration number, from 0 to 4, so the images will be saved as image0.jpg, image1.jpg, and so on.
  25. Run the code again and hold the camera in position. It will take one picture every five seconds. Once the fifth picture is taken, the preview will close. Now look at the images on your Desktop and you’ll see five new pictures.
  26. Let's us try to record a video. Amend your code to replace capture() with start_recording() and stop_recording():
  27. Run the code; it will record 10 seconds of video and then close the preview.
  28. To play the video, you’ll need to open a terminal window by clicking the black monitor icon in the taskbar:
  29. Type the following command: omxplayer video.h264 . Press Enter button to play the video: 
  30. The video will be shown. It may actually play at a faster speed than what has been recorded, due to omxplayer’s fast frame rate.

Code

Effects

At the beginning, you created a camera object with camera = PiCamera(). You can manipulate this camera object in order to configure its settings. The camera software provides a number of effects and other configurations you can apply. Some only apply to the preview and not the capture, others apply to the capture only, but many affect both.

The resolution of the capture is configurable. By default it’s set to the resolution of your monitor, but the maximum resolution is 2592 x 1944 for still photos and 1920 x 1080 for video recording. Try the following example to set the resolution to max. Note that you’ll also need to set the frame rate to 15 to enable this maximum resolution:

The minimum resolution allowed is 64 x 64. Try taking one at that resolution.

You can easily add text to your image with annotate_text. Try it:

You can alter the brightness setting, which can be set from 0 to 100. The default is 50. Try setting it to another value:

Try adjusting the brightness in a loop, and annotating the display with the current brightness level:

Similarly, try the same for the contrast:

You can set the annotation text size with the following code: camera.annotate_text_size = 50

Valid sizes are 6 to 160. The default is 32.

You can also alter the annotation colours. First of all, ensure that Color is imported by amending your import line at the top: from picamera import PiCamera, Color

Then amend the rest of your code as follows:

You can use camera.image_effect to apply a particular image effect. The options are: none, negative, solarize, sketch, denoise, emboss, oilpaint, hatch, gpen, pastel, watercolor, film, blur, saturation, colorswap, washedout, posterise, colorpoint, colorbalance, cartoon, deinterlace1, and deinterlace2. The default is none. Pick one and try it out:

Try looping over the various image effects in a preview to test them out:

You can use camera.awb_mode to set the auto white balance to a preset mode to apply a particular effect. The options are: off, auto, sunlight, cloudy, shade, tungsten, fluorescent, incandescent, flash, and horizon. The default is auto. Pick one and try it out:

You can loop over the available auto white balance modes with camera.AWB_MODES.

You can use camera.exposure_mode to set the exposure to a preset mode to apply a particular effect. The options are: off, auto, night, nightpreview, backlight, spotlight, sports, snow, beach, verylong, fixedfps, antishake, and fireworks. The default is auto. Pick one and try it out:

You can loop over the available exposure modes with camera.EXPOSURE_MODES.

Summary

We have learnt how to use the Raspberry PI camera module V2.

Library

  • No libraries needed in this project

Sketch

  • No sketches needed in this project


Other projects of Acoptex.com
Medium Basics: Project 083w Sipeed Maixduino board - Using PlatformIO IDE of Acoptex.com in Sipeed Maixduino 08-08-2019
Medium Basics: Project 083e Sipeed Maixduino board - Uploading MaixPy of Acoptex.com in Sipeed Maixduino 04-08-2019
Medium Basics: Project 083f Sipeed Maixduino board - Using MycroPython of Acoptex.com in Sipeed Maixduino 04-08-2019

Published at 17-03-2018
Viewed: 1672 times