Geek Culture
Published in

Geek Culture

Camera setup on Raspberry Pi 4

Wireless camera setup

Author: Eduardo Padrón

One of the most used applications in Raspberry is the use of cameras, to use Tensorflow and give it the ability to detect objects. Now that the Raspberry Pi is fast enough to perform machine learning🧠, adding these functions in projects is easier.

This guide will show you the steps to learn how to connect the Raspberry Pi camera module, take photos, record videos, and apply format changes. It is important to know the camera module 📷 well before starting to create our apps.

Let’s get started.

1. What you’ll need

There are multiple options but in this guide I will give you two options to choose from, on the part of Raspberry Pi there are two camera modules, I will talk about them later.

  • Laptop
  • Raspberry Pi 4
  • Power supply (Preferably purchase the official source together with your Raspberry so as not to have any problems, if you do not have much experience USB-C Power Supply.
  • Smartphone with Mobile Hotspot function
  • Micro SD card
  • Raspberry Pi High Quality Camera (It is the one I will use in this guide, but you can use the Raspberry Pi Camera Board v2 It is the older and cheaper version.)
  • 6mm 3MP Wide Angle Lens for Raspberry Pi HQ Camera 3MP (The HQ camera module allows us to use lenses, this element is optional in case of using the second camera option).
  • Flex Cable for Raspberry Pi Camera (It is usually included in the purchase of Raspberry Pi Camera modules).
  • Camera tripod (Note: only in case of using the HQ module and you see it necessary). The advantage of the HQ module is that it has a standard 1/4"- 20 tripod mount, which is what most current tripods have, I leave you a link to a purchase option on Amazon.

Optional:

  • Raspberry Pi HQ Camera Case (In case you have a 3D printer or access to make 3D prints I leave you a link so you can print a camera-shaped case, later I leave you the image of how it would look).
  • If you use the Case, you will need custom screws M2.5, We will need 12mm and 8mm long, although to take advantage you can buy some 10mm and 5mm they can be useful for other projects, you can get them in Amazon or specialized electronics stores.
Image by Eduardo Padron

If you are going to use the 3D model case, here you have an image of what it would look like, it’s amazing, don’t you think?

Image by Adafruit 3D Case Raspberry Pi

Now we will talk about the two modules that I mentioned.

1.1 HQ Camera module

This module is the latest Raspberry Pi camera accessory. It offers higher resolution (12 megapixels, compared to the previous 8 megapixels) and sensitivity (approximately 50% more area per pixel for improved performance in low light conditions) than the existing v2 camera module, and is designed to work with interchangeable C and CS mount lenses. Other lenses can be used using adapters.

6mm CS mount lenses and 16 mm with C-mount are examples of all the compatible ones that exist. The high-quality camera offers an alternative to the camera module v2.

For industrial and consumer applications, including security cameras, that require the highest levels of visual fidelity and / or integration with specialized optics. It is compatible with all Raspberry Pi models, the latest version of software.

Image by Raspberry Pi
Image by Raspberry Pi

1.2 Raspberry Pi Camera Board v2 (8 Mp)

This 8 Mp camera module is capable of capturing 1080 px video and images, it can be connected to all Raspberry Pi models. Ready to plug and play, well suited for lap photography, video recording, or for use in security and motion detection applications. Just connect the included cable to the CSI port of the Raspberry Pi.

The module is small measuring 25 mm x 23 mm x 9 mm and has a weight of 3 grams, making it perfect for mobile applications or others where weight is a very important factor.

The sensor has a resolution of 8 megapixels and has a focusing lens. As for images, the camera is capable of taking still images up to 3280 x 2464 pixels and 1830p30 videos.

Image by Raspberry Pi

2. Camera setup

2.1 Connection

All current Raspberry Pi models have a port to connect the camera module.

Image by Raspberry Pi

Note: If you want to use a Raspberry Pi Zero, you need a camera module cable that fits into the smaller camera module port of the Raspberry Pi Zero.

Connect the camera module. Make sure your Raspberry Pi is turned off.

1. Locate the camera module port

2. Gently pull up on the edges of the plastic port clip

3. Insert the flat cable of the camera module (Note: Make sure the cable is in the correct direction. The blue side of the cable is facing the jack connector and the USB ports)

4. Put the plastic clip back in place.

Gig by Raspbery Pi
Image by Raspberry Pi

Connecting the HQ module in my case would look like this:

Image by Eduardo Padron

I opted to only use the camera mount of the 3D model with the Raspberry so I will show you a bit of how to use the screws and the final result.

Image by Eduardo Padron

Now I just need to connect the lens that on this occasion I used the 6mm one, you can skip this part and go to 2.2 if you used the 8 Mp V2 option.

Image by Raspberry Pi

We have to remove the dust cap for the 6mm lens and the C-CS adapter so it would be as follows:

Image by Raspberry Pi

The lenses come with two covers, one on the side of the lens and the other for the input that connects to the module.

2.1.1 Fitting the lens

The 6mm lens is CS mount so you do not need the C-CS adapter ring. It won’t focus properly if the adapter is installed, so remove it if necessary.

Then turn the lens clockwise all the way to connect it with the back focus adjustment ring. It should not be squeezed too hard because you can damage the lens or the module, just keep it tight.

Image by Raspberry Pi

2.1.2 Back focus adjustment ring and lock screw

The back focus adjustment ring should be fully screwed in for the shortest possible back focal length. Use the back focus locking screw to make sure that it does not move from this position when adjusting the aperture or focus (When purchasing the HQ module, it includes a screwdriver of the exact size to be able to manipulate the locking screw).

Image by Raspberry Pi

Once ready and installed, our lens will look like the following.

Image by Eduardo Padron

Once the lens is installed and the module is connected to our Raspberry, we can continue with the configuration when turning on and entering through VNC.

2.2 Raspberry Pi Configuration by VNC.

  1. Turn on your Raspberry Pi.
  2. Enter via VNC from your computer to your Raspberry.
  3. Go to the main menu and open Raspberry Pi Configuration.
Screenshot by Eduardo Padron

Select the tab Interfaces and make sure the camera is enabled.

Screenshot by Eduardo Padron

Reboot your Raspberry.

Screenshot by Eduardo Padron

Once restarted, we will make some changes in VNC within the Raspberry desktop, when executing some commands it will show us the preview of what it is going to capture in VNC it is not possible unless we make these changes. The first thing is to go to the VNC icon and click.

Screenshot by Eduardo Padron

The following window will open, where we will find a box on the right side, we will click.

Screenshot by Eduardo Padron

We will click on “Options”.

Screenshot by Eduardo Padron

We will go to the tab “Troubleshooting”.

Screenshot by Eduardo Padron

We will click on the “Enable direct capture mode” box and then we will click on Apply. This will allow us to see the preview of what our camera module is capturing. It will go to black screen for a few seconds and when it recovers the image it will be ready, it is not necessary but you can restart the Raspberry to start with the next step.

Screenshot by Eduardo Padron

At the moment the camera is out of focus so we need to run a command to access the view. So let’s open the Raspberry terminal.

Screenshot by Eduardo Padron

raspistill is a command tool for capturing images from the camera. To verify that the camera is installed correctly and use the camera only as a viewfinder, without saving a photo, enter this command:

raspistill -t 0

It will give us a blurry image for sure in the case of the HQ module for the V2 module it will not have a major problem.

Screenshot by Eduardo Padron

Now we will see the steps to focus the module with its lens.

Screenshot by Eduardo Padron

2.2.1 Aperture

To fix this you have to adjust the aperture, keep the camera with the lens away from you.
Rotate the middle ring while holding the outer ring, farthest from the camera, stable. Shift clockwise to close the aperture and reduce image brightness. Turn counterclockwise to open the opening. Once you are happy with the light level, tighten the screw on the side of the lens to lock the aperture.

Image by Raspberry Pi

2.2.2 Focus

First, lock the inner focus ring, labeled.

“NEAR FAR”, in position by tightening its screw. Now hold the camera with the lens facing away from you. Hold the two outer rings of the lens and turn clockwise.

Until the image is in focus, it will take four or five full turns. To adjust the focus, turn the two outer rings clockwise to focus on a nearby object. Rotate counterclockwise to focus on a distant object you will most likely need to adjust the aperture again after this each time you remove and attach the lens.

Image by Raspberry Pi

After focusing, we will have to close the window, maybe we cannot because the image is very large and the close icon does not appear, in that case we will have to access by SSH and use the command:

sudo reboot

To be able to access again by VNC.

2.2.3 Test image.jpg

You should now see a clear image and you can take a test photo by entering the command:

raspistill -o test.jpg

When you press ENTER, a live preview image will appear, and after a predetermined period of five seconds, the camera will capture a single still image. This will be saved in your home folder and named test.jpg.

Screenshot by Eduardo Padron

This is what the image I took with this command looks like:

Image by Eduardo Padron

2.2.4 Video test

To record videos, raspivid is what you need. Try it with this Terminal command:

-t 10000 is the time that it will be recording video, 10000 milliseconds would be 10 seconds of recording. h264 is the video format to play in vlc, a player that includes the Raspberry Pi OS, in case you want to change the format to MP4 which is a more common and friendly format for editing, at the end I will leave you some commands and links for this .

raspivid -t 10000 -o testvideo.h264
Screenshot by Eduardo Padron

We can see the preview of the file and then in the Raspberry Pi folder we will find the file to be able to reproduce it.

Screenshot by Eduardo Padron

3. Control the camera module with Python code

3.1 Preview

The Python library picamera allows you to control your camera module and create amazing projects.

Open a Python 3 editor, such as Thonny Python IDE or taking advantage of the previous tutorial [VS Code on Raspberry Pi] (https: //) we can use the VS Code, we will create a folder on the desktop called camera or camera, we will right click to Open the options, we will select “Open current folder in terminal”, to directly open the folder and the terminal, you can also do this in the terminal with the cd command to locate us in the camera folder.

Once located inside the terminal in the folder we will use the command:

code .

With this we will directly open VS Code in the folder that we are going to work with.

Screenshot by Eduardo Padron

Open a new file and save it as camera.py.

Note: it is important that you never save the file as picamera.py.

In VS code we can find the icon to create a new file.

Screenshot by Eduardo Padron

Now we will click on “CTRL + S” to save and the following window will appear. We enter the name of our file to be able to enter the code.

Screenshot by Eduardo Padron

Before entering the code we must make sure that we have the Python tool for VS Code installed. Verify with the image that you have installed the one that we will use.

Screenshot by Eduardo Padron

Enter the following code that will allow us to do the same function of giving us a preview image to verify that our lens is in focus. If you have already done this step and have not disconnected the camera, you can skip this step and go to the following code:

Code by Eduardo Padron
Screenshot by Eduardo Padron

We use “CRTL + S” to save and now we will open a terminal in VS Code for this project.

Screenshot by Eduardo Padron

Now we can see the terminal in the lower part and we only need to run the lines of code with the button that has the VS Code in the upper right part. You can also do this from the terminal in the traditional way and use the command:

sudo python3 camera.py
Screenshot by Eduardo Padron

You can see at the bottom the execution of the command.

Screenshot by Eduardo Padron

When executed, it will open a very large window that will close after 5 seconds as we have defined in sleep (5), we have to define a time if the window will not remain open as in raspistill -t 0 then we would have to use SSH to enter the sudo command reboot to enter again and continue with the next code.

3.2 Photo

Now to take some pictures you can create a second file in my case call it cameratake.py, modify your code to add a camera.capture() line:

Code by Eduardo Padron

Note: it is important to use a sleep of at least two seconds before capturing an image, because this gives the camera sensor time to detect light levels.

Screenshot by Eduardo Padron

You should see the open camera preview for five seconds and then a still image should be captured. While the image is being taken, you can watch the preview briefly adjust to a different resolution. Your new image should be saved to the desktop.

Screenshot by Eduardo Padron

Image taken by the code:

Image by Eduardo Padron

3.3 Video

Now shoot a video!

Modify your code to remove capture () and instead add start_recording () and stop_recording () to start and stop recording. Your code should look like this now:

Code by Eduardo Padron

Run the code. Your Raspberry Pi should open a preview, record 10seconds of video, and then close the preview.

Screenshot by Eduardo Padron
Screenshot by Eduardo Padron

4. Extras

4.1 Video format MP4

Raspberry Pi captures video as a raw H264 video format. Many media players will refuse to play it, or play it at the wrong speed, unless it is “wrapped” in a suitable container format like MP4. The easiest way to get an MP4 file with the raspivid command is using MP4Box.
Install MP4Box with this command:

sudo apt install -y gpac

Capture your raw video with raspivid from the terminal and wrap it in an MP4 container like this:

Code by Eduardo Padron

Alternatively, wrap the MP4 format around your existing raspivid output, with:

MP4Box -add video.h264 video.mp4

With this we come to the end of this guide you can practice making a code in Python to leave your video in MP4 format putting into practice the acquired knowledge, to review more about the MP4 format in Raspberry visit raspivid.

You can take advantage of the Git and VS Code guide to upload these codes to your Github.

If you want to know more about Raspberry Pi and the use of the camera visit Camera Raspberry Pi..

Visit my repository on Github.

--

--

--

A new tech publication by Start it up (https://medium.com/swlh).

Recommended from Medium

TNN: How to Choose the Right Camera for Travel

Why does Google Earth show dark spots on certain locations?

How To Not Lose In A Competition

How to enhance your old photos using GFP-GAN?

My Tune In With Surroundings

Studio shoot with Madické, french model. Paris

Nothing more ominous than the first stain of ink on an empty paper.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Eduardo Padron

Eduardo Padron

Hydraulic Engineer / Hardware and Software Developer. Science, technology and research enthusiast , development of IoT projects.

More from Medium

Create a Webcam Image Capture Utility with Python and the OpenCV API

Wildlife Trekker Bird and Critter Cam.

Python — Sending Push Notifications To Your Phone

Play Music Using Python Subprocess

How to Create & Structure a Complex Tkinter Application [2022]