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.
- 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.
- 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.
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?
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.
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.
2. Camera setup
All current Raspberry Pi models have a port to connect the camera module.
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.
Connecting the HQ module in my case would look like this:
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.
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.
We have to remove the dust cap for the 6mm lens and the C-CS adapter so it would be as follows:
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.
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).
Once ready and installed, our lens will look like the following.
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.
- Turn on your Raspberry Pi.
- Enter via VNC from your computer to your Raspberry.
- Go to the main menu and open Raspberry Pi Configuration.
Select the tab Interfaces and make sure the camera is enabled.
Reboot your Raspberry.
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.
The following window will open, where we will find a box on the right side, we will click.
We will click on “Options”.
We will go to the tab “Troubleshooting”.
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.
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.
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.
Now we will see the steps to focus the module with its lens.
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.
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.
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:
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.
This is what the image I took with this command looks like:
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
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.
3. Control the camera module with Python code
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:
With this we will directly open VS Code in the folder that we are going to work with.
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.
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.
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.
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:
We use “CRTL + S” to save and now we will open a terminal in VS Code for this project.
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
You can see at the bottom the execution of the command.
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.
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:
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.
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.
Image taken by the code:
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:
Run the code. Your Raspberry Pi should open a preview, record 10seconds of video, and then close the preview.
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:
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.