.:A Single Servo:.
(NEDX) .:Netduino Experimentation Kit:. (NEDX)

What We're Doing
Spinning a motor is good fun but when it comes to projects where motion control is required they tend to leave us wanting more. The answer? Hobby servos. They are mass produced, widely available and cost anything from a couple of dollars to hundreds. Inside is a small gearbox (to make the movement more powerful) and some electronics (to make it easier to control). A standard servo is position-able from 0 to 180 degrees. Positioning is controlled through a timed pulse, between 1.25 milliseconds (0 degrees) and 1.75 milliseconds (180 degrees) (1.5 milliseconds for 90 degrees). Timing varies between manufacturer. If the pulse is sent every 25-50 milliseconds the servo will run smoothly. To achieve this on the Netduino we will use its PWM function.

The Circuit
The Parts
Breadboard Sheet
2 Pin Header
3 Pin Header
Mini Servo



breadboard layout sheet


Code (no need to type everything in just)
Download the Code from ( )
(and then copy the text and paste it into an empty Netduino Sketch)

//Libraries that our program uses
using System;                               //use base classes                            
using System.Threading;                     //use threading classes        
using Microsoft.SPOT;                       //use smart personal objects technology classes
using Microsoft.SPOT.Hardware;              //use hardware related SPOT classes
using SecretLabs.NETMF.Hardware;            //use Secret Labs hardware framework
using SecretLabs.NETMF.Hardware.Netduino;   //use the Netduino specific classes

namespace NCIR04                    /// Define the namespace we are in ///
    public class Program            /// Define any variables used in the program and subrout
                    //ines after Main()///
        public static void Main()   /// The Main loop (run at power up) ///
                                    /// Setup Portion of Program, runs once at startup ///
            PWM servo = new PWM(Pins.GPIO_PIN_D9);  //creates a new PWM object the servo is 
                    //connected to

            while (true)            /// Do Forever ///
                for (int pos = 0; pos < 180; pos++)     // goes from 0 degrees to 180 degree
                                                        // in steps of 1 degree  
                    setServo(servo, pos);               // tell servo to go to position in v
                    //ariable 'pos'        

                for (int pos = 180; pos > 0; pos--)           // goes from 180 degrees to 0 
                    setServo(servo, pos);                 // tell servo to go to position in
                    // variable 'pos'        

        /// Sets a servo to the desired angle

        /// A PWM object the servo is connected to
        /// An angle between 0 and 180 degrees
       public static void setServo(PWM servo, int position)
           if (position > 180) position = 180;  //ensuring the desired angle is less than 18
                    //0 degrees
           if (position < 0) position = 0;      //ensuring the desired angle is greater than
                    // 0 degrees

           int min = 500;         //pulse length for 0 degrees
           int max = 2500;        //pulse length for 180 degrees
           int range = max - min; //range between min and max pulse length

           float pulse = ((float)max - ((((float)180 - (float)position) / (float)180) * (flo
                    //at)range)); //Calculating the pulse length
           servo.SetPulse(20000, (uint)pulse);  //pulsing the pin for the desired amount of 
    }                               /// Close the Program Loop ///
}                                   /// Close the Namespace Loop ///


Not Working? (3 things to try)

Servo Not Twisting?
Even with colored wires it is still shockingly easy to plug a servo in backwards. This might be the case.
Still Not Working
A mistake we made a time or two was simply forgetting to connect the power (red and brown wires) to +5 volts and ground.
Fits and Starts
If the servo begins moving then twitches, and there's a flashing light on your Netduino board, the power supply you are using is not quite up to the challenge. Using a fresh battery instead of USB should solve this problem.

Making it Better?
Changing the Sweep Speed:
Not happy with the speed the servo is moving? You can change this by altering the code. Simply change:

for (int pos = 0; pos < 180; pos++)
for (int pos = 0; pos < 180; pos = pos + #)
for (int pos = 0; pos > 180; pos--)
for (int pos = 0; pos > 180; pos = pos - #)

# = degrees per step.
Send to a Specific Angle:
To get the servo to go to a specific angle rather than sweep, change the code after while (true) to:

setServo(servo, ###);

### = angle between 0 & 180 degrees

Mixing and Matching:

Each circuit in this guide is rather basic. The real fun comes when you start using multiple components and real world items to make something neat. The controlled movement of a servo is very helpful in this, so get thinking and creating.