Raspberry Pi Camera Auto Focus, Explained (HQ, V2 and V1)

This article explains everything you need to know about the Raspberry Pi camera autofocus.

It applies to all Raspberry Pi Models (Pi 3, Pi 4, Pi Zero, Compute Module, and all others), and all Raspberry Pi camera modules (V1.3, V2.1, HQ).

So let’s dive right in and clarify this issue once and for all.

Prerequisites for autofocus

Here is the definition of autofocus from Wikipedia:

An autofocus (or AF) optical system uses a sensor, a control system, and a motor to focus on an automatically or manually selected point or area.

Easily, we get all the answers we need. Here are the 3 prerequisites for autofocus:

Sensor, focus motor and control system are the 3 prerequisites of autofocus

To focus is to move the lens. To autofocus is to automatically move the lens.

The focus motor is the “what” to move the lens, and the control system is the “how” to move the lens. When we focus manually, our hands function like the focus motor, and our brain functions like the control system.

Therefore, autofocus does not mean no focusing process is involved. It’s just that the focus motor and the control system take over the job for your hand and your brain.

Well, do the Raspberry Pi cameras meet these prerequisites?

Do the official Raspberry Pi cameras meet all autofocus prerequisites?

Sadly, they don’t. ​😥

Let’s go over the prerequisites list again and see how the official Raspberry Pi cameras match.

The official raspberry pi cameras only meet 1 of the 3 prerequisites, so it won’t do autofocus

The official Raspberry Pi cameras are not designed with autofocus in mind, instead, they just meet the basic needs.

The idea of the basic needs goes like this: You can get a clear picture with manual focus. The focus motors and control system not only add to the complexity but also add to the cost. Not everyone needs that. No need to meet all autofocus prerequisites.

Does this mean the official Pi cameras can’t do autofocus?

Yes. All of the 3 official Raspberry Pi camera modules (V1/V2/HQ) cannot do autofocus themselves, period.

Does this mean there is no hope for Pi cameras to do autofocus?

Absolutely no. Arducam has fixed the issues and made it possible.

How does Arducam meet the Raspberry Pi Camera Autofocus prerequisites?

The logic to fix the issues is simple:

If the official Raspberry Pi cameras don’t have a focus motor and a control system, then we build variants that do.

Let’s break down the two unmet prerequisites one by one.

1. Add Focus motor to the Pi camera

First, it’s the focus motor.

Let’s consider the focus motor a part of the lens assembly.

In case you don’t know, there are two kinds of lens assembly in Raspberry Pi cameras:

The detachable compact camera module on Raspberry Pi Camera Module V2
The Raspberry Pi High Quality Camera has the image sensor on the camera PCB

That leads us to two methods to design a Pi camera that support autofocus:

CCM Autofocus replacement on the official V2 Camera Board
Special Autofocus lens assembly on Arducam’s own variants of IMX477 Camera Board

2. Add Control system (so the Pi can control the focus motor)

Second, it’s the control system. The Raspberry Pi should be able to control the focus motor.

Let’s consider how the Raspberry Pi is connected to the focus motor before we set up the connection:

From above, we know that the key physical connection is the MIPI CSI camera ribbon cable. So, do we have anything on the MIPI CSI connection that can help us control the motor?

I2C bus. More specifically, the I2C for the VideoCore processor.


Set to “on” to enable the i2c interface, usually reserved for the VideoCore processor (default “off”).

I2C in raspberry pi camera pinout, useful for controlling the focus motor

So, we use the i2c_vc to control the motor. What we need to do is to add the following line to the /boot/config.txt.


Now that we’ve met the prerequisites, what else is needed?

How to enable the autofocus feature once we’re ready?

The next thing to do is to automate the focusing process with a program.

Step 1: Software-controlled motorized focus

Let’s first get the motors moving.

With a simple python program, we can use the keyboard keys to move the focus motor.

See the keyboard focus control demo.

If you are interested in how we’ve done that, here is the python code for software motorized focus control.

import os
import time
import sys
import threading
import pygame,sys
from pygame.locals import *
from time import ctime, sleep
def runFocus(func):
temp_val = 512
while True:
for event in pygame.event.get():
if event.type ==KEYDOWN:
print temp_val
if event.key == K_UP:
print 'UP'
if temp_val < 1000:
temp_val += 10
temp_val = temp_val
value = (temp_val<<4) & 0x3ff0
dat1 = (value>>8)&0x3f
dat2 = value & 0xf0

