Servo detach

Hi all, I’m trying to detach my servos after every move to stop the buzzing/jitter. I tried using the .detach command after write(angle) and then .attach after digitalRead(RIGHT)/(LEFT) == LOW but it doesn’t want to work. I am new to coding and looking for some help. please see the code below.

Kind Regards,
Mike.

#include <Servo.h>

Servo servo1;  // create servo object to control a servo
Servo servo2;  // create servo object to control a servo

int angle =180;    // initial angle  for servo
int angleStep =180;

#define LEFT 12   // pin 12 is connected to left button
#define RIGHT  2  // pin 2 is connected to right button

void setup() {
  
  Serial.begin(9600);          //  setup serial
  servo1.attach (9);  // attaches the servo on pin 9 to the servo object
  servo2.attach (10);  // attaches the servo on pin 10 to the servo object
  pinMode(LEFT,INPUT_PULLUP); // assign pin 12 ass input for Left button
  pinMode(RIGHT,INPUT_PULLUP);// assing pin 2 as input for right button
  servo1.write(angle);// send servo to the middle at 180 degrees
  servo2.write(angle);// send servo to the middle at 180 degrees
 Serial.println("Exhaust Valves ");
}

void loop() {
  
  while(digitalRead(RIGHT) == LOW){

    if (angle > 0 && angle <= 180) {
      angle = angle - angleStep;
       if(angle < 0){
        angle = 0;
       }else{
      servo1.write(angle); // move the servo to desired angle
      servo2.write(angle); // move the servo to desired angle
      Serial.print("Moved to: ");
      Serial.print(angle);   // print the angle
      Serial.println(" degree");
       }
    }
    
  delay(100); // waits for the servo to get there
  }

  while(digitalRead(LEFT) == LOW){

    
    if (angle >= 0 && angle <= 180) {
      angle = angle + angleStep;
      if(angle >180){
        angle =180;
       }else{
      servo1.write(angle); // move the servo to desired angle
      servo2.write(angle); // move the servo to desired angle
      Serial.print("Moved to: ");
      Serial.print(angle);   // print the angle
      Serial.println(" degree");
       }
    }
    
  delay(100); // waits for the servo to get there
  }// 

  
}

I do not see any detach in your code.

but it doesn't want to work

This gives us nothing to go on.

A better way to describe difficulty with a sketch is to state what you expected to happen, what actually happened and how those two differ.

Some servos buzz more than others.
Almost all servos buzz a fair amount when they are under a load.
What servos are you using?
Do you have them doing more work that sweeping the arm back and forth?

Jittering is also very often a power problem. So as well as what servos you have it would useful to know how they are connected and powered.

You code is strangely complicated for something that only ever writes either 0 or 180 to the servos. Is that really what it is supposed to do?

Steve

Hi, the code I posted is just the standard code to get the servos to move 180 when a button is pushed and move back to 0 when the other button is pushed. the servos are MG996R.

What I want to happen is, At power on the servos go to their set angle 0 (home position) as stated in the code. When the servos have finished doing their moves I want to detach the servos to stop the buzz. Then, when a button is pressed to move the servos to their next position I want to reattach the servos to allow them to move and then detach when they have finished, and so on.

Mike.

s200bym:
I want to detach the servos to stop the buzz

Have you considered WHY it is buzzing?

One of the reasons is that it is overloaded. Maybe you are asking it to move a little bit too far?

…R

I have tried different angles but it still buzzes.

You have the code. What is stopping you from just putting your detach/attach commands in and testing to see if they do what you think? Post THAT code.

But buzzing/jittering is normally a power, or as Robin2 says, a mechanical problem. Detach hardly ever fixes it.

Steve

New code with detach and attach added in. It does not work. Am i putting the commands in the wrong place?

Mike.

#include <Servo.h>

Servo servo1;  // create servo object to control a servo
Servo servo2;  // create servo object to control a servo

int angle = 180;   // initial angle  for servo
int angleStep = 180;

#define LEFT 12   // pin 12 is connected to left button
#define RIGHT 2  // pin 2 is connected to right button

void setup() {

  Serial.begin(9600);          //  setup serial
  servo1.attach (9);  // attaches the servo on pin 9 to the servo object
  servo2.attach (10);  // attaches the servo on pin 10 to the servo object
  pinMode(LEFT, INPUT_PULLUP); // assign pin 12 ass input for Left button
  pinMode(RIGHT, INPUT_PULLUP); // assing pin 2 as input for right button
  servo1.write(angle);// send servo to 180 degrees
  servo2.write(angle);// send servo to 180 degrees
  Serial.println("Exhaust Valves ");
}

