Trouble with servos

Hello,so I'm a little new to the arduino, but i have an arduino uno(real one) and a tower pro SG90 servo. I made the code bellow to try to make it go from 160 degrees to 20 degrees with a 1 second delay in between. Any one have any ideas on whats wrong with the code? Thanks!

#include <Servo.h>
Servo myservo;

void setup() {
myservo.attach(13);

}

void loop() {
myservo.write(160);
delay(1000);
myservo.write(20);
delay(1000);

}

What is it doing? How do you have it hooked up? Do you get any errors when uploading?

The code looks simple and correct. My guess is that your problem lies in how you are powering the servos. They need a dedicated, external power supply and should NOT be powered by the Arduino.

How are you powering your servo(s)?

Have you checked the wiring ?
Do you have a common ground?

The code looks fine. But if you are expecting write(20) and write(160) to be specific positions exactly 140 degrees apart that's not how the Servo library works. Servos all behave differently so you need to fine tune the values you use in write().

If you have some other problem then tell us what it is.

Steve

Power_Broker:
The code looks simple and correct. My guess is that your problem lies in how you are powering the servos. They need a dedicated, external power supply and should NOT be powered by the Arduino.

How are you powering your servo(s)?

I just found the problem, it was a faulty servo i tried another servo and it works. Why cant they be powered by the arduino? These micro servos( https://www.ebay.com/i/112012439026?chn=ps&dispItem=1 ) are meant to be powered by 5 volts. Also i had a bigger servo(the ones that cant be powered by an arduino), and ive tried to use it with the 5 volts from the arduino. Did me doing that damage the arduino in anyway. Lastly, i would try this but i don't if it would do anything bad to the arduino, what would happen if i took the delays out of that code?
Thanks!

If you are simply trying to get an unloaded servo to move, you might get away with powering it with the arduino. Some people have even been aboe to get 2 servos to move when powered by the arduino.
but you are inviting problems.
The moment you try to power a servo that has to move a load, you will immediately exceed the power capabilities of the 5v pin. Best case scenario is that your board shuts down.

So, take the advice of the ones who have been there. Power the servos from an external source. Pic below.

On the delays, if you remove both, your program will tell the servo to move to 160. Then, before it has a chance to move there, it will tell it to move to 20.
You might be able to see the servo vibrate, but I expect it won't do much.

ServoDiagram.jpg

the_quadster:
....are meant to be powered by 5 volts. Also i had a bigger servo(the ones that cant be powered by an arduino), and ive tried to use it with the 5 volts from the arduino.

You can keep doing random things and asking why they broke, or you can pause here and go learn about the differences between voltage and current.

vinceherman:
If you are simply trying to get an unloaded servo to move, you might get away with powering it with the arduino. Some people have even been aboe to get 2 servos to move when powered by the arduino.
but you are inviting problems.
The moment you try to power a servo that has to move a load, you will immediately exceed the power capabilities of the 5v pin. Best case scenario is that your board shuts down.

So, take the advice of the ones who have been there. Power the servos from an external source. Pic below.

On the delays, if you remove both, your program will tell the servo to move to 160. Then, before it has a chance to move there, it will tell it to move to 20.
You might be able to see the servo vibrate, but I expect it won't do much.

ServoDiagram.jpg

Oh, i think i gotcha. So you can power the micro servo unloaded on the 5 volt pin but its not the best idea right? I tested the voltage that the servo pulls unloaded and its 4.8 volts. Also the only power supply/battery i have is a 2 cell lipo(7.4 volts) would that work or what should i order to use as a seperate 5 volt psu/battery for the servo?
Thanks!

" I tested the voltage that the servo pulls unloaded and its 4.8 volts. "

It's not a question of voltage. It's about current. The servo pulls more amps than the board can handle.

Delta_G:
" I tested the voltage that the servo pulls unloaded and its 4.8 volts. "

It's not a question of voltage. It's about current. The servo pulls more amps than the board can handle.

I looked into some websites and they said that the arduino can handle the amps(270mA FS90 9g Mini Servo – Addicore ) This website safety - How much current can I draw from the Arduino's pins? - Electrical Engineering Stack Exchange says the arduino can handle 400 mA if its plugged into the usb port and 900mA if its plugged into the dc power plug. And what would be a good 5volt power suppy/ battery to power the sg90 sepperatly?
Thanks and sorry if i'm being a little nieve

Ok. Well you read a stack exchange page so you probably know more about it than we do. You can just ignore our advice and let us know how it works out.

Delta_G:
Ok. Well you read a stack exchange page so you probably know more about it than we do. You can just ignore our advice and let us know how it works out.

No, I'm not saying your wrong its just i dont understand this and im trying to under stand your point of view, that's why im asking you for a good 5volt power supply to order.

the_quadster:
I looked into some websites and they said that the arduino can handle the amps(270mA FS90 9g Mini Servo – Addicore ) This website safety - How much current can I draw from the Arduino's pins? - Electrical Engineering Stack Exchange says the arduino can handle 400 mA if its plugged into the usb port and 900mA if its plugged into the dc power plug. And what would be a good 5volt power suppy/ battery to power the sg90 sepperatly?
Thanks and sorry if i'm being a little nieve

One thing that you didn't mention is that even an sg90 can pull more than 1A when under full load. You should get a buck converter rated for at least as many amps as you have servos.

Power_Broker:
One thing that you didn't mention is that even an sg90 can pull more than 1A when under full load. You should get a buck converter rated for at least as many amps as you have servos.

Thanks for the comment i understand why its a bad idea to use a servo directly to a arduino! But with a buck converter doesn't that step down voltage to increase amps? And the servo needs 5 volts so if i stepped the volts down from 5volts from the arduino i would be getting more apms but i wouldn't be getting my 5volts needed. Also to get the arduino 5volt pin's 400mA to 1A how many volts would i lose?

the_quadster:
Thanks for the comment i understand why its a bad idea to use a servo directly to a arduino! But with a buck converter doesn't that step down voltage to increase amps? And the servo needs 5 volts so if i stepped the volts down from 5volts from the arduino i would be getting more apms but i wouldn't be getting my 5volts needed. Also to get the arduino 5volt pin's 400mA to 1A how many volts would i lose?

I think you don't understand how power supplies work. You'd power the buck converter off a battery (or array of batteries) and then convert the raw battery voltage to 5V (which would power the servos - up to the rated current of the buck converter).

P.S. Never even think of sourcing 1A from an Arduino. That will most definitely kill it!

the_quadster:
Thanks for the comment i understand why its a bad idea to use a servo directly to a arduino! But with a buck converter doesn't that step down voltage to increase amps? And the servo needs 5 volts so if i stepped the volts down from 5volts from the arduino i would be getting more apms but i wouldn't be getting my 5volts needed. Also to get the arduino 5volt pin's 400mA to 1A how many volts would i lose?

Wrong. The more volts you are stepping down from, the LESS amps you actually get.

Btw, you really need to take a time out and go learn about what voltage is, and what current is.
Throwing around the word 'amps' when you have no clue is wasting everyone's time.

the_quadster:
No, I'm not saying your wrong its just i dont understand this and im trying to under stand your point of view, that's why im asking you for a good 5volt power supply to order.

You saw my pic on reply #6. It shows 4 1.5v alkaline batteries as the power source for the servo.
Many many times this arrangement has been used successfully.

vinceherman:
You saw my pic on reply #6. It shows 4 1.5v alkaline batteries as the power source for the servo.
Many many times this arrangement has been used successfully.

Wouldn't that fry the servo by powering it with an extra volt?

Model servos have been running off 6 V DC for over forty years.