SG90 Servo motor mkaing buzzing noise

I made a small circuit with two relays and a servo motor, giving the conditions as the motor rotating in one direction if 1st relay is ON and other direction of 2nd relay is ON. And also to rotate between them if both are ON. (periodically)

2-3 days back as the motor was making buzzing sound, I removed its power supply from Arduino and gave it an external supply of 4 AA batteries.

Earlier it worked fine, but today it's again making that buzzing noise with no movement. Then I tried the basic sweep one and it's doing that. But when am again uploading my code and running it again it's making that noise again.

4 AA batteries as power supply (6.2V, around 415mA)(measured at the pins given to the motor)
TowerPro SG90 is the motor.

Can someone explain me how to remove this buzzing noise and why exactly is it happening.

Is my current low?
My motor is small ?
Should I be giving more delay?

Code below

//   https://forum.arduino.cc/index.php?topic=637630
// 25 september 2019

//servo rotating left for relay1 ON and servo rotating right for relay2 ON.

#include <Servo.h>
Servo myservo;

// check the values in this next 5 lines....
byte myservoPin = 13;
byte myservoGoOneWaySwitch = 2;  //wire buttons from pin to ground
byte myservoGoOtherWaySwitch = 3;
byte minPos = 0; //I like to keep away from the end stops
byte midPos = 90;
byte maxPos = 150;
byte xPos = 45;
byte yPos = 125;
byte pos = 2;
void setup()
{
  Serial.begin(9600);
  Serial.println(".... servo controlled by two relays as normal inputs .... forum 637630");
  Serial.print("Created: ");
  Serial.print(__TIME__);
  Serial.print(", ");
  Serial.println(__DATE__);
  Serial.print("File: ");
  Serial.println(__FILE__);

  //by default the servo starts in the middle
  // uncomment one of these next lines if you want to start at either side
  //myservo.write(minPos);
  //myservo.write(maxPos);
  myservo.attach(myservoPin);

  pinMode(myservoGoOneWaySwitch, INPUT_PULLUP); //wire switches from pin to ground
  pinMode(myservoGoOtherWaySwitch, INPUT_PULLUP);

  Serial.println("Waiting for a relay"); Serial.println(" ");
} //setup

void loop()
{
  //input_pullup are low when relay closed
  /*if (digitalRead(myservoGoOneWaySwitch) == HIGH )//both closed=illegal
    if (digitalRead(myservoGoOtherWaySwitch) == HIGH)
  {
    Serial.println("Going both ways");
    myservo.write(maxPos);
    delay(2000);
    myservo.write(midPos);
    delay(2000);
    myservo.write(minPos);
  }
  else if (digitalRead(myservoGoOtherWaySwitch) == HIGH )//  //both closed=illegal
    if (digitalRead(myservoGoOneWaySwitch) == HIGH)
  {
    Serial.println("Going both ways");
    myservo.write(minPos);
    delay(2000);
    myservo.write(midPos);
    delay(2000);
    myservo.write(maxPos);
  }*/  
  
  if ((digitalRead(myservoGoOtherWaySwitch) == HIGH) && (digitalRead(myservoGoOneWaySwitch) == LOW))
  {
    myservo.write(minPos);
    // this approach just zooms the servo at full speed, see https://www.arduino.cc/en/Tutorial/Sweep if that's a problem
    Serial.println("Going one way");
  }

  else if ((digitalRead(myservoGoOneWaySwitch) == HIGH) && (digitalRead(myservoGoOtherWaySwitch) == LOW))
  {
    myservo.write(maxPos);
    // this approach just zooms the servo at full speed, see https://www.arduino.cc/en/Tutorial/Sweep if that's a problem
    Serial.println("Going other way");
  }
  else if (digitalRead(myservoGoOneWaySwitch) == HIGH)
    if (digitalRead(myservoGoOtherWaySwitch) == HIGH)
  {
    myservo.write(maxPos);
    delay(2000);
    myservo.write(minPos);
    delay(2000);
    myservo.write(midPos);
    delay(2000);
    // this approach just zooms the servo at full speed, see https://www.arduino.cc/en/Tutorial/Sweep if that's a problem
    Serial.println("Going other way");
  }
  else if (digitalRead(myservoGoOtherWaySwitch) == HIGH)
    if (digitalRead(myservoGoOneWaySwitch) == HIGH)
  {
    myservo.write(minPos);
    delay(2000);
    myservo.write(maxPos);
    delay(2000);
    myservo.write(midPos);
    delay(2000);
  }
}
  /*else 
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(100);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(100);                       // waits 15ms for the servo to reach the position
  }/*
}
  
    /*if ((digitalRead(myservoGoOneWaySwitch) != HIGH) && (digitalRead(myservoGoOtherWaySwitch) != HIGH))
  {
    myservo.write(minPos);
    delay(2000);
    myservo.write(xPos);
    delay(2000);
    myservo.write(midPos);
    delay(2000);
    myservo.write(yPos);
    delay(2000);
    myservo.write(maxPos);
    delay(2000);
  }
} //loop*/

Did you connect all the grounds?

Post a hand drawn wiring diagram.

Please format your code properly (Autoformat in the IDE does a great job) as with those if/else clauses and some commented out it's very very hard to follow what's going on.

How are your buttons wired? Comments say "wire switches from pin to ground" and you do enable the internal pull-up for those pins, but your code is written for buttons that are active HIGH. That's a clear discrepancy.

Cheap servos aren't that precise, but they are good enough for model aircraft and they are made by the thousands. Meaning cheap.

What's happening is that if you send a servo to 90-degrees, for example, and there is a few ounces of force on the servo arm, then the processor will see the movement of the sensor and correct for it. Thus the buzz.

wvmarle:
Please format your code properly (Autoformat in the IDE does a great job) as with those if/else clauses and some commented out it's very very hard to follow what's going on.

How are your buttons wired? Comments say "wire switches from pin to ground" and you do enable the internal pull-up for those pins, but your code is written for buttons that are active HIGH. That's a clear discrepancy.

Ignore the comments as the whole code is modified many times. and what do u mean by "your code is written for buttons that are active HIGH" ?

1 Like

abhinav-7:
Ignore the comments as the whole code is modified many times.

Then update those comments as well. Or remove them. Misleading comments are worse than no comments - we rely on them to know what's going on, and so will you after putting aside the code for a month or two.

and what do u mean by "your code is written for buttons that are active HIGH" ?

Your code appears to consider a HIGH signal from a button to be a pressed button. If so, your buttons must be wired between Vcc and pin with a pull-down resistor. That contradicts your setup() function (both the code and the comments there).

So which is it?