Pan and tilt control of webcam with PICAXE 08M


Senior Member
This project provides pan and tilt control through an 08M of a Quickcam 9000 webcam. The webcam itself runs off of a Linksys NSLU2 which has been modified to run the openWRT Linux operating system as described here:

The project uses two servos from Trossen Robotics, shown here:

The picaxe program allows the user to type single-letter commands to control the pan and tilt. The commands are as follows:

"p" activate pan servo
"t" activate tilt servo
"0" home the active servo
"1" step one left or up
"2" step one right or down
"3" step 5 left or up
"4" step 5 right or down
"5" step 10 left or up
"6" step 10 right or down
"7" step to limit left or up
"8" step to limit right or down

The program steps smoothly from the current position to the requested one. 254 of the 256 program bytes of the 08M are used.

The video from the webcam is served through an ip address on the NSLU2. My purpose ultimately is to make a remote "rivercam" of this, showing a spot I love to view during the time that I am not present. The video would be served over the internet, and the commands for pan and tilt would be passed from the viewing computer to the NSLU2 to the picaxe.

' 08Srv_1D test servo
#picaxe 08m

symbol panSpot = b13
symbol tiltSpot = b12
symbol activeServo = b11
symbol activeSpot = b10
symbol lastSpot = b9
symbol panServo = 1
symbol tiltServo = 2
symbol servoMin = 75
symbol servoMax = 225
symbol servoCenter = 150
symbol tiltMin = 155

pause 15000               ' wait 10 seconds
activeSpot = servoCenter 
panSpot = activeSpot       ' center
tiltSpot = activeSpot      ' center
lastSpot = activeSpot - 1
'high 0          'set up for serout
ActiveServo = tiltServo    ' tilt servo is active
gosub servoOut
ActiveServo = panServo     ' pan servo is active
gosub servoOut

    serin 4,N2400,b1
'    serout 0,N2400,(b1, " ",  #b1, " ")
    select b1
     case "p"
      activeServo = panServo
      activeSpot = panSpot
      lastSpot = panSpot   ' make sure we move with new servo
     case "t"
      activeServo = tiltServo
      activeSpot = tiltSpot
      lastSpot = tiltSpot   ' make sure we move with new servo
     case "0"
      activeSpot = servoCenter 
     case "1"
      inc activeSpot
     case "2"
      dec activeSpot
     case "3"
      activeSpot = activeSpot + 5
     case "4"
      activeSpot = activeSpot - 5
     case "5"
      activeSpot = activeSpot + 10
     case "6"
      activeSpot = activeSpot - 10
     case "7"
      activeSpot = servoMax
     case "8"
      activeSpot = servoMin

    if activeSpot > servoMax then
      activeSpot = servoMax 
    if activeSpot < servoMin then
      activeSpot = servoMin 
    if activeServo = tiltServo and activeSpot < tiltMin then
      activeSpot = tiltMin
    if activeSpot <> lastspot then
'      serout 0,N2400,(#activeServo, " ",  #activeSpot)
      gosub servoOut
      lastspot = activeSpot
      if activeServo = panServo then
        panSpot = activeSpot
        tiltSpot = activeSpot
'      pause 50

servoOut:  ' smoothly move from lastSpot
  b2 = activeSpot - lastSpot
  b3 = 1
  if activeSpot < lastSpot then
    b2 = lastSpot - activeSpot
    b3 = $FF   ' minus one
'  serout 0,N2400,(#b2, " ", #b3, " ", #lastSpot, " ", #activeSpot,10,13)
  activeSpot = lastSpot
  for b4 = 1 to b2       ' move smoothly to new activeSpot
    activeSpot = activeSpot + b3
'  serout 0,N2400,(#b4, " ", #b3, " ", #activeSpot,10,13)
      pulsout activeServo,activeSpot
      pause 20
      pulsout activeServo,activeSpot  ' twice seems to make it work
      pause 20
  next b4



Senior Member
web cam wantabe

I have the Pan/Tilt servos and have set them up on a 08/M as described in the post. I am using an 18X to send commands to the 08 but am not getting consistent results. Can someone give me a little sample code that I can use to test this setup?
I am not sure how to send the commands to the 08 from the editor?



Senior Member
The 08M expects its input on pin4 (leg 3). If you have it wired as in the diagram, you would disconnect the programming cable and plug it into H4.

Just make sure that you have the 22K and 10K resistors in place as in the download circuit. I set it up to use Hyperterminal in Windows--you could also download and use putty on Windows or Linux, and use the same download cable. Make sure you note which COM port the Program Editor is using, and set up Hypertermainal the same way, with 2400 bps, 8 bits, no parity, 1 stop bit, no flow control.

Then you should be able to type--for instance, "p8" to pan all the way to the right, and "t8" to tilt all the way down.

Once it is responding you might need to adjust servoMax and servoMin.


Senior Member
I have moved to a 40x1 and am able to use the servo/poservo commands with no problems. I am still a bit confused when you talk about
"The picaxe program allows the user to type single-letter commands to control the pan and tilt. The commands are as follows:"
Are you using 'terminal' from the editor program to control the PIC?


Senior Member
This was from a year ago next Tuesday. I'm afraid I don't remember what terminal program I was using--perhaps hyperterminal, perhaps Hippy's AITerm, perhaps putty, and perhaps the program editor terminal window. All should work, as would others if you get the serial communications settings correct.



Im just checking so this program allows me to type letters and it will move by a certain amount?

this is genious and exactly what i was looking for!

And it has taught me alot about using this language


Senior Member

Im just checking so this program allows me to type letters and it will move by a certain amount?
Correct. Check the full code and instructions above.

Therefore if P0 is sent then the Pan servo will be "homed", etc. Send either P or T, then the value of change you require.

Hope this helps.



Senior Member
Alex3k--yes, that's the theory, as mpep describes. In my implementation, there is a glitch somewhere which I haven't revisited to track down. It sometimes goes to the down and center position and won't home--it has to be nudged up in steps. I suspect the code, so if you use it, then test it thoroughly.

Glad you found it useful.


Senior Member
For anyone who might be interested, this interactive, over-the-net, pan and tilt webcam is working again (for now). The power supply for the NSLU2 had failed. When I plugged in a new one, it was nice to hear the whir of servos centering the webcam. There are still some glitches in the pan and tilt, but it mostly works.