# First Servomotor Control Program

Contents

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
pw = 100
sweep: pulsout 0,pw
pause 18
pw = pw + 1
if pw > 200 then back
goto sweep
back: pulsout 0,pw
pause 18
pw = pw β 1
if pw < 100 then sweep
goto back

β create a variable pw
β start at extreme left
β send pulse to motor
β set frequency to about 50 Hz
β increase pw by 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

## End of Listing 1

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.

For more detail: First Servomotor Control Program