I connected the black wire to ground, orange to +5V, and white to digital pin 9 (PWM). The servo turns fine but constantly vibrates and is noisy. Is there a way I can fix this? If not, what type of servo should I buy that is quite, and only makes a noise when it turns. Since this is in my room, I don't want to sleep with a constant buzzing in my ear.
It could be the servo, but it could also be your program not sending out the correct signal often enough or not always sending the same pulse length when the servo should be static. If you use the servo library, that shouldn't be a problem, if you do the PWM on your own, it might be.
Yes, I'm using the arduino servo library. This is the first time I'm using a servo. My setup includes two lasers and two photoresisters placed a few cm apart. Depending on which order they're triggered, they turn the servo. The problem is that when the servo is still, it vibrates and makes noises. If you could tell me how I can improve my code, please do.
#include <Servo.h>
Servo servo;
int visitors = 0; //counts visitors in the room
int inside = 0; //Analog IN pin for inner laser
int outside = 1; //Analog IN pin for outer laser
int inner = 0; //Inner photoresistor reading
int outer = 0; //Outer photoresistor reading
void setup()
{
Serial.begin(9600);
servo.attach(9);
}
void loop()
{
inner = analogRead(inside);
outer = analogRead(outside);
//If inner laser is tripped,
if (inner < 800)
{
//keep checking for half a second
for (int i = 0; i < 500; i++)
{
//then if outer laser is tripped,
if (outer < 500) //(this value is 500 because this photoresistor is normally lower for some reason compared to the other one
{
//a person left the room
visitors--;
break;
}
}
}
//does the opposite of previous if statement
if (outer < 800)
{
for (int i = 0; i< 500; i++)
{
if(inner < 500)
{
visitors++;
break;
}
}
}
if (visitors > 0)
{
servo.write(180);
}
else
{
servo.write(0);
}
delay(500);
}
If you're not moving the servo for long periods of time, you could call "detach".
This will stop servo control pulses, so if it still vibrates, it isn't a problem with the library.
I connected the black wire to ground, orange to +5V
I hope that isn't to the +5v on the arduino board. It gets posted probably once a day in the forum that the arduino power supply is generally not adequate to power a servo.
Just a thought.. don't know if it is pertinent or not.. just a noobie with Arduino (using a 328). Be sure and look at the warning on using delay() (it prevents other processing while waiting) as opposed to using millis() and doing a do loop to wait for the elapsed time desired. -- good luck