Can't get Servo Motor to work

Hi there,

I was working testing out some new servo motors (MG995 servo motor) so I wrote up a simple code to have them go back and forth when I push a button (shown below). I am using my arduino nano to run the code also. When I plug the nano into my laptop, the servo snaps to the 90 degree position, but when I push the button to activate the servo, it moves one way and does not move back. It will not try to move the other direction even after multiple button pushes too. I have two of these servos and they both exhibit the same behavior so I doubt there is something wrong with the servo motor. Confused on what could be causing the problem. Any help would be much appreciated!

//setting up the servo communications
#include <Servo.h>
Servo servo;

const byte servoPin = 5;
const byte resetPin = 6;

void setup() {
  // put your setup code here, to run once:

  pinMode(resetPin, INPUT);
  servo.attach(servoPin);

}

void loop() {
  // put your main code here, to run repeatedly:

  if (digitalRead(resetPin) == HIGH) {

    servo.write(0); //servo motor activates
    delay(1000); //delay to let motor operate
    servo.write(180); //servo motor moves back
    delay(1000); //delay to let motor operate
    
    
  }

}

How is your resetPin switch wired ?


FYI

Right now I have set up as a pull down configuration on my resetPin. It should be reading for a HIGH voltage when the button gets pushed.

Sounds to me like the servo is overloading the power supply and causing the Arduino to reset. You need a more capable power supply for the servos. One common rule of thumb is 1 Amp (5V) per servo.

Hi,
Welcome to the forum.

Try longer delay times, 1000ms or 1s may be to fast.

Tom..... :smiley: :+1: :coffee: :australia:

Specs on that servo seem pretty wide.
"Neutral position- 1500usec
Running degree|180°±3°(when 500~2500usec) "
Did you try using servo.writeMicroseconds() instead of servo.write()?

I mean, you said

but how do you know that the Arduino and the servo agree that that's 90? Sure, you can run the sketch and take it as truth that whatever pulse is put out by servo.write(90); is in fact 90 degrees and slap your servo horn on centering it there but that doesn't necessarily make it halfway in the servo's travel range. It does in your case, but some servos also seem to have a deadband to either side of neutral that doesn't respond precisely to the servo library angle commands.

Also, I assume you're using external power to those servos? Please say yes...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.