Camera setup on Raspberry Pi 4 2023
Wireless camera setup in Raspberry Pi OS Bullseye
Author: Eduardo Padrón
Update: This update is for the Bullseye version from Raspberry at July 2023 with the new Python library PiCamera2
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. You need to set up your Raspberry Pi
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(Set up Raspberry Pi 4 with Raspberry OS Bullseye)
- 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.
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
2.1 Connection
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.
At this 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.
libcamera 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:
libcamera-hello -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.
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.
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.
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:
libcamera-jpeg -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 .
libcamera-vid -t 10000 -o test.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
3.1 Photo
The Python library picamera2 allows you to control your camera module and create amazing projects. Open a Python 3 editor, such as Thonny Python IDE. Open a new file and save it as cameratest.py.
Note: it is important that you never save the file as picamera.py.
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.
Then name the file.
Enter the following code that will allow us to do the same function of giving us a preview image and save the image:
import time
from picamera2 import Picamera2, Preview
picam2 = Picamera2()
preview_config = picam2.create_preview_configuration(main={"size": (800, 600)})
picam2.configure(preview_config)
picam2.start_preview(Preview.QTGL)
picam2.start()
time.sleep(2)
metadata = picam2.capture_file("test.jpg")
print(metadata)
picam2.close()
You can see at the top the run button to run the code.
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 2 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 pi folder.
Image taken by the code:
3.2 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:
import time
from picamera2 import Picamera2, Preview
from picamera2.encoders import H264Encoder
picam2 = Picamera2()
preview_config = picam2.create_preview_configuration(main={"size": (800, 600)})
picam2.configure(preview_config)
video_config = picam2.create_video_configuration()
picam2.configure(video_config)
encoder = H264Encoder(10000000)
picam2.start_preview(Preview.QTGL)
picam2.start()
picam2.start_recording(encoder, 'test.h264')
time.sleep(10)
picam2.stop_recording()
picam2.close()
Run the code. Your Raspberry Pi should open a preview, record 10seconds of video, and then close the preview.
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 libcamera-vid
from the terminal and wrap it in an MP4 container like this:
Alternatively, wrap the MP4 format around your existing libcamera-vid 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.