Hi all, I'm currently encountering an issue with my Arduino Uno in which my servos rotate continuously despite being coded otherwise. I do not have a continuous servo. My setup is described as follows:
Yellow ->Control Pin 9 (PWM)
Black -> POWER GND
Red -> POWER 5V
Servos are receiving power from the Arduino board, which is connected to PC via USB.
My code is the simple code provided by Arduino:
#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(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
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(15); // 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(15); // waits 15ms for the servo to reach the position
}
}
Upon execution, this code rotates the servo motor continuously in a clockwise fashion without stop. Interestingly, if I reduce the code to simply the myservo.attach(pin), upon connecting the servo it immediately begins rotating.
My hypothesis is that the servo is not speaking the Arduino's current language (frequency), but am unsure. Any help would be much appreciated!!
#include <Servo.h>
// Declare the Servo pin
int servoPin = 9;
Servo Servo1;
void setup() {
// We need to attach the servo to the used pin number
Servo1.attach(servoPin);
}
void loop(){
// Make servo go to 180 degrees
Servo1.write(180);
delay(1000);
// Make servo go to 0 degrees
Servo1.write(180);
delay(1000);
}
Sorry my mistake but their is -180 for reaches to 0 degree
#include <Servo.h>
// Declare the Servo pin
int servoPin = 9;
Servo Servo1;
void setup() {
// We need to attach the servo to the used pin number
Servo1.attach(servoPin);
}
void loop(){
// Make servo go to 180 degrees
Servo1.write(180);
delay(1000);
// Make servo go to 0 degrees
Servo1.write(-180);
delay(1000);
}
I had thought this was the case earlier and tried running code that would work for a continuous servo (0 full reverse speed, 90 stationary, 180 full speed). Unfortunately this produced identical results.
I've tried to take this code line by line to identify potential issues. I do not have much work with arduino servo control, but I find it exceptionally odd that simply running this code, and then connecting the pins, causes the servo to continuously rotate:
#include <Servo.h>
// Declare the Servo pin
int servoPin = 9;
Servo Servo1;
void setup() {
// We need to attach the servo to the used pin number
Servo1.attach(servoPin);
}
void loop() {}
This is what makes me think there is something else going on..
I ended up connecting a LiPo battery to a 5V BEC and a 6-Ch receiver and have been able to control these servos perfectly fine using an RC aircraft controller. Upon powering with the controller, these servos move perfectly to their zero positions and I am able to control them to 90deg of operation (as standard for servos in aircraft applications, +/- 45deg)
I am under the impression as of the moment that there is something wrong with the Arduino, as it is clear that it was not communicating properly with the servos. I will have the opportunity to test with a new Arduino shortly and will update again. Any other ideas are welcome!!