Posted on by & filed under Arduino Tutorials, Electronics, Schools & Curriculum, Software and Apps.

Lessons Menu:

Lesson 5 Hardware:

  1. Computer / Laptop or Netbook
  2. Arduino Microcontroller
  3. USB to Serial Adapter (if your microcontroller does not have a USB port)
  4. Appropriate USB cable (Arduino boards draw power from the USB port – no batteries yet)
  5. Standard servo motor (current consumption <50mA)
  6. Pin headers / cables

Controlling a servo motor directly from the Arduino is quite easy. However, a servo motor may require significantly more current than the Arduino can provide. The following example uses a standard sized servo (without any load) powered directly from the Arduino via USB. When powering the servo directly from the Arduino board:

  1. Connect the black wire from the servo to the GND pin on the Arduino
  2. Connect the red wire from the servo to the +5V pin on the Arduino
  3. Connect the yellow or white wire from the servo to a digital pin on the Arduino

Arduino 5 Minute Tutorials

Alternatively, you can plug the servo’s wire into three adjacent pins, and set the pin connected to the red lead to “HIGH” and the pin connected to the black lead to “LOW”. If you want to use a more powerful servo, or if you want to connect it to a separate power supply, you would connect the battery / power supply’s red (5V) and black (GND) wires to the servo’s red and black wires, and connect the signal wire to the Arduino. Note that you also need to connect the batter’s GND line to the Arduino’s GND pins (“common ground”).

Arduino 5 Minute Tutorials

pinMode(pin number, OUTPUT);

This sets a pin number as dedicated input or output. In this case, we called the pin “servopin” and assigned it a value of 4. The term “pulse” is in black as it is not a reserved word and can be changed by the user. It is best to use descriptive variables when coding to understand what each does, or the information it will contain. Servos operate by sending a timed +5V pulse (usually between 500us and 2500us) to the onboard electronics, which is repeated every ~20ms. This pulse corresponds to a servo position, usually from 0 to 180 degrees.

  • 5V for 500 microseconds = 0.5 milliseconds and corresponds to 0 degrees
  • 5V for 1500 microseconds = 1.5 milliseconds and corresponds to 90 degrees
  • 5V for 2500 microseconds = 2.5 milliseconds and corresponds to 180 degrees
  • The relationship is linear, so use mathematics to determine the pulse which corresponds to a given angle. Note that if you send a signal that is greater or lower than the servo can accept (for example, Firgelli linear actuators accept 1 to 2 ms), you might damage the actuator.

Another option for controlling servos is to use the Arduino “servo library” (previously separate from the basic Arduino software, it is now included with V1.0). The servo library manages much of the overhead and includes new, custom commands. If you want to control multiple servo motors, you should use a servo motor controller and a separate power supply between 4.8V to 6V.


20 Responses to “Arduino 5 Minute Tutorials: Lesson 5 – Servo Motors”

  1. Scouty

    After some math I found out the type of time-angle: 90*t-45=a. Where t is time in milliseconds and a is angle in degrees.

  2. Joseph

    i just did exactly what was said. but the servo motor makes no move, apart from an initial move it makes when i plug in the micro controller. i dont know what the problem is

    • Coleman Benson

      @Joseph, are you using the servo library? If so, that automatically takes care of updating the servo position every ~30ms. It sounds like your servo’s position is not being updated. Double-check your connections as well and ensure there is no load on the servo.

  3. Mike

    I was planning on making an auto pet feeder with a servo that cycles once every 24hrs. Can I just change the
    To a higher value

  4. Mike

    Also is this a 360 servo or 180 servo motor that this code is used for?

    • Coleman Benson

      @Mike There is quite a lot of discussion on the Arduino forum regarding delays, as well as the use of real time clocks if it needs to be accurate. The blog comments are not the best place for technical support, so we invite you to create a new topic on the RobotShop Forum.

  5. Mike

    Thank you for your help so far, it’s helps me with my project ideas even more

  6. Abdo Salem

    It’s okay for analog servo motor, but for digital servo motor this program can not work with Arduino.
    I use rds3115mg motor. plz any help to make it work.

  7. mehran

    Can I connect my servo directly with arduino?
    won’t it damaged the controller?
    and can arduino provide a sufficent current to drive a motor?

    • Coleman Benson

      @mehran If the servo consumes <40mA, you can power it using the digital pins. This is the case with low torque, standard sized servos such as Hitec’s 322. However, any more current than this and you will need to power it separately. You can still connect the signal pin to the Arduino and power it separately.

  8. Alina

    I’ve a hitec servo motor. What I want to do is move the servo shaft from position 0 degrees to position 90 degress and make it stop. I dont want that loop to be repeating. How would I do that?

  9. megh

    Hey i am making a humanoid bot and i want to make it walk so please can u help me witj a program i have six servos on my legs on the bot
    Ankle joint z axis and knee and pivot joints x axis .please help.

    • Coleman Benson

      @megh Building and programming a humanoid robot surpasses the context of this blog post, so feel free to create a new topic on the RobotShop Forum and we would be happy to help. It would be important to know what products you are using.

Leave a Reply

  • (will not be published)