Object Tracking Webcam

In this project I show you how to build a automatic tracking system for a webcam using Mathematica, Arduino and a Servo motor.

Why Mathematica you may ask? Because it is available to all undergraduates at UCL and too expensive for those of you not at UCL.

To start with you will need to install Mathematica available from here for UCL people or directly from Mathematica.

You will also need to install the Arduino Software.

Download all the code that you will need to control the system here.

Step 1:

Connect the Arduino board and run the Arduino blink example to make sure everything is up and running.

Step 2:

Mounting blocks for servo motor

Mount the servo motor.

Connect the Servo motor to the Test the servo motor Arduino board. The servo has three wires: +4.5-6V (red), Ground (Black or Brown), Control (Orange or White). Servo motors used closed loop control to move to a particular position over the range of 0 to 180 deg. The position is determined by the control signal which uses Pulse Width Modulation (PWM). Typically pulse widths of 1ms and 2ms  correspond to 0 and 180 degs respectively.

Mounted Servo

Step 3:

Run the Arduino Servo Example to test your servo motor. Note the servo when running draws significant current from the Arduino. Programming the Arduino also draws more current than standard operation. Thus before downloading the code unplug the servo motor if you are running the Arduino board off USB power only.

Step 4:

Download the ServoSerialControl Sketch to the Arduino. This sketch implements a serial listener that sends whatever byte the is received to the servo motor controller. This is the code that will allow us to control the Servo motor postion from within Mathematica.

Step 5:

Run the SerialLinkRefresh.nb in Mathematica to test the serial port and servo motor control from within Mathematica. Note for this code to work on Windows you must have .NET version 2.0 or higher installed.

If the following code runs then you have .NET


You can then test the servo with the following code. Note you first need to click the open button to open the serial port. Please also close the serial port when finished before closing the notebook, otherwise you will need to restart Mathematica and disconnect the USB cable to get going again.

ser =
NETNew["System.IO.Ports.SerialPort", "COM10"]
ser[BaudRate] = 9600;
run = False;
Button["open", ser@Open[]; "Serial Port Open"]
Button["close", ser@Close[]]

Button["MidPoint", ser@Write[MakeNETObject[{90}, "System.Byte[]"], 0, 1]]
Button["Start", ser@Write[MakeNETObject[{4}, "System.Byte[]"], 0, 1]]
Button["End", ser@Write[MakeNETObject[{180}, "System.Byte[]"], 0, 1]]

DynamicModule[{run = False, x = 90}, {Checkbox[Dynamic[run]],
Slider[Dynamic[x], {14, 170, 1}], Dynamic[x],
Refresh[ser@Write[MakeNETObject[{x}, "System.Byte[]"], 0, 1],
UpdateInterval -> 0.1]; "Enabled", "Not enabled"]]}]

Step 6:

Plug in the web cam and test the object tracking software by running the ObjectTracking.nb

Step 7:

Camera Mount

Full System

Mount the Webcam on the servo

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s