Arduino Person Tracker: Closing the Loop Between a Webcam and Two Servos
A camera that follows a moving object is a standard robotics feature. Getting it to work with a webcam, an Arduino, and two servos tests the full pipeline. Vision on one end, motion on the other, and serial communication in between.
The Python script captures video, filters for a specific color using HSV thresholds, finds the largest contour, and calculates its center. It sends the center coordinates over serial. The Arduino receives them, maps the pixel values to servo angles, and moves pan and tilt servos to track the object.
The Vision Side
The image gets converted from BGR to HSV because HSV separates color from brightness. This makes the tracking more robust under changing light compared to RGB. A binary mask isolates pixels within the target color range. The mask passes to a contour finder, which returns the boundaries of all detected regions.
The largest contour by area is assumed to be the tracked object. Its bounding box gives a center point. That point gets drawn on the frame and sent over serial as a comma separated coordinate pair.
The Motion Side
The Arduino reads the incoming coordinates and maps them to servo angles. The x-coordinate range of 0 to 640 maps to pan angles from 0 to 180 degrees. The y range of 0 to 480 maps to tilt angles the same way.
The movement is smoothed by incrementally adjusting the current angle toward the target angle by a fixed step each loop cycle. This prevents the servos from snapping to position and creates a tracking motion that looks natural instead of robotic. The smoothing step is a constant. It is not adaptive to the distance the servo needs to travel.
What I Would Change
The color range is hardcoded for a red hue. Tracking a different object means editing the Python script. I would add a calibration mode that samples the color from a user-selected region in the frame.
The system tracks only the largest contour. If another object of the same color enters the frame, the tracker jumps to it. There is no continuity or object ID. A tracking algorithm like centroid tracking would fix this.
The serial port is hardcoded. Switching machines requires editing the code. The resolution is assumed to be 640 by 480. The smoothing is linear and does not account for servo load or acceleration.
It is 50 lines of Arduino and 73 lines of Python. The point was never the code count. It was that a webcam, two servos, and a serial cable are enough to make a camera follow you. That never gets old.