Fareed Mohmed← Project library

Engineering · Raspberry Pi · Project 27 of 47 · Camera and vision · Intermediate

Colour tracker that points a servo

The camera finds a red ball. A servo arm points to where the ball is. Move the ball left and right and the arm follows. The idea behind object tracking and vision robots.

▶ Open in the simulator

Raspberry Pi simulator drawing of the circuit for Colour tracker that points a servo: the board, its power supply and the parts wired to the 40-pin header
Privacy and safety. A camera records people. Tell everyone who lives in or visits the place, keep the pictures private, and delete old ones. Switch the Pi off before you plug in or remove the camera ribbon cable.

Parts in this circuit

PartType
Pi CameraPi Camera Module 3
Pointer servoServo

Wiring, pin by pin

Red = 5 V, orange = 3.3 V, black = GND, other colours = signals. “Pin” is the position on the 40-pin header; the GPIO number is what the code uses. Power the Pi from a 5.1 V supply and wire with the power off.

#ColourFrom (part and leg)To (header pin)
1ColouredPointer servo - signalPin 12 (GPIO18)
2RedPointer servo - 5 VPin 2 (5V)
3BlackPointer servo - GNDPin 6 (GND)

Python code (gpiozero)

from picamera2 import Picamera2
from gpiozero import AngularServo
from time import sleep

cam = Picamera2()
cam.configure(cam.create_preview_configuration())
cam.start()
sleep(1)

servo = AngularServo(18, min_angle=-90, max_angle=90)

while True:
    frame = cam.capture_array()
    spot = frame.find_color("red")       # (x, y, area) or None
    if spot is None:
        print("No red ball in view")
    else:
        x, y, area = spot
        fraction = x / frame.width       # 0 = far left, 1 = far right
        servo.angle = -90 + fraction * 180
        print("Ball at %3d %%  ->  servo %4d degrees" % (fraction * 100, servo.angle))
    sleep(0.2)

How the code works

You will learn

  • Finding an object by its colour
  • Turning a position in the picture into a servo angle
  • Handling the case when the object is not there

Try this

  • Drag the ball slowly across the view and watch the servo arm.
  • Choose a green ball. Change the code to find "green".
  • Use y instead of x so that the servo follows the ball up and down.

On a real Pi

  • On a real Pi find the colour with OpenCV: convert to HSV, use cv2.inRange, then cv2.moments for the centre.
  • A real pan-tilt camera moves the CAMERA, so the ball stays in the middle. That is a feedback loop: move a little towards the ball each time.
  • Power the servo from its own 5 V supply when you use bigger servos. Join the GND.

Parts to buy

The links open a search on each store, so you see what is in stock today. Check the model, voltage and price before you order.

#ItemQtyBuy / compare
1Raspberry Pi 4 Model B
about 4000 to 9000 INR
1Robu.in Amazon.in ThinkRobotics
2Official 15 W USB-C power supply (5.1 V, 3 A)
Do not use an old phone charger
1Robu.in Amazon.in ThinkRobotics
3microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS)
And a card reader to write it
1Robu.in Amazon.in ThinkRobotics
4Raspberry Pi Camera Module 3 (12 MP, autofocus)
Or a Camera Module 2 / HQ camera
1Robu.in Amazon.in ThinkRobotics
5CSI camera ribbon cable, 15-pin, 30 cm
Check the port size of your board
1Robu.in Amazon.in ThinkRobotics
6Camera mount or small tripod1Robu.in Amazon.in ThinkRobotics
7SG90 micro servo1Robu.in Amazon.in ThinkRobotics
8Dupont jumper wires, male-to-female, 20 cm (pack of 40)1Robu.in Amazon.in ThinkRobotics
9Breadboard 830 points
For resistors and shared GND / 5 V
1Robu.in Amazon.in ThinkRobotics
← 26. Motion detector with the cameraAll projects28. Colour detector with three LEDs →

This is a learning tool. The simulator checks the program logic, not the electrical design. Never put more than 3.3 V on a GPIO pin.