os.system("i2cset -y 0 0x0c %d %d" % (dat1,dat2))
elif event.key==K_DOWN:
print 'DOWN'
if temp_val <12 :
temp_val = temp_val
temp_val -= 10
value = (temp_val<<4) & 0x3ff0
dat1 = (value>>8)&0x3f
dat2 = value & 0xf0
os.system("i2cset -y 0 0x0c %d %d" % (dat1,dat2))

def runCamera():
cmd = "sudo raspistill -t 0"
if __name__ == "__main__":
t1 = threading.Thread(target=runFocus,args=("t1",))

Now we get a camera module with a software-controlled focus motor. That’s already a big step forward. You won’t have to do the focus near the camera module with your bare hands. Instead, you can sit remotely and adjust the focus with software.

Adjust the focus of a Pi camera with keyboard arrow keys

However, if we want to enable autofocus, we need the Raspberry Pi to decide whether the camera is in focus, and it should stop moving the focus motors. But how would the Pi know?

Step 2: Autofocus enabled by OpenCV

As the biggest open-source computer vision library, OpenCV can help us make the Pi smart enough to tell whether the camera is in focus — by contrast detection.

sudo apt-get install python-opencv

The intensity difference between adjacent pixels of the sensor naturally increases with correct image focus. Then the Pi can adjust the motor until the maximal contrast is detected.

Arducam provides an OpenCV-powered autofocus example in Python. Here is how the image looks like on Raspbian when it runs.

See the Raspberry Pi Autofocus Demo.

If you are interested in how we’ve done that, here is the python code for autofocus.

import cv2 #sudo apt-get install python-opencv
import numpy as py
import os
import time
import smbus
bus = smbus.SMBus(0)
import picamera
from picamera.array import PiRGBArray

def focusing(val):
value = (val << 4) & 0x3ff0
data1 = (value >> 8) & 0x3f
data2 = value & 0xf0
# time.sleep(0.5)
print("focus value: {}".format(val))
# bus.write_byte_data(0x0c,data1,data2)
os.system("i2cset -y 0 0x0c %d %d" % (data1,data2))

def sobel(img):
img_gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
img_sobel = cv2.Sobel(img_gray,cv2.CV_16U,1,1)
return cv2.mean(img_sobel)[0]

def laplacian(img):
img_gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
img_sobel = cv2.Laplacian(img_gray,cv2.CV_16U)
return cv2.mean(img_sobel)[0]

def calculation(camera):
rawCapture = PiRGBArray(camera)
camera.capture(rawCapture,format="bgr", use_video_port=True)
image = rawCapture.array
return laplacian(image)

if __name__ == "__main__":
#open camera
camera = picamera.PiCamera()

#open camera preview
#set camera resolution to 640x480(Small resolution for faster speeds.)
camera.resolution = (640, 480)
print("Start focusing")

max_index = 10
max_value = 0.0
last_value = 0.0
dec_count = 0
focal_distance = 10

while True:
#Adjust focus
#Take image and calculate image clarity
val = calculation(camera)
#Find the maximum image clarity
if val > max_value:
max_index = focal_distance
max_value = val

#If the image clarity starts to decrease
if val < last_value:
dec_count += 1
dec_count = 0
#Image clarity is reduced by six consecutive frames
if dec_count > 6:
last_value = val

#Increase the focal distance
focal_distance += 15
if focal_distance > 1000:

#Adjust focus to the best
#set camera resolution to 2592x1944
camera.resolution = (1920,1080)
#save image to file.
print("max index = %d,max value = %lf" % (max_index,max_value))
#while True:
# time.sleep(1)


Suggestions and Recommendations

That’s everything about how we’ve made autofocus possible on the Raspberry Pi cameras. However, here are some of our suggestions:

Well, that brings us to the end of this overview.​

To conclude: The official Raspberry Pi camera modules do not support autofocus, but Arducam has made it possible to achieve autofocus by controlling a focus motor in our customized module via I2C.

​Why do you need the camera module to do autofocus on Raspberry Pi?

Leave your comment below!

Original article

See original article and product recommendations here: https://www.arducam.com/raspberry-pi-camera/autofocus/

Camera solutions expert for embedded systems - like Raspberry Pi, NVIDIA Jetson, Arduino — from lens to sensor to driver to PCB and industrial design.