How Detach/turn off a servo?

In my project i just need that the servo move a few times during the day, so im trying to find a way to just attach the servo when requested. Now the servo is on all the time, and it moves when is requested. I need that when requested: turn ON, move and turn OFF. Someone know what i have to do?. I already tried some tips with the servo.detach( ) command but didn’t worked.

Tnks

PS: i’m beginner in programming :wink:

The code:

bool state = LOW;

Servo servo1;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 

int pos = 0;    // variable to store the servo position 

void setup() 
{ 
  servo1.attach(A0);  // attaches the servo on the A0 pin to the servo object 
  Spark.function("test", testFunction);
} 


void loop() 
{ 
    
  
}

int testFunction(String args) {
    state = !state; // toggle the state
    servo1.write(22);
    delay(1000);
    servo1.write(0);
    return 200; // This is checked in the web app controller for validation
}

Which servo are you using?

I’m no expert in servos but had to work with one recently…

Let check my code and see what I did!

Basically try using Servo.detach() after servo1.write(0);

According to the docs, it should literally cut off power to the Servo but i can’t confirm this behavior :smiley:

Try and see how it turns out? Or some Servo experts might be commenting here soon :wink:

I’m using a Micro Servo Tower Pro 9g /1.5kg.

I tried that (code below) and the apparently don’t have problem, the code it’s ok, but i don’t know if it’s really turning off the servo.

Im using the VIN as a 5V source for the servo (It’s VIN, A0, and GND). Does have a way to turn OFF the VIN? I think that until now, this will be the most guaranteed way to make sure that the servo it’s off.

Tnks.

Code:

int testFunction(String args) {
    servo1.attach(A0);  // attaches the servo on the A0 pin to the servo object 
    state = !state; // toggle the state
    servo1.write(22);
    delay(1000);
    servo1.write(0);
    return 200; // This is checked in the web app controller for validation
    servo1.detach(); // detach the servo on the A0 pin to the servo object
}

Well we can use some external circuitry to cut off power to the Servo but it’s definitely not needed.

The software will be able to handle it.

I’m thinking the part below might be a problem but just try the code below 1st :smile:

        servo1.write(22);
        delay(1000);

Try:

int testFunction(String args) {
        servo1.attach(A0); // attaches the servo on the A0 pin to the servo object 
        state = !state; // toggle the state
        servo1.write(22);
        delay(1000);
        servo1.detach(); // detach the servo on the A0 pin to the servo object
        return 200; // This is checked in the web app controller for validation
}

Correct me if I’m wrong… Doing the servo detach does not power down the servo, it just stops supplying the PWM control signal to the servo. What would the servo do in that case? Maybe it’s a standard that a well behaved servo just stops trying to move. Without movement the power consumption will be very low so all is well. Is this expected, normal, throughout the supply of servos?

I need to do a simular thing and was planning to power down the servo by switching it’s power. Would be great if this is not necessary. In my application, the servo would be powered ever minute or so to move to a new position only when needed. When the servo is powered down there should be enough holding force to keep things in place. (there will be nearly zero working load on the servo)

I think detach() would simply keep the servo free such that you can rotate it by hand.

But not too sure since I haven’t tested it but you can always hook it up and do a quick test!

Hi @uChobby

Different servos do different things when you remove the PWM control signal. Cheap servos like I normally use just don’t move when you control signal stops but some more expensive servos for model aircraft can be programmed to return to a specific position when the control signal fails. This allows your model aircraft to not crash immediately if something goes wrong.

When you disconnect power to the servo, it will sort-of hold position but there is nothing holding it other than the gear train and the motor mechanical resistance. You can increase this slightly if you short out the motor power terminals instead of just removing power, but even then you will be able to move the servo by hand.

1 Like

A post was merged into an existing topic: How to stop servo from pulsating?