Pan tracking webcam using OpenCV and Arduino

Table of Contents

1 Overview

1.1 OpenCV

The various OpenCV windows displayed while the program is running:

(That's Linux, using the Sawfish window manager with mxflat theme.)

1.2 Arduino

I'm using a Philips SPC900NC webcam (from which I removed the part used to fix it on a monitor). I used adhesive tape to bind the motor support to the webcam.

The Arduino board and the motor come from the Arduino kit prepared by Sparkfun. See



1.3 Example

A video showing how the webcam is controlled accordingly to the object to track:

(You need Javascript or a HTML5 capable browser to play the video.)

2 Source code

There are 2 programs:

  1. the OpenCV program which track the object from the webcam and send orders to Arduino to position the servo motor. You can download it from
  2. the Arduino program which receive orders from the OpenCV program. You can download it from

The protocol is very simple between OpenCV and Arduino: the program transmit one integer per line through the serial connection (using the USB connection or a RS232 port). Each integers gives the position (in microseconds) of the servo motor to set.

Author: Frédéric Jolliton <>

Date: 2011-04-01 15:01:01 CEST

HTML generated by org-mode 7.4 in emacs 23