I'm having issues with my arduino controlling a servo which I can't seem to figure out. I have an arduino uno and a servo (tried both a 180 and a continuous servo) hooked up to an external power supply. I have the signal lead from the arduino connected to pin 9 on the Arduino.
I uploaded the "sweep" example code that is included in the "servo" library which is suppose to make the servo arm sweep back and forth from 0 to 180 degrees. Mine however just goes in one direction: for the 180 servo, it rotates clockwise and stops when it reaches the limit. For the continuous servo, it just keeps rotating in one direction. I've never gotten it to sweep back and forth. I've checked many different tutorials and examples all showing the same thing but for some reason mine does not do that.
I initially suspected something was wrong with the servo so I tried a different one but to no avail. Then I thought maybe it's my board so so I got a hold of a different Arduino Uno but I'm still having the same issue.
Now I'm wondering if it has to do something with my OS (I'm running windows 8.1)?
Any help/suggestions would be greatly appreciated. Thanks.
I have the signal lead from the arduino connected to pin 9 on the Arduino.
I don't understand that. Do you have the arduino signal pin connected to the servo signal pin? You also need to have the arduino and servo power grounds connected together.
I have the servo connected properly and is not the issue. I have an external power supply powering the servo (red to 5v and black to ground). The signal wire from the servo is connected to digital pin 9 on the arduino which corresponds to the pin assigned in my sketh "myservo.attach(9)" Failing to properly connect the ground will result in an open circuit and will not let the servo move at all. This is not the issue I'm experiencing. What I'm experiencing is that the servo will move continuously in one direction without being able to control delays or direction/angles.
Put some diagnostic Serial.print() lines into the code then. Have it print "going up" or "going down" along with the value of pos in the two fors and let's check that the code's actually going there.
edit.... When I did that I got this, as expected. Let's see what you get?
going one way 174
going one way 175
going one way 176
going one way 177
going one way 178
going one way 179
going other way 180
going other way 179
going other way 178
going other way 177
going other way 176
going other way 175
hatts:
What I'm experiencing is that the servo will move continuously in one direction without being able to control delays or direction/angles.
That suggests you are using a continuous rotation servo.
Have you got another servo that you can try?
Can you write a short program that just moves the servo to a specific position - for example (not tested). Experiment with different values in place of 90.
Below is some simple serov command code you might try to see if you can get your servo to position.
//zoomkat 7-30-10 serial servo test
//type servo position 0 to 180 in serial monitor
// Powering a servo from the arduino usually *DOES NOT WORK*.
String readString;
#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup() {
Serial.begin(9600);
myservo.attach(9);
Serial.println("servo-test"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the String readString
delay(2); //slow looping to allow buffer to fill with next character
}
if (readString.length() >0) {
Serial.println(readString); //so you can see the captured String
int n = readString.toInt(); //convert readString into a number
Serial.println(n); //so you can see the integer
myservo.write(n);
readString="";
}
}
Thank you everyone for the suggestions. Unfortunately none of them are working. I've tested this on both a continuous and a non-continuous servo and the issue is the same - even with code to position to a certain position, I can not get it to position at all. For a continuous servo it keeps rotating. For the non-continuous servo, it'll rotate until it reaches its maximum angle... I'm completely lost here...
I set mine up just exactly the way this person did with the wiring and the code and what my servo does is just spin continuously.
Do you mean rotate continuously in one direction? If the servo horn can be rotated continuously in a single direction, that indicates the servo has the internal stop removed and is a continuous rotation servo. Does it have a small adjusting pot? Below is some servo test code that might be useful for testing a continuous rotation servo.
// zoomkat 3-28-14 serial servo incremental test code
// using serial monitor type a character (s to increase or a
// to decrease) and enter to change servo position
// (two hands required, one for letter entry and one for enter key)
// use strings like 90x or 1500x for new servo position
// for IDE 1.0.5 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.
#include<Servo.h>
String readString;
Servo myservo;
int pos=1500; //~neutral value for continous rotation servo
//int pos=90;
void setup()
{
myservo.attach(7, 400, 2600); //servo control pin, and range if desired
Serial.begin(9600);
Serial.println("serial servo incremental test code");
Serial.println("type a character (s to increase or a to decrease)");
Serial.println("and enter to change servo position");
Serial.println("use strings like 90x or 1500x for new servo position");
Serial.println();
}
void loop()
{
while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
delay(2); //slow looping to allow buffer to fill with next character
}
if (readString.length() >0) {
if(readString.indexOf('x') >0) {
pos = readString.toInt();
}
if(readString =="a"){
(pos=pos-1); //use larger numbers for larger increments
if(pos<0) (pos=0); //prevent negative number
}
if (readString =="s"){
(pos=pos+1);
}
if(pos >= 400) //determine servo write method
{
Serial.println(pos);
myservo.writeMicroseconds(pos);
}
else
{
Serial.println(pos);
myservo.write(pos);
}
}
readString=""; //empty for next input
}
hatts:
I set mine up just exactly the way this person did with the wiring and the code and what my servo does is just spin continuously.
I suspect you have two continuous rotation servos, or a broken servo. The only way to be absolutely sure is to get a third one that you KNOW is a regular servo.
I have arduino nano (tried also with uno) and I tried the sweep example. I have connected 5v to red cable, gnd to black and white to D9. I have copied the code and uploaded it.
What happends is that 180 degree sero moves only in one direction and in slow small steps (not continous) and when it reaches limit, it stops. I tried different servo (360 degree) and it moves continuosly without stoping in one direction. I tried different board but without change.
You need to post the program you are using so that we can see what you can see.
A program for a regular servo (i.e. one with limited travel) will not be suitable for a continuous rotation servo (i.e. one with unlimited travel).
Do NOT power a servo from the Arduino 5v pin because the servo needs more current and may damage the Arduino or cause it to operate erractically. Give the servo its own power supply with a common GND with the Arduino.
I am doing this tutorial https://www.arduino.cc/en/Tutorial/Sweep .The servo (SM-S2309S) is from the starting kit and everything is connected as in the tutorial.
so I tried to compile and upload it with different computer and it works without problems. I have no idea what i did to other one. At least I know it is not problem of code or arduino.