You are on page 1of 7

First Servomotor Control Program

Online Catalog

Shopping Cart

Previous Page

Next Page

Home > Articles > Robotics > Servomotor Controlling Page 2

First Servomotor Control Program

In our first program , we will simply sweep the servomotor from CCW to CW and then sweep back. The program will be kept simple as to demonstrate the priniciples of controlling a servo with a the PIC Basic language. The schematic can be seen in figure 2 (below).

The variable pw controls the pulsewidth, and is started at 100 (extreme left, -45 degrees). The program sends the pulse out to the servo, and then is increased by a value of 1 until it reaches 200 (extreme right, 45 degrees), at which point it will reverese the rotation. ----Listing 1---' First servomotor program ' Sweeps left to right, then reverses Symbol B1 = pw ' create a variable pw pw = 100 ' start at extreme left sweep: pulsout 0,pw ' send pulse to motor pause 18 ' set frequency to about 50 Hz pw = pw + 1 ' increase pw by 1

http://www.imagesco.com/articles/picservo/02.html (1 of 2)02-03-2006 2:49:44 pm

First Servomotor Control Program

if pw > 200 then back goto sweep back: pulsout 0,pw pause 18 pw = pw - 1 if pw < 100 then sweep goto back ----End of Listing 1----

' ' ' ' ' ' '

at extreme right, turn CCW otherwise, continue send pulse to motor set frequency to about 50 Hz decrease pw by 1 at extreme left, turn CW otherwise, continue

If desired, we could extend the rotation of the servomotor to a full 180 degrees (-90 to 90 degrees) rotation by decreasing the minimum pulsewidth to below 1 ms and increasing the maximum pulsewidth to over 2 ms. This can be accomplished with our previous program by modifying the occurances of 100 and 200 to your desired minimum and maximum pulsewidths, respectivly. However, a note of caution: the pulsewidth required for servos varies from brand to brand. One motor may require a 2.8 ms pulsewidth for maximum rotation, while another may only need 2.4 ms. Furthermore, servomotors have end stops that limit its rotation. If you send the motor a pulsewidth that is beyond the end stops, the motor will keep trying to turn. A motor in this stalled condition not only draws more current, but also puts wear on the internal gears, shortening the lifespan of the motor.

Next Page

Online Catalog

Shopping Cart

Previous Page

Next Page

http://www.imagesco.com/articles/picservo/02.html (2 of 2)02-03-2006 2:49:44 pm

Manual Servomotor Control

Online Catalog

Shopping Cart

Previous Page

Next Page

Home > Articles > Robotics > Servomotor Controlling Page 3

Manual Servo Control

Our next program will allow you to control the direction of the servo manually via a SPDT switch (with a center-off position) connected to ports B1 and B2. Without a center-off position, you will have to use two switches. A schematic is shown in figure 3 (below).

With the switch in the center position, the servo will not turn. When it is moved into forward, it will turn one way. Moving the switch down will make it turn the opposite direction. This program as-is will limit rotation to 45 degrees off-center, but can be modified to extend rotation through the aforementioned methods. ----Listing 2---' Manual control of servo direction via ' an SPDT switch. Symbol B1=pw ' create a variable pw pw = 150 ' begin at center position check: if pin1 = 0 then left ' is pin 1 active? if pin2 = 0 then right ' is pin 2 active?

http://www.imagesco.com/articles/picservo/03.html (1 of 2)02-03-2006 2:50:01 pm

Manual Servomotor Control

Pulsout 0,pw pause 18 goto check left: pw = pw + 1 pulsout 0,pw pause 18 if pw > 200 then max goto check right: pw = pw - 1 pulsout 0,pw pause 18 if pw < 100 then min goto check max: pw = 200 goto check min: pw = 100 goto check ----End of Listing 2----

' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '

