servo motor rotation

am planning to make some rotation using servo motor. right now I am using 2 HX5010 motors. I want them to rotate 0 to 180 & back one by one.

I have modified the example code Sweep. & the code looks like this

#include <Servo.h>

Servo servo1; // create servo object to control a servo
Servo servo2; // 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()
{
servo1.attach(9); // attaches the servo on pin 9 to the servo object
servo2.attach(9);
}

void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
servo1.write(pos); // tell servo to go to position in variable ‘pos’
delay(30); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
servo2.write(pos); // tell servo to go to position in variable ‘pos’
delay(30); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos < 0; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
servo1.write(pos); // tell servo to go to position in variable ‘pos’
delay(30); // waits 15ms for the servo to reach the position
}
for(pos = 0; pos>=180; pos-=1) // goes from 180 degrees to 0 degrees
{
servo2.write(pos); // tell servo to go to position in variable ‘pos’
delay(30); // waits 15ms for the servo to reach the position
}
}

When I run this code the motors look super confused. the motors are randomly rotating. Am i doing it right? & time of delay fine or I need to increase it?

Need help!!

Attaching two servos to the same pin?
No.
How are you powering them?

Ya I am attaching both the servos to the same pin.

powering them from 5V pin from the arduino board

powering them from 5V pin from the arduino board

Don’t - use a separate supply.

Why are you attaching the two servo objects to the same pin?

Yup my mistake I changed the pins to 9 & 10.
I thought arduino could power 2 motors.

& I also want the motors to rotate to 180 & again back to 0 one after the other.

with the above code the motors are not stopping even after I put long delays

Probably because "loop()" is, well, looping.

I thought arduino could power 2 motors.

I don't ever remember reading that anywhere.

I read it here..

Note servos draw considerable power, so if you need to drive more than one or two,

Two is more than one.

Servos vary enormously in their sizes and current draw, so always play it safe if you don't know what you're doing.

But is it because of the power drawn by arduinos?

No the power drawn by the Arduino is tiny in comparison with what a servo can draw.
Have you got the grounds connected?

Yes, now the rotation is controlled but I want servo motor 1 to rotate 180 degrees & then motor 2 to rotate 180 degrees & then back to 0 degree one by one.

here is the code now

#include <Servo.h>

Servo servo1; // create servo object to control a servo
Servo servo2; // 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()
{
servo1.attach(9); // attaches the servo on pin 9 to the servo object
servo2.attach(10);
}

void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
servo1.write(pos); // tell servo to go to position in variable ‘pos’
delay(50); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
servo1.write(pos); // tell servo to go to position in variable ‘pos’
delay(100); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos < 0; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
servo2.write(pos); // tell servo to go to position in variable ‘pos’
delay(50); // waits 15ms for the servo to reach the position
}

for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
servo2.write(pos); // tell servo to go to position in variable ‘pos’
delay(100); // waits 15ms for the servo to reach the position
}
}

Haven’t changed much except the delays. i want motors to rotate alternately. right now motor1 finishes rotation 0-180 & back to 0 first then motor 2 starts rotating. there is variation of speed too.

there is variation of speed too.

You’ve got different length delays in there.

for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees

It’s a tiny thing, but try to get the code and the comment to agree.

for(pos = 180; pos < 0; pos += 1)  // goes from 0 degrees to 180 degrees

Got it working!!
some looping problem was there…

Here is the code:

#include <Servo.h>

Servo servo1; // create servo object to control a servo
Servo servo2;

int pos = 0; // variable to store the servo position

void setup()
{
servo1.attach(9); // attaches the servo on pin 9 to the servo object
servo2.attach(10);
}

void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
servo1.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); } // waits 15ms for the servo to reach the position
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
servo2.write(pos); // tell servo to go to position in variable ‘pos’
delay(15);
}

for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
servo1.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
{
servo2.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); }
}
}

for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees

Goes from 0 to 179 degrees.

for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees

Goes from 180 to 1 degree.

Thanks for the details :slight_smile: