What is commonly done to stop a servo after reaching desired position?

You do not "stop" a servo.

A servo is always running. In a general sense, the servo is a control loop that takes as input a position target and applies force to hold at the requested target. If you want the target to be maintained, then the servo must be running.

A servo motor contains electronics that are internally turning a DC motor on and off as necessary to hold the target position. If the target does not match the current position, it turns on the motor to turn until the two match. Once the output shaft reaches what you requested, the electronics inside the servo motor turn the motor "off". Off meaning the DC motor is drawing little or no current. The servo control loop is always running comparing the shaft position to a target.

If you powered off the entire servo motor, the position is not under control and could move. For many applications if you unplugged the wires, the gears and motors apply enough friction that it would not turn. But in general, you do not count on friction to make things work.

Do I use...Servo.Write(desired position) to keep it from turning?

Yes. To tell a servo to stay at one position, you can just keep sending it the same target.

But you do not have to Write() again and again. If you want, you can keep track of what the last setting was and apply only when you have a change. For example, you can code like this:

if(position_target != position_last_set) {
  Servo.Write(position_target);
  position_last_set = position_target;
}  

Regardless of your code, under the hood, Servo is constantly transmitting the position target to the servo motor.


A normal servo doesn't have a "go" function, it only has positional control. That is, whether you run servo.write(90) forever or once, the servo will go to 90 and continue to actively keep that position until you write another position. The only way to turn a servo off is:

servo.detach();

For a normal DC motor, you will need to turn it on then off and then have kind of state tracking in the loop to never turn it on again, using a variable or millis() etc.

Of course, if you want something to only run once, put it in setup() (maybe within a loop in setup for simple tasks).

Also, you can simulate ending the program by entering an infinite loop with while(true){} or for(;;){}.


myservo.detach(); is what you are looking for. Moving it to a location than detaching. This is good when you want to use it to control a servo and not have it continue to take power like when using a battery. Here is a example.

#include <Servo.h>
Servo myservo;  // create servo object to control a servo

void setup() {
  myservo.attach(9);
  // attaches the servo on pin 9 to the servo object
}

void loop() {
  myservo.attach(9);
  // attaches the servo on pin 9 to the servo object
  delay(15);
  myservo.write(1);
  // sets the servo position according to the scaled value
  delay(1000);
  // waits for it to get to the position
  myservo.detach();
  delay(1000);
  myservo.attach(9);
  // attaches the servo on pin 9 to the servo object
  delay(15);
  myservo.write(179);
  // sets the servo position according to the scaled value
  delay(1000); // waits for it to get to the position
  myservo.detach();
  delay(1000);

Tags:

Servo