send current pw set frequency to about 50 Hz check again increase pulsewidth send current pw set frequency to about 50 Hz dont go over 2 ms go back and check again decrease pulsewidth send current pw set frequency to about 50 Hz dont go under 1 ms check again cap pw at 2 ms check again cap at 1 ms check again

Next Page

Online Catalog

Shopping Cart

Previous Page

Next Page

http://www.imagesco.com/articles/picservo/03.html (2 of 2)02-03-2006 2:50:01 pm

Multiple Servomotors

Online Catalog

Shopping Cart

Previous Page

Next Page

Home > Articles > Robotics > Servomotor Controlling Page 4

Multiple Servomotors

Using a modified version of the last program, we can control as many servomotors as we have I/O lines on port B. In the next listing, we will control two servos in the same manner as we controlled a single servo in the previous program. The circuit is shown in figure 4 (below).

The program uses two pulsewidth variables, pw1 and pw2; and two sets of routines, left1 and left2, right1 and right2; one for each motor. As you can see in the schematic, the first servo is wired as per the previous circuit. The second servo is now using B3 as it's pulse out, and B4 and B5 for the SPDT switch. ----Listing 3----

http://www.imagesco.com/articles/picservo/04.html (1 of 3)02-03-2006 2:50:11 pm

Multiple Servomotors

'Manual control of two servomotors using 2 SPDT switches 'Use B1 to hold pulsewidth variable for servo 1 'Use B2 to hold pulsewidth variable for servo 2 'Initialize Variables B1 = 150 B2 = 150 start: IF pin1 = 0 Then IF pin2 = 0 Then IF pin4 = 0 Then IF pin5 = 0 Then PulsOut 0, B1 PulsOut 3, B2 Pause 18 GoTo start 'start servo 1 at center position 'start servo 2 at center position 'check for switch closures 'is sw1 left active? 'is sw1 right active? 'is sw2 left active? 'is sw2 right active? 'send current servo 1 position out 'send current servo 2 position out

left1 right1 left2 right2

'Routines for Servomotor left1: B1 = B1 + 1 PulsOut 0, B1 PulsOut 3, B2 Pause 18 IF B1 > 225 Then max1 GoTo start right1: B1 = B1 - 1 PulsOut 0, B1 PulsOut 3, B2 Pause 18 IF B1 < 75 Then min1 GoTo start max1: B1 = 225 GoTo start min1: B1 = 75 GoTo start 'Routines for Servomotor left2: B2 = B2 + 1 PulsOut 0, B1 PulsOut 3, B2 Pause 18 IF B2 > 225 Then max2 GoTo start right2: B2 = B2 - 1 PulsOut 0, B1 PulsOut 3, B2 Pause 18

1 'increase the pulse width 'send current B1 'send current B2 'set frequency update about 50 hz 'maximum 2.25 millisecond

'decrease the pulse width 'send current B1 'send current B2 'set frequency update about 50 hz 'minimum .75 millisecond

'cap max B1 at 2.25 milliseconds

'cap min B1 at .75 millisecond

2 'increase the pulse width 'send current B1 'send current B2 'set frequency update about 50 hz 'maximum 2.25 millisecond

'decrease the pulse width 'send current B1 'send current B2 'set frequency update about 50 hz

http://www.imagesco.com/articles/picservo/04.html (2 of 3)02-03-2006 2:50:11 pm

Multiple Servomotors

IF B2 < 75 Then min2 GoTo start max2: B2 = 225 GoTo start min2: B2 = 75 GoTo start ----End of Listing 3----

'minimum .75 millisecond

'cap max B2 at 2.25 milliseconds

'cap min B2 at .75 millisecond

Catalog Page for PIC Microcontrollers (a kit containing all necessary parts is available) Catalog Page for Servo Motors

Back to Robotics Articles

Online Catalog

Shopping Cart

Previous Page

Next Page

http://www.imagesco.com/articles/picservo/04.html (3 of 3)02-03-2006 2:50:11 pm

You might also like