Hi everyone, I'm new to Arduino and programming. I tried to test around the Tower Pro SG90 and wanna to make it keep 180 degree go left and right in loop. I downloaded some sample code from web (as below), it works BUT I noticed that the motor stop moving after 5 rounds (left and right) for around 1-2 second, and it move back to (i guess) the pos "0" and go left and right again for 5 rounds and it stop again.
Please can someone tell me what's wrong with that? Thanks.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
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>=1; 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
}
}
I tried to test around the Tower Pro SG90
How are you powering the servo? You are not having the Arduino supply the power, are you?
I connected the USB cable on the arduino and the computer, it's with power and also connected the "GND" to the brown of the servo, "5V" to red, and "PIN 9" to orange.
I connected the USB cable on the arduino and the computer, it's with power and also connected the "GND" to the brown of the servo, "5V" to red, and "PIN 9" to orange.
So, you ARE expecting the Arduino to power the servo. Well, you can forget that. Google how to power a servo.
Add a call to Serial.begin() and a call to Serial.println() in setup(), and see what is happening. I strongly suspect that the Arduino is resetting because the servo is sucking too much current.
Is the servo fixed to rotate continuously or does it have a restriction to 180 degrees. (you can tell by using your hands to rotate the servo. If it stops at a certain point than its a 180 servo.) Also, can you please explain your problem, I am not sure I understand the issue.
Thanks PaulS. Will try to google that.
Thanks Mrbobokins. Sorry for my poor english. My servo is 180 degrees only , and my problem is the servo can rotate as expected by moving left and right (0 - 180 and 180 - 0), but somehow it rotates exactly 5 times (left and right), and it stop moving for 1 second, then it rotate back to 0 and start moving left and right again. It keeps looping in this patterns.
And my connection is simple:
Connected the USB cable on the arduino and the computer,
Connected the "GND" to the brown of the servo,
Connected the "5V" to red of the servo,
Connected the "PIN 9" to orange of the servo.
I just want the servo keep rotate (left and right for 180 degrees) continuously.
Please kindly help. Thanks.
Will try to google that.
What is there to google? Google isn't going to hold your hand and bend your fingers to make you type:
Serial.begin(115200);
Serial.println("Arduino is starting");
in setup().
Once you do that, on your own volition, and open the Serial Monitor application, you will see either:
Arduino is starting
or
Arduino is starting
Arduino is starting
Arduino is starting
Arduino is starting
Arduino is starting
Arduino is starting
Arduino is starting
Arduino is starting
Arduino is starting
If you see the latter, you are drawing too much current, and the Arduino is resetting.
That is strange. Your code seems correct and the way you hooked your servo up is the same way I've always hooked mine up. Do you have another servo or Arduino you could test to see if it may just be one of those that are causing your issue because your code seems perfectly fine as does your hookup. Maybe put a small ohm resistor in between the ground and brown or black wire of the servo to see if the servo draws too much current.