Servo sweep not working

I'm fairly new to Arduino.
I wanted to try a project using a servo, but the start out with it, I tried the standerd code of the servo sweep.
The problem is that the sweep is going to the 180 pos, but it takes 8 seconds to go back to the original pos.
I tried changing the delay, with no effect.

Anyone knows how I can fix this?

All help is welcome!

What standard code would that be?

Paul

What servo are you using? How do you have it wired up?
It is pretty much necessary to power a servo from a separate power supply. Yes, the tutorial shows it powered from the 5v pin. This is bad. It usually works for a very small servo, without any load on the servo arm.
But if you try to use multiple servos, or attach anything to the servo arm, you will shut down the arduino or cause damage.
Google "servo separate power"

Use a 4xAA battery pack to power the servo, and connect the grounds. That fixes about 95% of the problems reported on this forum.

the code I'm using is: https://www.arduino.cc/en/Tutorial/Sweep

the servo I'm using is: Feetech 9 KG Torque Analog Servo - Standard RC Metal Gear Servo Motor

It is connected via the standard 5V, gnd, and the 9 pin. (I'm also using an Arduino Yun Rev 2 if this helps anyone)

We are not interested in a link to where you found the code. We need to see your code. Post it here, using code tags.

There is a good link at the top of this forum named "How to use this forum - please read." Read through it. It will help you present your request for help in a way that will garner the most support. Note that it details how to post code using code tags.

You are attempting to run a standard servo off of the arduino 5v pin. As mentioned above, use a separate power supply. Connect the grounds.

While I agree that it's wrong of the tutorial to show powering a servo (even a tiny one, and I really despair at that tutorial being incorrect for years) from the Arduino 5V, and that OP should not do that, how would the lack of current explain the OP's problem of it seeming to work correctly in one direction but not the other?

bvernimmen:
The problem is that the sweep is going to the 180 pos, but it takes 8 seconds to go back to the original pos.

[rant]
Why is it that servo datasheets hardly ever quote the current?
[/rant]

To reply to all above messages since I’m still having trouble:

My code (I’ve modified the code I previously mentioned)

#include <Servo.h>

Servo myservo;  


int pos = 0;    

void setup() {
  myservo.attach(9);  
}

void loop() {
  for (pos = 0; pos <= 90; pos += 90) { 
    myservo.write(pos);              
    delay(15);                       
  } 
  for (pos = 90; pos >= 0; pos -= 90) { 
    myservo.write(pos);              
    delay(15);                       
  }
}

While using the pos±= 90, the code at least works, normally this would be a 1, but this didn’t work.

Further you can find how I connected the servo to the Arduino with and without extra power supply, using the external power supply didn’t even make the servo move.

Also I think I didn’t explain it good enough, the servo only moves in the opposite direction every 8 seconds, so it takes 16 seconds to do a full 90 degree movement.

Normally with this contruction, the servo plugged into the Arduino’s 5V port, it should still work even though it is bad for the board.

I’ve asked a local retailer for Arduino and he says that it should work. So I think my servo might be broken?

T.I.A. for all the answers.

Normally with this contruction, the servo plugged into the Arduino's 5V port, it should still work even though it is bad for the board.

No, "normally" it DOES NOT work, and doing so could even destroy the Arduino.

The 3V battery shown in the "Fritzing for idiots" diagram will not run any servo. Use a 4xAA battery pack. Make sure that you have connected all the grounds.

Post a proper hand drawn wiring diagram, and post a link to the exact servo you have.

jremington:
No, "normally" it DOES NOT work, and doing so could even destroy the Arduino.

The 3V battery shown in the "Fritzing for idiots" diagram will not run any servo. Use a 4xAA battery pack.

how come this works?

btw I did use 6V total of batteries, I could only find the 3V in Fritzing.

Do you think people post youtube videos of failed projects?

On this forum, at least 95% of the servo problems (posted daily), are caused by people following bad advice and attempting to use the 5V output of the Arduino to power their servos. But sometimes it does work, for a while.

I could only find the 3V in Fritzing.

That is one of the many reasons why we so strongly recommend to avoid Fritzing. Such diagrams are always confusing and usually wrong.

Just looking at a snippet of your code:

  for (pos = 0; pos <= 90; pos += 90) {
    myservo.write(pos);             
    delay(15);                       
  }

This is not the standard servo sweep. It looks like you changed the end point so that it will stop at 90 rather than 180. That is OK.
It also looks like you changed the step size from 1 to 90. That is odd.

Can you try that with += 1 (and -=1 in the second loop)

vinceherman:
Just looking at a snippet of your code:

  for (pos = 0; pos <= 90; pos += 90) {

myservo.write(pos);           
    delay(15);                     
  }




This is not the standard servo sweep. It looks like you changed the end point so that it will stop at 90 rather than 180. That is OK.
It also looks like you changed the step size from 1 to 90. That is odd.

Can you try that with += 1 (and -=1 in the second loop)

When I tried using +=1 and -=1, the servo will move for like 1 degree and then move back.

The way I see it, your current code executes exactly 2 iterations of the loop going from 0 to 90.
The first iteration it commands the servo to go to 0 and then delays 15ms.
The second (and final) it commands the servo to go to 90 and then delays 15ms.

Then the second loop from 90 to 0 also executes 2 iterations.
The first iteration it commands the servo to go to 90 and then delays 15ms.
The second (and final) it commands the servo to go to 0 and then delays 15ms.

In total, the servo is given 30ms to get from 0 to 90.
The feetec servo has a reported speed of 0.15sec/60degree. so 150ms for 60 degrees. That is 225ms for a 90 degree movement.

This means that your servo will be twitching back and forth in very small movements.

The servo sweep is intended to move it just a single degree at a time, with that small 15ms being adequate time to get there. That would be 90 (91?) iterations. That makes a slow movement from one end of the travel to the other.

I still say the right move is to put in the +=1, the -=1 and give it a go.

vinceherman:
The way I see it, your current code executes exactly 2 iterations of the loop going from 0 to 90.
The first iteration it commands the servo to go to 0 and then delays 15ms.
The second (and final) it commands the servo to go to 90 and then delays 15ms.

Then the second loop from 90 to 0 also executes 2 iterations.
The first iteration it commands the servo to go to 90 and then delays 15ms.
The second (and final) it commands the servo to go to 0 and then delays 15ms.

In total, the servo is given 30ms to get from 0 to 90.
The feetec servo has a reported speed of 0.15sec/60degree. so 150ms for 60 degrees. That is 225ms for a 90 degree movement.

This means that your servo will be twitching back and forth in very small movements.

The servo sweep is intended to move it just a single degree at a time, with that small 15ms being adequate time to get there. That would be 90 (91?) iterations. That makes a slow movement from one end of the travel to the other.

I still say the right move is to put in the +=1, the -=1 and give it a go.

So I tried putting the delay to 230ms and the -=1 and +=1 back. This resulted in it "sweeping".
It does sweep now, but it still takes 8 seconds to make the full 90 degrees. now it just does it in 1 degree steps.

I need to find a way to make the 90 degrees in 1 sweep that doesn't take 8 seconds.

What happens if you leave the delay at 15ms?

vinceherman:
What happens if you leave the delay at 15ms?

it turns 1 degree, then goes back 1 degree.

Post your code please.

vinceherman:
Post your code please.

My last used code:

 #include <Servo.h>

Servo myservo; 

int pos = 0;    

void setup() {
  myservo.attach(9);  
}

void loop() {
  for (pos = 0; pos <= 90; pos += 1) { 
    // in steps of 1 degree
    myservo.write(pos);              
    delay(15);                     
  } 
  for (pos = 90; pos >= 0; pos -= 1) { 
    myservo.write(pos);              
    delay(15);                       
  }
}

I am not seeing how it could do that, move one degree and them move on degree back.

It should move degree by degree, but quickly enough that it would appear to be on continuous move.
It should take a bit under 1.5 seconds to move from 0 to 90 and then the same time to move from 90 back to 0.

How quickly is it moving back and forth for the 1 degree movement you just described?

Youi say that when you had the delay set to 230 that it took 8 seconds. Is that 8 seconds for the move from 0 to 90? Or 8 seconds for 0 to 90 and back to 0?
the delay(230) should be just under a quarter seconds. That means that a 91 iteration sweep to 90 and a 91 iteration sweep back should take about 40 seconds.

None of this is matching what you are describing.