Arduino Servo Control for Beginners

arduino-servo-setup-1.jpgI managed to get my servo working with Arduino today using this tutorial. Nothing to special. The first time I tried nothing happened because I had the min/max pulse time setup wrong for the hitec servo. With the hitec servos the min pulse is 900µsec and the max is 2100µsec. Once I changed that it worked and should work for all hitec servos. I have looked at the Arduino servo libraries and they all seem unfinished or limited in one way or another. I am currently working on a sketch that will allow the control of up to 11 servos with analog inputs or serial commands. Also in the works is an Aduino servo shield.

Arduino Servo Sketch for analog control of a hitec servo

23 Responses to “Arduino Servo Control for Beginners”

  1. The Stem » Blog Archive » Arduino Servo Control… Screw It, I am Going To Learn Some Assembly Says:

    […] a servo with Arduino is easy, as shown in a previous post. What I have found difficult is getting multiple servos and serial data processing to play nice […]

  2. Blog de Daniel Bernat » Conociendo Arduino (II) Says:

    […] MOTOR: blog post including code for the HItec brand of […]

  3. stan Says:

    Hi I have a question. I am trying to get the arduino servo control to work but im still having issues. Is there anything else that needs to be added at the end of the script. ? if not what kind of results are expected, so far i have no results in my hitec servo. Im assuming im supposed to modify the script somehow.

    any help would be tremendously appreciated.

  4. Brettf Says:

    Well there are a couple of things i need to know first. Do you have a potentiometer hooked up to your arduino? What pins do you have it connected to and what pins do you have connected to your servo?

  5. alice Says:

    Hi. I am new, on the arduino.cc website there is different code to make my servo works. but now I would like to use a sound sensor as a switch for the servo. I am having a lot of difficulties to write a code. does someone has ideas?

  6. Brettf Says:

    Well depends on what you mean by a sound sensor? I need more info before I could comment.

  7. Au Pair Says:

    very nice web site. My English is not so good, so I do not understandt it well, but it seems very good. Thanks

  8. marc Says:

    i will like to build a small robot…

    i would like to have 2 wheels…

    surely i need one servo by wheel?

    somebody can explain me how to connect 2 servo on the arduino?

  9. lsi Says:

    it would be better with other languages support, but thanks..

  10. jerry Says:

    hi!
    i constructed a servo project controlled by arduino that continously rotate from zero degree to 180 degrees then back to zero degree again and so on. could somebody help me to modify the program where it will rotate one complete cycle only from 0-180-0 then finish and no looping. thanks in advance.

  11. anrudh Says:

    how to connect 8 servos for a walking robot

  12. Brettf Says:

    That is part of the problem with a regular arduino is that you can’t connect that many servos. Something I hope to work on in the near future. But no promises.

  13. Neil Says:

    Hi Brett,

    If I want to use a high power servo do I simply share the arduino and servo grounds with the external power, then apply +6 to the servo from the external too?

  14. Brettf Says:

    Based on what I read here http://www.glacialwanderer.com/hobbyrobotics/?p=7 yes.

  15. Rick Says:

    I have a question regarding servos and Arduino.

    I am thinking of using a servo in conjunction with an Ultra-sonic to detect minimum altitude.

    My idea is pretty simple ( I think ) – as my quad copter descends, – when it reaches ( say 5 feet ) the ultra sonic will detect the ground ( which is seen as a change of state, ) and it will then move my servo to maintain throttle.

    I’m looking at an automatic “altitude” or “minimum” hover height.

    I’m guessing that when the ultra sonic causes a ” trigger” in the Arduino program ( state goes high) – I need 2 things to happen..
    1: to “read” the current throttle setting.
    and
    2: transfer that information to a “KK board” which currently controls my quad via the radio receiver.

    Any ideas to get a servo to move whe nan ultra sonic detects a change ?

    Many thanks in advance.

    Rick.

  16. Rick Says:

    I have a question regarding servos and Arduino.

    I am thinking of using a servo in conjunction with an Ultra-sonic to detect minimum altitude.

    My idea is pretty simple ( I think ) – as my quad copter descends, – when it reaches ( say 5 feet ) the ultra sonic will detect the ground ( which is seen as a change of state, ) and it will then move my servo to maintain throttle.

    I’m looking at an automatic “altitude” or “minimum” hover height.

    I’m guessing that when the ultra sonic causes a ” trigger” in the Arduino program ( state goes high) – I need 2 things to happen..
    1: to “read” the current throttle setting.
    and
    2: transfer that information to a “KK board” which currently controls my quad via the radio receiver.

    Any ideas to get a servo to move when an ultra sonic detects a change ?

    Many thanks in advance.

    Rick.

  17. Brettf Says:

    Hello Rick,

    This is certainly possible. However in my experience an ultra-sonic distance sensor is going to give you a fairly inaccurate reading. If you haven’t seen this site before it probably is the place to start. Their forums are going to be full of people much smarter than me. http://diydrones.com/

  18. David Says:

    I am very new here , I want to control a servo by push – button.
    Push/Hold button servo move 80* to 30* / 30* to 80* non-stop , release buttom and servo stop at 80.

    How do i wirte these code? thks

  19. Brettf Says:

    Hello David,

    What do you consider your skill level with arduino? Do you just need some pseudo code? or are you looking for a full sketch?

  20. Webuser Says:

    See this might add to answeres

    http://www.youtube.com/watch?v=Y2dkykxlXfI

  21. Webuser Says:

    pinMode(9, OUTPUT);

    void loop()
    {
    //create a pulse of 900us to position the servo to approx 30 deg
    digitalWrite (9, HIGH)
    delayMicroseconds (900)
    digitalWrite(9,LOW)
    delay(19)

    //create a pulse of 1300 to position the servo to approx 80 deg
    digitalWrite (9, HIGH)
    delayMicroseconds (1300)
    digitalWrite(9,LOW)
    delay(19)
    }

    Just play a little bit of the value to get the precise position or use the servo library with below sketch, a much straight forward approach for truly beginner.

    #include
    servo DavidServoName;//name your servo
    int pin=9; //the pin number where servo data wire is attached

    void setup()
    {
    DavidServoName.attached(pin);
    }

    void loop()
    {
    DavidServoName.write(30); //move servo arm to 30 deg
    delay(500); //delay the servo for half a sec before it move to 90
    DavidServoName.write(90); //move servo arm to 90 deg
    delay(500); //delay the servo for half a sec before it move to 30
    }

  22. Webuser Says:

    correction on 1st line should be

    #include

  23. Brettf Says:

    Can’t remember but I don’t think there was a servo library when i originally wrote this in 2007