Wednesday, January 14, 2009

Arduino Webcam Servo Project

Having received my first Arduino I had to come up with a good first microcontroller project.  Something not too complicated but more than just a book example.  I had just purchased a wireless webcam (Linksys WVC54GCA) and thought it would be cool if I could move it around on a pan & tilt mechanism.  And how much cooler would it be to be able to control it over the internet?

After I completed this project I looked around the internet and found several other examples of this type of thing.  But I hope the documentation here is still useful as I don't recall any examples showing detail of how to use the Arduino and more specifically, the Xport in the designs.

Here are the pieces I used:

* Arduino ethernet shield (
* 2 servos from my crashed RC glider
* The mechanical bits.  I used machined aluminum but you can use anything you like


I started with Ladyada's examples (

You can find my Arduino code here:

You'll need to make sure you include the necessary libraries (AF_Xport.h, AFSoftSerial.h, ServoTimer1.h) which are needed by the code.

The code is fairly straightforward except for the bits about the web page.  Being an HTML rookie, I struggled a bit, but keeping things simple and using MS Frontpage as a crutch, I managed to create a very simple webpage.  And it needs to be simple to fit within the limited memory of the Arduino.  The large char strings early on in the code store the HTML.  Note that for every quotation mark in your HTML, you need to add a \ before so that you don't terminate the string.

One key is to enable a variable via the webpage pushbuttons.  Each pushbutton assigns a different value to the variable which tells the program which servo to turn as well as the direction.  This line of code was important to understanding how web pages transmit user input:

found = strstr(linebuffer, "?servo="); // "?inputfield=" GET request

Printing out the line buffer in the serial window allows you to see exactly the info that your web page is sending out upon a button push.  You look for this information in the line buffer and then extract the useful part - in this case, the value to indicate which button was pushed.

Another slight glitch I found was that the baudrate had to be reduced from that used in ladyada's example.  I don't know why.


One more area of complication is accessing the webpages.  The Linksys webcam has its own internal webserver.  This is accessed remotely by enabling port forwarding on your router.  To access this remotely, make use of your computer's ip address (not your local network) followed by a colon, and then the port that the webcam uses.  For example (this is a fake ip addy): 

The Linksys happens to use 1024 as its port.  You do exactly the same for the Xport, except it uses a different port which is specified during the Xport setup process (see the ladyada site for instructions).  In my case it was 10001.

Note that your ip provider probably provides a dynamic ip address to your computer, meaning that it may change over some time (days, weeks, months).  To deal with this situation you may want to make use of a DNS service provider.  I used which is a free service.  This allowed me to use a semi-custom name that is linked to your computer's ip address (for example, something like  When your ip address changes, software on your computer updates the DNS service provider.

Here's an example of the router setup to enable the port forwarding:


These are stupidly simple.  I merely plugged the servos into ground and pins 9 and 10.


I was lucky to have access to aluminum scraps and a machine shop.  I have limited skills with the equipment but was able to fashion some workable parts.  You can easily use any material you like.  I happened to put the pan servo mechanism on the bottom and then attached the tilt mechanism to that.  

The entire mechanism was mounted on a tripod using a tapped 1/4" hole in the baseplate.  Here are some additional pics of the mechanism.


  1. Very Cool... Can you verify the link to google docs for your code? I currently get a blank page.

  2. Sorry for the missing code - I will have this fixed in a few hours...

  3. This is great! So... When are you going to make that webpage accessible so WE can move it around? ;)

  4. Very cool, I am planning on using a similar servo setup to build a laser pointer controlling robot to play with the cats when we are not around.

  5. Very nice, I did a quick'n'dirt port for the wiznet module which can be found here:

    Servos are connected to port 2 and 3, you need the SoftServo Library (

  6. I am very interested in your use of the hardware counter with the encoder. Could you please help me with some code to just do that and output the number?


  7. xport has been discontinued do you think that the new model can be used ?

  8. I am using an xport direct+ in, for your intents and purposes, a similar project and it works fine.

    I do have 1 issue though, and I'm wondering if you (Dennis, the author) have had any like problems. That is, the webpage occasionally doesn't appear to load, but when I check the source, it's all the HTML that should be printed. It just appears like a blank page. Reloading the page usually fixes it, but still, that shouldn't need to be done. Any ideas?

  9. great job. Im looking at arduino, and have a little electronic experience from high school. Do you recommend any good online stores to but general electronics kits and components?

  10. i'm needing to know how to create the small webpage with the up,down,left,right,center controls and i need to know how to make the webpage control the arduino. please email me at

  11. Hey man,

    Firstly I would like to say I am a huge fan of your work.

    Me and my friends were trying to do the Arduino Webcam Servo Project. We have done everything exactly as you instructed.

    But it does not work. The code compiles onto the arduino properly. We have configured the shield via telnet exactly like you specified. The wirings have been done exactly as the pins you specified too. But the html loaded on the system does not want to load. It keeps timing out regardless of however ways we have tried.

    Also one point to note is that since the XPort direct has been discontinued so we are using hte XPort direct+ !

    We are eagerly hoping from a response from you,

    Thanking you in advance


    Mehran Reza

  12. Hi Dennis,

    I have questions regarding some sensor integration with Arduino.
    Can you help me?


  13. Hi! What type of strange gun appears in the video, inside a sort of briefcase?
    Great project BTW!!!