Hey there!
I'm new to arduino and using the servos but in troubleshooting I brought my code very simply
My code: #include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(6); // attaches the servo on pin 9 to the servo object
}
void loop() {
myservo.writeMicroseconds(1500); // tell servo to go to position in variable 'pos'
}
When I use this my servo (non continuous) will go to the selected position and then return to position 0 and go back and forth. Using myservo.write(XX) does the same thing. Its reading the position because when I input writeMicroseconds(1000) vs (1500) it will only go to a higher position then back to 0. I bought 3 servos in a robotic arm kit and all three function this way. They are "towen pro mg 99gr" if that matters at all.
I was going to suggest a power issue, where overcurrent if you power the servo from the Arduino may reset the board and re-attach() the servo. But since 1500 is the defaut position for an attach, that wouldn't explain why it's moving.
But that said, how are you powering the servo?
To see if the board is resetting, put these lines in setup()then open the monitor and see if the sketch is restarting. You should only see "Hello" once.
void setup() {
Serial.begin(9600);
Serial.println("Hello");
myservo.attach(6); // attaches the servo on pin 9 to the servo object
}
There's no such thing as a "towen pro mg99gr". But if you mean the Towerpro MG996R then that's a powerful servo. The stall current is around 1.5A so it should never be powered through the Arduino always directly from a battery/power supply.
95% of all servo problems people have is because they don't understand the power requirements of
servos and motors, which are large. Your servo is crow-barring the supply and reseting itself and/or the Arduino.
Always use separate supply for motors/servos to the Arduino 5V
Always allow plenty of current for a servo - at least 1A per small servo, more for larger or
high-torque servos.
Okay, so I hooked it up to an external 5V battery supply and it solved the constant rotating problem....
but now when I write it to a location it simply doesn't move
Ok this may seem like a really silly question, but are you sure the power supply is ok? Looks like a 9V battery to me, so I'm assuming it's partly flat. Maybe it can't provide the current?
A small 9V battery is way way way underpowered for a servo. Not remotely powerful enough. We've
explained the power requirements of servos already. Breadboards are not designed for high currents
either.