Turn off a servo in code??

I have a project that has a servo attached that consumes a lot of power. I would like to turn off the PWM pin that it connects to once the loop finishes. Just setting the 'POS' to 0 doesn't seem to completely turn off the pin. Is there any way in the loop to disable the pin until the loop starts back over?

"detach" stops the pulses.

AWOL beat me to it, but still, here's the link to the example....

Sweet! Thanks

Still having trouble.. I used servo.detach(); as seen in the code posted below. However, my servo is still getting pulses to it. If I press on the servo it resists me. Is this right? I was hoping that by using detach that it would stop that.

#include <Servo.h> 
 
int servoPin = 9;
int StpBtn = 3;
int UsrBtn = 4;
 
Servo servo;  
 
int angle = 0;   // servo position in degrees 
 
void setup() 
{ 
  //servo.attach(servoPin); 
  pinMode(UsrBtn, INPUT);
} 
 
 
void loop() 
{ 
  servo.attach(servoPin);
  int letMeBe = digitalRead(UsrBtn);
  
  
  if (letMeBe == 1)
  {
  // scan from 0 to 180 degrees
  for(angle = 0; angle < 180; angle++)  
  {                                  
    servo.write(angle);               
    delay(3);                   
  } 
  // now scan back from 180 to 0 degrees
  for(angle = 180; angle > 0; angle--)    
  {                                
    servo.write(angle);           
    delay(3); 
  }  
  }  
 else if (letMeBe == 0)
 {
   delay(250);
   servo.detach(); 
   
  }
}

I've never had much faith in the detach servo command doing anything useful or not to the servo. A typical R/C servo is simply not characterized to handle the situation where the servo has power but no control pulses are being sent to it. That is an undefined servo state that the servo manufactures don't design for. So how a random servo with a random mechanical load attached will respond in such a situation is not something I would design a project around.

Now there are more expensive programmable 'digital servos' available that can be programmed to respond in a specific manner (move to 'fail-safe position or 'freeze' on last position, etc) on loss of control pulse, but that is not something that standard analog servos can do.

Lefty

Below is some servo detach test code from some time back. A value less than 500 should detach the servo.

// zoomkat 10-14-11 serial servo test
// type servo position 500 to 2500 in serial monitor
// type in number <500 to detach servo
// for IDE 0022 and later
// Powering a servo from the arduino usually DOES NOT WORK.

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

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(2000); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control 
  Serial.println("servo-test-22"); // so I can keep track of what is loaded
}

void loop() {
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  // allow buffer to fill with next character
    }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string 
    int n = readString.toInt();
    
    if (n < 500) {
      myservo.detach(); 
    }
    else {
       myservo.attach(7);
       myservo.writeMicroseconds(n); //convert readString to number for servo
    }
    readString=""; //empty for next input
  } 
}

I used what is found here-------->

modsbyus:
I used what is found here-------->
How to properly detach/turn off a servo with arduino. | Modsbyus.com

I'm glad it worked for you.

Based on that link, which transistor could be used? I tried a 2N2222 and started to get a funny smell so I pulled the plug. It has similar specs to the quoted. The author said he was going to choose a new transistor and update but it seems the one on the site is the old one. It uses the detach function anyway even though the webpage says the detach function doens't work.

Oh well, I might get try a brute force switch on the servo and hope for the best.

Maybe use the pull-up resistor in the software?

I'm puzzled....

what do you mean by a servo? - something like Futaba make for RC models? or a dc motor driven by a shield?

is there a feedback mechanism? if so, what?

regards

Allan

I've never heard of a "dc motor driven by a shield" being referred to as a servo before but there's a first for everything. It is a small 9g analog servo, yes like a Futaba for RCs.

Puzzled? I was referring to the link that modsbyus used. I emailed the author to ask whether the transistor listed was the old one or the updated one (the site said it would be updated with which transistor to use).

Not sure what you mean by "is there a feedback mechanism? if so, what?".

My current project is a PIR sensor waking up the Arduino via interrupt to move a servo and turn on LEDs. When the arduino is sleeping the quiescent current due to the stationary servo is around 6 mA, which I hope I can get down substantially. Not sure if 6mA is spec.

I didn't try the code posted by Zoomcat because I didn't understand it.

Old thread, but just for the record...

I was trying to accomplish the same thing and this simple code seems to work for me. I'm using the little SM-S2309S analog servo that comes standard in most arduino starter kits.

// Include the Servo library 
#include <Servo.h> 
// Declare the Servo pin 
int servoPin = 3; 
// Create a servo object 
Servo Servo1; 
void setup() { 
   // We need to attach the servo to the used pin number 
   Servo1.attach(servoPin); 
}
void loop(){ 
   // Make servo go to 0 degrees 
   Servo1.attach(servoPin); 
   Servo1.write(0);  
   delay(500); //Not sure why I had to add this short delay here, but without it, the servo doesn't move at all
   Servo1.detach();
   delay(3000);
   // Make servo go to 180 degrees 
   Servo1.attach(servoPin); 
   Servo1.write(180); 
   delay(500);  //Not sure why I had to add this short delay here, but without it, the servo doesn't move at all
   Servo1.detach();
   delay(3000);
}

I haven't tested whether the servo is still drawing power after the Servo1.detach() during the three second pause, but it appears to be totally silent, and I can not feel any vibration at all during that 3 second period. Good enough for me!

Zimbu:
Old thread, but just for the record...

I was trying to accomplish the same thing and this simple code seems to work for me. I'm using the little SM-S2309S analog servo that comes standard in most arduino starter kits.

// Include the Servo library 

#include <Servo.h>
// Declare the Servo pin
int servoPin = 3;
// Create a servo object
Servo Servo1;
void setup() {
  // We need to attach the servo to the used pin number
  Servo1.attach(servoPin);
}
void loop(){
  // Make servo go to 0 degrees
  Servo1.attach(servoPin);
  Servo1.write(0);  
  delay(500); //Not sure why I had to add this short delay here, but without it, the servo doesn't move at all
  Servo1.detach();
  delay(3000);
  // Make servo go to 180 degrees
  Servo1.attach(servoPin);
  Servo1.write(180);
  delay(500);  //Not sure why I had to add this short delay here, but without it, the servo doesn't move at all
  Servo1.detach();
  delay(3000);
}




I haven't tested whether the servo is still drawing power after the Servo1.detach() during the three second pause, but it appears to be totally silent, and I can not feel any vibration at all during that 3 second period. Good enough for me!

This is a method that I ended up using. The reason you need to the 500ms delay is because otherwise the PWM is turned off before the servo can adjust it's position.