User Manual

Using an analog input to control servos
An important feature of the Maestro is that it can be used to read inputs from sensors, switches, or
other devices. As a simple example, suppose we want to use a potentiometer to control the position of
a servo. For this example, connect the potentiometer to form a voltage divider between 5V and 0, with
the center tap connected to channel 1. Configure channel 1 to be an input, and examine the signal
on the Status tab of the Maestro Control Center. You should see the position indicator vary from 0 to
255 μs as you turn the potentiometer from one side to the other. In your script, this range corresponds
to numbers from 0 to 1023. We can scale this number up to approximately the full range of a servo,
then set the servo position to this number, all in a loop:
Alternatively, you might want the servo to go to discrete positions depending on the input value:
The example above works, but when the potentiometer is close to 300 or 600, noise on the analog-to-
digital conversion can cause the servo to jump randomly back and forth. A better way to do it is with
hysteresis:
1
2
3
4
5
6
# Sets servo 0 to a position based on an analog input.
begin
1 get_position # get the value of the pot, 0-1023
4 times 4000 plus # scale it to 4000-8092, approximately 1-2 ms
0 servo # set servo 0 based to the value
repeat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Set the servo to 4000, 6000, or 8000 depending on an analog input.
begin
1 get_position # get the value of the pot, 0-1023
dup 300 less_than
if
4000 # go to 4000 for values 0-299
else
dup 600 less_than
if
6000 # go to 6000 for values 300-599
else
8000 # go to 8000 for values 600-1023
endif
endif
0 servo
drop # remove the original copy of the pot value
repeat
?
?
Pololu Maestro Servo Controller User’s Guide © 2001–2019 Pololu Corporation
6. The Maestro Scripting Language Page 82 of 102