void loop() {


  while (digitalRead(LEFT) == LOW) {
    servo1.attach(9);
    servo2.attach(10);

    if (angle > 0 && angle <= 180) {
      angle = angle - angleStep;
      if (angle < 0) {
        angle = 0;

      } else {
        servo1.write(angle); // move the servo to desired angle
        servo2.write(angle); // move the servo to desired angle
        Serial.print("Valve: ");
        Serial.print(" Open");   // print the angle
        Serial.println(" (Loud Mode)");
        servo1.detach();
        servo2.detach();
      }
    }

    delay(100); // waits for the servo to get there

  }

  while (digitalRead(RIGHT) == LOW) {
    servo1.attach(9);
    servo2.attach(10);


    if (angle >= 0 && angle <= 180) {
      angle = angle + angleStep;
      if (angle > 180) {
        angle = 180;
      } else {
        servo1.write(angle); // move the servo to desired angle
        servo2.write(angle); // move the servo to desired angle
        Serial.print("Valve: ");
        Serial.print(" Closed");   // print the angle
        Serial.println(" (Quiet Mode)");
        servo1.detach();
        servo2.detach();
      }
    }

    delay(100); // waits for the servo to get there

  }


}

Fritzing Layout

If your servo power supply cannot provide 2 Amperes, replace it with one that can. 4XAA batteries will work for a while.

Current capability of a breadboard supply is very low.
It might be able to power one tiny hobby servo (if input voltage is low enough), but not two.
Leo..

MG996R are power-hungry servos. Connecting them via a breadboard is unlikely to work well. Try it with 4 x AA NiMH rechargeable cells and more solid power connections.

The code looks o.k. So when you say "It does not work" do you mean that it does nothing at all or do you mean that the servos work normally but the change did not cure the buzzing?

Personally I'd have put the attach commands immediately before the writes. There's not much point reattaching the servos if you're not going to write to them.

Steve

When you attach a servo, it goes to 90° by default unless you specify otherwise, so before attaching, specify position first.

servo1.write(180);
servo1.attach (9);
servo1.detach();

servo1.write(0);
servo1.attach(9);
servo1.detach();

Are the servos subjected to a constant force, like an air damper, causing them to "fight back"? That could cause them to buzz, you may need servos with more muscle.

Thanks for all your input guys. I have had a play with the code and I have kind of got it to work. The servos move and detach at the end of each move and reattach when the button is pressed as it should. Now, the problem I am having is that when the Arduino powers on the servos won’t go back to 180 degrees (home position) which it should do. Do I need to add any delays or other commands for when the Arduino powers on?

New code below.

Mike.

#include <Servo.h>

Servo servo1;  // create servo object to control a servo
Servo servo2;  // create servo object to control a servo

int angle = 180;   // initial angle  for servo
int angleStep = 180;

#define LEFT 12   // pin 12 is connected to left button
#define RIGHT 2  // pin 2 is connected to right button

void setup() {

  Serial.begin(9600);          //  setup serial
  servo1.attach (9);  // attaches the servo on pin 9 to the servo object
  servo2.attach (10);  // attaches the servo on pin 10 to the servo object
  pinMode(LEFT, INPUT_PULLUP); // assign pin 12 ass input for Left button
  pinMode(RIGHT, INPUT_PULLUP); // assing pin 2 as input for right button
  servo1.write(angle);// send servo to 180 degrees
  servo2.write(angle);// send servo to 180 degrees
  Serial.println("Exhaust Valves ");
}

void loop() {



  while (digitalRead(LEFT) == LOW) {
    servo1.attach(9);
    servo2.attach(10);


    if (angle > 0 && angle <= 180) {
      angle = angle - angleStep;
      if (angle < 0) {
        angle = 0;

      } else {

        servo1.write(angle); // move the servo to desired angle
        servo2.write(angle); // move the servo to desired angle
        Serial.print("Valve: ");
        Serial.print(" Open");   // print the angle
        Serial.println(" (Loud Mode)");


      }
    }

    delay(1000); // waits for the servo to get there

  }
 { servo1.detach();
  servo2.detach();
  
 }
  while (digitalRead(RIGHT) == LOW) {
    servo1.attach(9);
    servo2.attach(10);


    if (angle >= 0 && angle <= 180) {
      angle = angle + angleStep;
      if (angle > 180) {
        angle = 180;
      } else {

        servo1.write(angle); // move the servo to desired angle
        servo2.write(angle); // move the servo to desired angle
        Serial.print("Valve: ");
        Serial.print(" Closed");   // print the angle
        Serial.println(" (Quiet Mode)");

      }
    }

    delay(1000); // waits for the servo to get there

  }
  {servo1.detach();
  servo2.detach();
  }

}

The way I read your code, the initial angle is set to be 180. Why do you think it should go to 0?

vinceherman:
The way I read your code, the initial angle is set to be 180. Why do you think it should go to 0?

Edited to 180.

Mike.

So, you want the servo to go to 180 on power up.
In the setup function you write 180 to both servos.
Then what happens next? You enter the loop function and either start writing other values to the servos or detach them. Either way, your servos likely moved a very small distance towards your commanded 180 initial position before you then told it to go somewhere else, or detached it.

Quick update.

I did what every man does and took the servos apart!!!

It turns out that the gear that turns the pot is snagging in a guide within the housing and not allowing the gear to sit properly which in turn is giving a false position for the pot.

a quick scrape and clean has eradicated the buzzing.

problem solved.

I can now revert back to the original code and not have to worry about detaching the servos.

Thank you all for your input.

Kind Regards,

Mike.