Projects / Pan-tilt camera tracker

Pan-tilt camera tracker

Intermediate4 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. Pi 4RASPBERR33°

    Fit the camera on the bracket

    Cable with some slack.

  2. Pi 433°66°

    Test the servos

    Sweep each from the Python prompt.

  3. Pi 4RASPBERR

    Check the camera picture

    Save one frame.

  4. Pi 4

    Tune the colour range

    Print the mask.

  5. Pi 4

    Run the tracker

    Move the object slowly.

  6. Pi 4

    Fix the sign if needed

    Swap minus and plus.

  7. Pi 4

    Tune KP

    Until it settles without swinging.

In short

How do you build a pan-tilt camera tracker?

A camera on a pan-tilt bracket keeps a coloured object in the centre of the frame. You need Raspberry Pi 4 Model B (4 GB), SanDisk microSD card 32 GB (A1, Class 10), Raspberry Pi 27 W USB-C power supply, Raspberry Pi Camera Module 3, Servo pan-tilt bracket (SG90) and PCA9685 16-channel servo driver. It takes 7 steps and about 240 minutes.

Level
Intermediate
Time
About 240 min
Parts
6

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
PCA9685 SDA and SCLPi GPIO2 and GPIO3 (pins 3 and 5)I2C servo driver
PCA9685 VCC and GNDPi 3V3 (pin 1) and GNDLogic power
PCA9685 V+ and GNDSeparate 5 V supply of 2 A or more, ground joined to the PiServo power
Pan servoPCA9685 channel 0Turns left and right
Tilt servoPCA9685 channel 1Tilts up and down
Camera ribbonPi camera connectorCamera, fixed on the tilt bracket

Code

The camera finds the object by its colour: the frame is converted to HSV, where colour is easy to separate from brightness, and a mask keeps only pixels in the chosen blue range. The biggest blob is the target, its centre is compared with the centre of the picture, and the error, in pixels, is multiplied by a small gain to move each servo a little, which makes a proportional control loop. If it overshoots and swings, lower KP; if it follows too slowly, raise it. Fixed servo limits keep the bracket from hitting its stops. Power the servos from their own 5 V supply on the PCA9685's V+ terminal, and join the grounds, because servo current spikes can reset the Pi when they share its supply. Tune the HSV range under the lighting you will use, since colour changes with the light, and try a bright ball or a card. This follows an object by colour only; do not use it to follow or watch people without their knowledge. Raspberry Pi OS (Bookworm or newer) with Python 3.

# Pan-tilt tracker: finds the biggest blue object in the picture and nudges two servos to keep it at the centre.
# Install first:  sudo apt install python3-opencv python3-picamera2   and   pip install adafruit-circuitpython-servokit (in a virtual environment)
import cv2, numpy as np
from picamera2 import Picamera2
from adafruit_servokit import ServoKit

W, H = 320, 240
kit = ServoKit(channels=16)
pan, tilt = 90.0, 90.0
KP = 0.03                                   # servo degrees per pixel of error; lower it if it overshoots
LOWER = np.array([100, 150, 60])            # blue range in HSV; change for another colour
UPPER = np.array([130, 255, 255])

cam = Picamera2()
cam.configure(cam.create_preview_configuration(main={"format": "RGB888", "size": (W, H)}))
cam.start()
kit.servo[0].angle = pan
kit.servo[1].angle = tilt

while True:
    frame = cam.capture_array()             # picamera2 RGB888 arrives in BGR order for OpenCV
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(hsv, LOWER, UPPER)
    mask = cv2.erode(mask, None, iterations=2)
    mask = cv2.dilate(mask, None, iterations=2)
    contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    if contours:
        c = max(contours, key=cv2.contourArea)
        if cv2.contourArea(c) > 300:
            m = cv2.moments(c)
            cx, cy = m["m10"] / m["m00"], m["m01"] / m["m00"]
            pan = min(160, max(20, pan - KP * (cx - W / 2)))      # swap the minus and plus if it moves the wrong way
            tilt = min(150, max(30, tilt + KP * (cy - H / 2)))
            kit.servo[0].angle = pan
            kit.servo[1].angle = tilt

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Blue ball in the middleServos stay still
Move it leftCamera turns left to follow
Move it upCamera tilts up
Remove itServos hold their position

If it does not work

Moves the wrong way

Swap the sign in that axis.

Swings back and forth

Lower KP.

Pi resets

Power the servos from their own supply.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the pan-tilt camera tracker?

Raspberry Pi 4 Model B (4 GB), SanDisk microSD card 32 GB (A1, Class 10), Raspberry Pi 27 W USB-C power supply, Raspberry Pi Camera Module 3, Servo pan-tilt bracket (SG90) and PCA9685 16-channel servo driver.

How long does the pan-tilt camera tracker take to build?

About 240 minutes for a intermediate maker, in 7 steps.

How do I know the pan-tilt camera tracker works?

Blue ball in the middle: Servos stay still. Move it left: Camera turns left to follow. Move it up: Camera tilts up.

Moves the wrong way: what should I check?

Swap the sign in that axis.

Swings back and forth: what should I check?

Lower KP.

Pi resets: what should I check?

Power the servos from their own supply.