Lesson: Exercise: Using a Regulated Motor as a Servo

Learn how to use a regulated motor to act as a servo.

Understand how to use a regulated motor to act as a servo.


A servo is a motor where we control its movements in finer detail than just turn on or off. Typically we want to move the motor in a range less than one revolution. This allows us to use the servo (motor) to control things like arms (raise/lower) and grippers (open/close). A Regulated Motor allows us to move the motor to a specific angle or by a specific angle. This allows us to have finer control over the movement of the motor and that allows us to control more complex mechanisms than things that just rotate.

In the ev3.exercises package, create a new class called ServoDemo and copy the code below into that class:

This program assumes you have built the basic robot described in the Lego kit that has an arm on the front controlled by a Medium motor. The program will raise or lower the arm in response to the up/down buttons on the EV3, 10 degrees at a time until the angle of the arm is zero (all the way down) or 160 degrees (all the way up).

This example shows the use of the do-while loop and the Lcd library class to display the current arm angle on the LCD.

Note that the motor does not allow us to read the current angle of the motor so we have to keep track of that ourselves. When the motor object is created, it assumes it is at the zero position of travel. You have to put the arm in the position you define as zero before starting the program. Here we assume the arm is touching the ground at start. You have to run the program and determine what number of degrees represents the highest travel of the arm and then put that in the code as the upper limit.