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
}//
}
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?
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.
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
}
}
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.
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();
}
}
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.
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.