Click here for a free subscription to "HDE" our site newsletter.

Servo Motor Drive Arduino Sketch

Arduino sketch for experimenting

Arduino Uno Arduino Uno

The sketch that I used to test the servo motor is shown below. It drives the MG90S servo from end to end in 1µs steps pausing for a second at both ends and once in the middle. The pause is to allow you to see what happens when the servo is being held in one position.

The sketch uses the Arduino "Servo" library and I tested it on an Arduino Uno.

The Arduino IDE

The MG90S servo has drive requirements that are typical of a whole range of servo motors so it's no surprise that Arduino has a library for it built right into the IDE. It's called "Servo". If you haven't already got the Arduino IDE set up then do it now using the download from here and then see the Getting Started page

You can now copy and paste the sketch above into the Arduino IDE, compile it and load it onto your Arduino board. I used an Arduino Uno because I had one but just about any Arduino would do.

How the Servo library works

Servo motors like the one we are using require 3 inputs. Ground, +5v power and a position demand signal input. It would be possible to generate a servo positioning signal by sending a pin high with the digitalWrite() function, use delay() for the timing then send it low again. Unfortunately the timing requirements are quite strict so the Arduino would have difficulty doing anything else at the same time. This is not going to be a good situation in real life applications.

The Servo library provides a way of doing all the pulse generation and timing using hardware timers on the chip and does not place undue constraints on your program. Once you set the library functions to drive the servo to a specific position you can leave it to do it's own thing and your sketch can get on with something else. Perfect.

Arduino C sketch description

In the above sketch access to the Servo library is gained by the #include <Servo.h> line. This includes the header file for the Servo library so that we can use all it's functions and features. The statement Servo Servo_1 creates an instance object of the Servo class.

I've wired it up to use pin 9 of the Arduino Uno as the Servo drive signal. Putting Servo_1.attach(9) in setup() associates pin 9 with the servo output.

The Servo library has functions for setting the position of the servo shaft in degrees but I prefer to set the length of the positioning pulse in µs instead because it should provide a greater resolution.

Servo_Max and Servo_Min should be set to the maximum and minimum pulse lengths in µs. You can adjust these values to suit your servo. The macro Servo_half_way is then arranged to compute the half way point.

The loop() function then takes over and executes the code within it in an infinite loop. Two for() loops ramp the servo pulse µs by µs first to the half way point where a delay() pauses it before the second for() loop takes it the rest of the way. After another delay() the servo is driven to the start point and execution returns to the top of the loop().

This sketch is meant to be played with to experiment with different ways of driving the servo so feel free to modify it yourself.

Now subscribe to our newsletter and don't miss a thing


No comments yet.

Add Comment

* Required information
(will not be published)
Bold Italic Underline Strike Superscript Subscript Code PHP Code Quote Insert line Bullet list Numeric list Link Email Image Video
Smile Sad Huh Laugh Mad Tongue Crying Grin Wink Scared Cool Sleep Blush Unsure Shocked
Enter code:
Notify me of new comments via email.
Remember my form details on this computer.
I have read and understand the privacy policy. *
I have read and agree to the terms and conditions. *
Click here for a free subscription to "HDE" our site newsletter.