Headless Raspberry Pi HQ camera setup and focusing

Raspberry Pi officially offers two cameras: the Camera Module v2 (until January 2024) and the HQ camera. On their website, you can find a side-by-side comparison of the hardware specs. Both are relatively straightforward to setup, but a lens needs to be purchased for the HQ camera and focused. There are two lenses that can be readily mounted: a 6mm CS-mount lens and a 16mm C-mount lens. In this tutorial, we will setup the HQ camera and see how to focus the lens in real-time. As with the other setup tutorials, we will assume that you have no external screen for the Raspberry Pi.

What you need:

1) Connecting the camera

  1. Make sure the Raspberry Pi is off.
  2. Pull up the edges of the Camera Module port’s plastic clip.
  3. Insert the camera’s ribbon cable. Make sure the connectors at the bottom of the ribbon cable are facing the contacts in the port (blue side facing the Ethernet port).
  4. Push the plastic clip back into place.

Below is a nice visual for the Camera Module v2. It is the same idea for the HQ camera.

2) Software setup

Start up your Raspberry Pi and connect via SSH (as described here):

ssh <username>@<remote>

where <username> and <remote> should be set to your system’s configuration.

Run the following command once logged in:

sudo raspi-config

With your arrow keys, navigate to Interfacing Options, then Camera, and follow the prompt to enable the camera (note this may appear as Legacy Camera on the Bullseye OS).

Reboot for the changes to take place.

sudo shutdown -r now

3) Taking a picture

We can already take a picture with the camera connected and enabled. However, it will be completely out of focus as we have no lens. In any case, let’s see what those commands look like.

Connect to your Raspberry Pi via SSH and run the following command to take a picture (as of Bullseye).

libcamera-jpeg -o test.jpg

Older versions of the Raspberry Pi OS may not have libcamera, although it is recommended to install this library as Raspberry Pi will be transitioning to this software. For Raspberry Pi OS’ before Debian Bullseye (or the Legacy Camera), you can take a picture with the following command.

raspistill -o test.jpg

From your local machine, we can copy over the captured image with the following command.

scp <username>@<remote>:~/test.jpg .

Using your favorite image viewer, we can take a look at our unfocused image.

4) Focusing the lens

Both the 6mm CS-mount lens and the 16mm C-mount lens have essentially two knobs that we need to adjust: one for the aperture size and another for the focus. When using the 16mm lens, make sure the C-CS adapter that comes with the HQ camera is fitted to the lens. Conversely, it should be removed for the 6mm lens. Screw in your desired lens and screw in fully the back focus adjustment ring.

If we were to take a picture just after screwing on the lens, the captured image would be better but still out of focus. Unless you were extremely lucky to have it focused!

What we will need is a live-feed in order to adjust the aperture and focus knobs. To this end, we will use the VNC viewer.

4.1) Live-feed through VNC viewer

Connect to the Raspberry Pi from your local machine with VNC viewer. Enter your Raspberry Pi’s hostname or IP address at the top.

Bullseye OS

VNC has been reported to be quite laggy with Bullseye (forum). Commenting out the KMS driver in /boot/config.txt is one solution, but we found that it was not possible to use the camera afterwards.

Instead from sudo raspi-config , navigate to “Display Options” > “VNC Resolution”.

And set the resolution to a lower value.

If you don’t see the “VNC resolution” option, you may have to update your raspi-config.

After rebooting, open a Terminal (from VNC viewer) and enter the following command to launch a preview:

libcamera-vid -t 0

Prior to Bullseye OS

Open a Terminal and enter the following command to launch a preview:

raspivid -t 0

If you don’t see any preview popping up, you may have to enable the direct capture mode (from the Raspberry Pi).

4.2) Focusing with live-feed

This video (starting at 10:21) does a pretty good job in explaining how to focus the lens. You can also find some nice visuals from the guide for the CS-mount lens. Below are the basic steps:

  1. While holding the front ring, adjust the Aperture until you have a sufficient amount of light.
  2. Tighten the Aperture screw.
  3. Adjust the focus by rotating the front two rings.
  4. (Optionally) adjust the brightness by rotating the Aperture ring while keeping the front ring in place.

We can take another picture and copy it to our local machine as before.

# on Raspberry Pi
libcamera-jpeg -o test.jpg # Bullseye
raspistill -o test.jpg # prior to Bullseye

# on local machine
scp <username>@<remote>:~/test.jpg .

Much nicer 📷🐒🌱

5) Alternative approaches for live feed

If VNC isn’t working too well for you, you can try these approaches.

VLC

From the Raspberry Pi (Bullseye OS):

libcamera-vid -t 0 --inline --listen -o tcp://0.0.0.0:8888

Or with raspivid:

raspivid -t 0 -w 600 -h 400 -ih -fps 10 -l -o tcp://0.0.0.0:8888

Then from VLC on your local machine (install here), “Media” > “Open Network Stream…”, enter the URL:

tcp/h264://XXX.XXX.XXX.XXX:8888/

And press play!

Motion software (not compatible with Bullseye)

Note that as of writing (Nov 23, 2021), the motion software is not available on the Bullseye OS.

From the Raspberry Pi, install the webcam motion software.

sudo apt-get update && sudo apt-get install motion

Enable the motion daemon by editing the /etc/default/motion file, e.g. with Vim (which you can install with sudo apt-get install vim if it is not already installed on your Raspberry Pi).

sudo vim /etc/default/motion

Set start_motion_daemon=no to yes. To edit a file on Vim, press i and navigate to the place you would like to edit.

INSERT mode to edit a file with Vim.

To exit the INSERT mode, press, Esc.

To save and close a file, type :x and hit Enter.

Next, we have to edit the /etc/motion/motion.conf file.

sudo vim /etc/motion/motion.conf

Make sure the following are set.

daemon on
width 640
height 480
framerate 100
stream_localhost off

On Vim, you can search for something by typing /<content>, where <content> is what you would like to search for. Be sure to be out of INSERT mode (pressing Esc will make sure of this). Press Enter to set the cursor at the highlighted portion.

As before, press Esc to be out of INSERT mode, and type :x followed by Enter to save and close the file.

Reboot your Raspberry Pi for the changes to take effect. After SSH’ing back onto the Raspberry Pi, launch the live-feed with the following command.

sudo service motion start

Determine your Raspberry Pi’s IP address with ifconfig.

It should be the IP address associated with wlan0 and normally have the form XXX.XXX.XXX.XXX.

From your local machine, open a browser and go to <ip_address>:8081 . You should now have a live-feed from your HQ camera in order to focus the camera!

When you are happy with focus, you can turn off the live-feed by running the following command.

sudo service motion stop

Moreover, set start_motion_daemon back to no in /etc/default/motion so that the daemon doesn’t start at boot and block the camera.

6) Extra links

HQ camera product page: https://www.raspberrypi.com/products/raspberry-pi-high-quality-camera/

Camera setup with VNC: https://medium.com/geekculture/camera-setup-on-raspberry-pi-4-912e7d415cdf

Live-feed with Python: https://makezine.com/projects/beginner-project-a-remote-viewing-camera-with-raspberry-pi/

Auto-focus solution by Arducam: https://www.arducam.com/raspberry-pi-camera/autofocus/

Using Raspberry Pi camera with Bullseye OS: https://www.tomshardware.com/how-to/use-raspberry-pi-camera-with-bullseye