# Servo coding (solved)

**URL:** <http://community.robotshop.com/forum/t/servo-coding-solved/9646>\
**Category:** General Discussions\
**Tags:** legacy, arduino, servo, c, programming, code, coding\
**Created:** [November 1, 2011, 6:07pm UTC](http://community.robotshop.com/forum/t/servo-coding-solved/9646 "2011-11-01T18:07:48Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![giedow](http://community.robotshop.com/forum/letter_avatar/giedow/32/6_449c533f586939b6e5f246632a46421f.png) [@giedow](http://community.robotshop.com/forum/u/giedow)\
**Post date:** [November 1, 2011, 6:07pm UTC](http://community.robotshop.com/forum/t/servo-coding-solved/9646/1 "2011-11-01T18:07:48Z")

</div>

[arduino\_servo.png](http://community.robotshop.com/forum/uploads/default/original/2X/3/3df21cb97b22021d812668704fc9e33baaa167b7.png) (17522Bytes)  
[arduino\_servo\_a\_or\_b.png](http://community.robotshop.com/forum/uploads/default/original/2X/3/3e3b1a7d07b68048f0acbd4d22ea6b6f0796ddf9.png) (18126Bytes)

It is working now, thanks to everyone who helped me out!!!

&nbsp;

&nbsp;

I am trying to make my servo work,but if I start my arduino, with code and servo attached, it just goes to one position and stays there twitching. Can someone help me?

**second try**

I changed the delay to two seconds, but the servo only goes to the first 100 degree and does not move after that unless if I turn the servo. Could it be the servo? I use:[http://www.dealextreme.com/p/sg90-mini-servo-with-gears-and-parts-2kg-torque-35764](http://www.dealextreme.com/p/sg90-mini-servo-with-gears-and-parts-2kg-torque-35764)

Code:

&nbsp;

#include \<Servo.h\>&nbsp;

&nbsp;

Servo myservo; &nbsp;// create servo object to control a servo&nbsp;

&nbsp;

void setup()&nbsp;

{&nbsp;

&nbsp; myservo.attach(9); &nbsp;// attaches the servo on pin 9 to the servo object&nbsp;

}&nbsp;

&nbsp;

void loop()&nbsp;

{&nbsp;

&nbsp;myservo.write(0); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// sets the servo position according to the scaled value&nbsp;

&nbsp; delay(2000); &nbsp;

&nbsp;myservo.write(100); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// sets the servo position according to the scaled value&nbsp;

&nbsp; delay(2000); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// waits for the servo to get there&nbsp;

&nbsp; &nbsp;myservo.write(0); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// sets the servo position according to the scaled value&nbsp;

&nbsp; delay(2000); &nbsp; &nbsp;

&nbsp;myservo.write(100); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// sets the servo position according to the scaled value&nbsp;

&nbsp; delay(2000); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// waits for the servo to get there&nbsp;

}&nbsp;

&nbsp;

&nbsp;

**First try**

I used a part of the knob controlled servo example.

Code:

&nbsp;

&nbsp;

#include \<Servo.h\>&nbsp;

&nbsp;

Servo myservo; &nbsp;// create servo object to control a servo&nbsp;

&nbsp;

&nbsp;

void setup()&nbsp;

{&nbsp;

&nbsp; myservo.attach(9); &nbsp;// attaches the servo on pin 9 to the servo object&nbsp;

}&nbsp;

&nbsp;

void loop()&nbsp;

{&nbsp;

&nbsp;myservo.write(0); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// sets the servo position according to the scaled value&nbsp;

&nbsp; delay(20); &nbsp;

&nbsp;myservo.write(100); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// sets the servo position according to the scaled value&nbsp;

&nbsp; delay(20); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// waits for the servo to get there&nbsp;

&nbsp; &nbsp;myservo.write(0); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// sets the servo position according to the scaled value&nbsp;

&nbsp; delay(20); &nbsp; &nbsp;

}&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![geirandersen](http://community.robotshop.com/forum/user_avatar/community.robotshop.com/geirandersen/32/25938_2.png) [@geirandersen](http://community.robotshop.com/forum/u/geirandersen)\
**Post date:** [November 1, 2011, 6:20pm UTC](http://community.robotshop.com/forum/t/servo-coding-solved/9646/2 "2011-11-01T18:20:26Z")

</div>

**If your servo is taking its**

If your servo is taking its power from the Arduino, try an external power source.

---

<div class="post-metadata">

**Author:** ![christhecarpenter](http://community.robotshop.com/forum/user_avatar/community.robotshop.com/christhecarpenter/32/38394_2.png) [@christhecarpenter](http://community.robotshop.com/forum/u/christhecarpenter)\
**Post date:** [November 1, 2011, 7:20pm UTC](http://community.robotshop.com/forum/t/servo-coding-solved/9646/3 "2011-11-01T19:20:54Z")

</div>

**Too fast**

Your delays are not long enough to allow your servo to get to where it is going. As soon as it trys to move to a position, 20ms later it is told to go somewhere else. Make your delays longer.

---

<div class="post-metadata">

**Author:** ![davidschwarz](http://community.robotshop.com/forum/letter_avatar/davidschwarz/32/6_449c533f586939b6e5f246632a46421f.png) [@davidschwarz](http://community.robotshop.com/forum/u/davidschwarz)\
**Post date:** [November 1, 2011, 9:19pm UTC](http://community.robotshop.com/forum/t/servo-coding-solved/9646/4 "2011-11-01T21:19:11Z")

</div>

**unnessary code**

The second one of these is not neeted

> _myservo.write(0); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// sets the servo position according to the scaled value&nbsp;_
> 
> _delay(20);_

Also as Chris said your delays need to be longer.

So instead&nbsp;of:

void loop()&nbsp;

{&nbsp;&nbsp;myservo.write(0); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// sets the servo position according to the scaled value&nbsp;

&nbsp; delay(20); &nbsp;

&nbsp; myservo.write(100); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// sets the servo position according to the scaled value&nbsp;

&nbsp; delay(20); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// waits for the servo to get there&nbsp;

&nbsp; myservo.write(0); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// sets the servo position according to the scaled value&nbsp;

&nbsp; delay(20); &nbsp; &nbsp;

}

You should have:

void loop()&nbsp;

{&nbsp;

&nbsp;myservo.write(0); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// sets the servo position&nbsp;to zero degrees

&nbsp; delay(750);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // waits for the servo to get there

&nbsp;myservo.write(100);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// sets the servo position&nbsp;to&nbsp;100 degrees

&nbsp; delay(750); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// waits for the servo to get there&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

}

---

<div class="post-metadata">

**Author:** ![hardmouse](http://community.robotshop.com/forum/user_avatar/community.robotshop.com/hardmouse/32/39283_2.png) [@hardmouse](http://community.robotshop.com/forum/u/hardmouse)\
**Post date:** [November 2, 2011, 11:42am UTC](http://community.robotshop.com/forum/t/servo-coding-solved/9646/5 "2011-11-02T11:42:57Z")

</div>

**Agreed, try delay(500) or**

Agreed, try delay(500) or even longer

---

<div class="post-metadata">

**Author:** ![calculon320](http://community.robotshop.com/forum/letter_avatar/calculon320/32/6_449c533f586939b6e5f246632a46421f.png) [@calculon320](http://community.robotshop.com/forum/u/calculon320)\
**Post date:** [November 2, 2011, 1:17pm UTC](http://community.robotshop.com/forum/t/servo-coding-solved/9646/6 "2011-11-02T13:17:22Z")

</div>

**add a counter**

If your bot is doing a lot of things simultaneously in addition to moving servos, you could also benefit from a “servo counter” variable that counts how long since you gave the myservo command (in this case, your targe would be 750 or so). Once the servo is in place, you can turn it off. This way the servo won’t sit there pulling your current and making noise. once the servo is told to move again, just reset the variable.

---

<div class="post-metadata">

**Author:** ![giedow](http://community.robotshop.com/forum/letter_avatar/giedow/32/6_449c533f586939b6e5f246632a46421f.png) [@giedow](http://community.robotshop.com/forum/u/giedow)\
**Post date:** [November 2, 2011, 5:19pm UTC](http://community.robotshop.com/forum/t/servo-coding-solved/9646/7 "2011-11-02T17:19:42Z")

</div>

**Thank you**

I tried, but it still only goes to one position

---

<div class="post-metadata">

**Author:** ![giedow](http://community.robotshop.com/forum/letter_avatar/giedow/32/6_449c533f586939b6e5f246632a46421f.png) [@giedow](http://community.robotshop.com/forum/u/giedow)\
**Post date:** [November 2, 2011, 5:21pm UTC](http://community.robotshop.com/forum/t/servo-coding-solved/9646/8 "2011-11-02T17:21:26Z")

</div>

**Thanks**

I tried this one two, but that did not work either

---

<div class="post-metadata">

**Author:** ![geirandersen](http://community.robotshop.com/forum/user_avatar/community.robotshop.com/geirandersen/32/25938_2.png) [@geirandersen](http://community.robotshop.com/forum/u/geirandersen)\
**Post date:** [November 2, 2011, 5:37pm UTC](http://community.robotshop.com/forum/t/servo-coding-solved/9646/9 "2011-11-02T17:37:37Z")

</div>

**Again. Have you tried with a**

Again. Have you tried with a separate power supply for the servo?

---

<div class="post-metadata">

**Author:** ![giedow](http://community.robotshop.com/forum/letter_avatar/giedow/32/6_449c533f586939b6e5f246632a46421f.png) [@giedow](http://community.robotshop.com/forum/u/giedow)\
**Post date:** [November 2, 2011, 5:53pm UTC](http://community.robotshop.com/forum/t/servo-coding-solved/9646/10 "2011-11-02T17:53:52Z")

</div>

**I have**

I have, the servo is more powerfull, butt does only go to one position

---

<div class="post-metadata">

**Author:** ![davidschwarz](http://community.robotshop.com/forum/letter_avatar/davidschwarz/32/6_449c533f586939b6e5f246632a46421f.png) [@davidschwarz](http://community.robotshop.com/forum/u/davidschwarz)\
**Post date:** [November 2, 2011, 5:54pm UTC](http://community.robotshop.com/forum/t/servo-coding-solved/9646/11 "2011-11-02T17:54:40Z")

</div>

**Is you servo connected correctly?**  
Can you post a picture of you setup?  
Normally servos cant be turned (easily by hand) if there is a signal going to them. They will move on startup as the servo tries to gather the signal being sent to it.

---

<div class="post-metadata">

**Author:** ![giedow](http://community.robotshop.com/forum/letter_avatar/giedow/32/6_449c533f586939b6e5f246632a46421f.png) [@giedow](http://community.robotshop.com/forum/u/giedow)\
**Post date:** [November 2, 2011, 5:59pm UTC](http://community.robotshop.com/forum/t/servo-coding-solved/9646/12 "2011-11-02T17:59:03Z")

</div>

**I am not building an robot yet**

I am not building a robot yet, so I am only using the wall outlet. but do can you give an example of an counter( I could not find it on ebay)

---

<div class="post-metadata">

**Author:** ![giedow](http://community.robotshop.com/forum/letter_avatar/giedow/32/6_449c533f586939b6e5f246632a46421f.png) [@giedow](http://community.robotshop.com/forum/u/giedow)\
**Post date:** [November 2, 2011, 6:05pm UTC](http://community.robotshop.com/forum/t/servo-coding-solved/9646/13 "2011-11-02T18:05:27Z")

</div>

\*\* Geir Andersen told me to\*\*

&nbsp;[Geir Andersen](http://community.robotshop.com/letsmakerobots/user/10562 "Geir Andersen")&nbsp;told me to power the servo externally and when I did it was pretty hard to turn

---

<div class="post-metadata">

**Author:** ![davidschwarz](http://community.robotshop.com/forum/letter_avatar/davidschwarz/32/6_449c533f586939b6e5f246632a46421f.png) [@davidschwarz](http://community.robotshop.com/forum/u/davidschwarz)\
**Post date:** [November 2, 2011, 6:12pm UTC](http://community.robotshop.com/forum/t/servo-coding-solved/9646/14 "2011-11-02T18:12:29Z")

</div>

**Change the myservo.write values**  
Specifically 0 to 45 and 100 to 180.  
Does the servo move to a different angle?

---

<div class="post-metadata">

**Author:** ![giedow](http://community.robotshop.com/forum/letter_avatar/giedow/32/6_449c533f586939b6e5f246632a46421f.png) [@giedow](http://community.robotshop.com/forum/u/giedow)\
**Post date:** [November 2, 2011, 6:48pm UTC](http://community.robotshop.com/forum/t/servo-coding-solved/9646/15 "2011-11-02T18:48:31Z")

</div>

**yes**

yes if i use only one part of the code

---

<div class="post-metadata">

**Author:** ![calculon320](http://community.robotshop.com/forum/letter_avatar/calculon320/32/6_449c533f586939b6e5f246632a46421f.png) [@calculon320](http://community.robotshop.com/forum/u/calculon320)\
**Post date:** [November 3, 2011, 1:21am UTC](http://community.robotshop.com/forum/t/servo-coding-solved/9646/16 "2011-11-03T01:21:32Z")

</div>

**how about a button?**

**Try this steps:**

1. Make sure the servo is plugged in correctly. The orange/white should be connected to arduino pin 9, the red to 5v, and the white/brown to G. If the servo doesn’t move at all, look into powering it externally, but the board should be able to handle a single servo without a problem.

2. Connect a button or temporary switch. If you don’t have a button, you can just use two wires, one to the arduino and the other to ground and touch them together for a second. If you don’t have any sort of button, try Serial.

3. Try the following code. This code will make the servo go to the center at first. Then every time you push the button, it should swing to either side.

#include \<Servo.h\>&nbsp;

Servo myservo; &nbsp;// create servo object to control a servo&nbsp;

boolean ServLeft=false; &nbsp;//this variable tells the servo which direction to go. not sure if the name is accurate.

void setup()&nbsp;

{&nbsp;

myservo.attach(9); &nbsp;// attaches the servo on pin 9 to the servo object&nbsp;

pinMode(12,INPUT); &nbsp;//YOU NEED TO ATTACH A BUTTON OR SWITCH BETWEEN THIS PIN AND GROUND

digitalWrite(12,HIGH); //this activates the atmega’s internal pullup resistor

myservo.write(90);

delay(2000);

}&nbsp;

void loop()&nbsp;

{

if (digitalRead(12)==LOW){ &nbsp; &nbsp;//this button tells the arduino to change servo direction

&nbsp; (ServLeft)=(!ServLeft); //toggles which direction the servo will

&nbsp;if (ServLeft==true){

&nbsp; &nbsp;myservo.write(0); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// sets the servo position according to the scaled value&nbsp;

&nbsp; delay(2000); &nbsp;

&nbsp;}

&nbsp; if (ServLeft==false){

&nbsp;myservo.write(100); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// sets the servo position according to the scaled value&nbsp;

&nbsp; delay(2000); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// waits for the servo to get there&nbsp;

&nbsp; }

}&nbsp;

}&nbsp;

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![calculon320](http://community.robotshop.com/forum/letter_avatar/calculon320/32/6_449c533f586939b6e5f246632a46421f.png) [@calculon320](http://community.robotshop.com/forum/u/calculon320)\
**Post date:** [November 3, 2011, 1:24am UTC](http://community.robotshop.com/forum/t/servo-coding-solved/9646/17 "2011-11-03T01:24:04Z")

</div>

**counter variable**

Let’s get your servo working first, then we can worry about prettying up your code. What’s the bot going to do?

---

<div class="post-metadata">

**Author:** ![davidschwarz](http://community.robotshop.com/forum/letter_avatar/davidschwarz/32/6_449c533f586939b6e5f246632a46421f.png) [@davidschwarz](http://community.robotshop.com/forum/u/davidschwarz)\
**Post date:** [November 3, 2011, 9:04am UTC](http://community.robotshop.com/forum/t/servo-coding-solved/9646/18 "2011-11-03T09:04:22Z")

</div>

**So the servo is going to different angles-**  
What do you mean by only one part of the code?

---

<div class="post-metadata">

**Author:** ![giedow](http://community.robotshop.com/forum/letter_avatar/giedow/32/6_449c533f586939b6e5f246632a46421f.png) [@giedow](http://community.robotshop.com/forum/u/giedow)\
**Post date:** [November 3, 2011, 1:07pm UTC](http://community.robotshop.com/forum/t/servo-coding-solved/9646/19 "2011-11-03T13:07:58Z")

</div>

**no still not**

it only goes like 5% anticlock ways and back

---

<div class="post-metadata">

**Author:** ![christhecarpenter](http://community.robotshop.com/forum/user_avatar/community.robotshop.com/christhecarpenter/32/38394_2.png) [@christhecarpenter](http://community.robotshop.com/forum/u/christhecarpenter)\
**Post date:** [November 3, 2011, 2:24pm UTC](http://community.robotshop.com/forum/t/servo-coding-solved/9646/20 "2011-11-03T14:24:57Z")

</div>

**Are your grounds tied together?**

I read a bit further up that you had switched to an external power supply going to your servo. Did you tie the ground of this external supply to the ground of your brain? You should do this.

[Next page](http://community.robotshop.com/forum/t/servo-coding-solved/9646.md?page=2)
