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*/