What We're DoingThe Netduino's pins are great for directly controlling small electric items like LEDs. However, when dealing with larger items (like a toy motor or washing machine), an external transistor is required. A transistor is incredibly useful. It switches a lot of current using a much smaller current. A transistor has 3 pins. For a negative type (NPN) transistor, you connect your load to the collector and the emitter to ground. Then when a small current flows from base to the emitter, a much larger current will flow through the transistor and your motor will spin (this happens when we set our Netduino pin HIGH (true)). There are literally thousands of different types of transistors, allowing every situation to be perfectly matched. We have chosen a P2N2222AG a rather common general purpose transistor. The important factors in our case are that its maximum voltage (40v) and its maximum current (600 milliamp) are both high enough for our toy motor (full details can be found on its datasheet http://nedx.org/2222). (The 1N4001 diode is acting as a flyback diode for details on why its there visit: http://nedx.org/4001 )

namespace NCIR03 /// Define the namespace we are in ///
{
public class Program /// Define any variables used in the program and subrout //ines after Main()///
{
/// Setup Portion of Program, runs once at startup ///
//Empty because we define the pin type in the subroutines below

///
/// motorOnThenOff() - turns motor on then off
/// (notice this code is identical to the code we used for
/// the blinking LED)
///
static void motorOnThenOff()
{
OutputPort motor = new OutputPort(Pins.GPIO_PIN_D9, false); //Defines the outpu //t pin as pin 9 (operates as a digital pin)

int onTime = 2500; //the number of milliseconds for the motor to turn on for
int offTime = 1000; //the number of milliseconds for the motor to turn off for

motor.Dispose(); //Disposes of the motor pin variable(okay because we redefin //e it on every call)
}

/*
* motorAcceleration() - accelerates the motor to full speed then
* back down to zero
*/
static void motorAcceleration()
{
PWM motor = new PWM(Pins.GPIO_PIN_D9); //Defines the output pin as pin 9 (ope //rates as a PWM pin)

int delayTime = 50; //milliseconds between each speed step

//Accelerates the motor
for (uint i = 0; i
{
motor.SetDutyCycle(i); //sets the new speed
Thread.Sleep(delayTime); // waits for delayTime milliseconds
}

//Decelerates the motor
for (uint i = 100; i > 0; i--)
{ //goes through each speed from 255 to 0
motor.SetDutyCycle(i); //sets the new speed
Thread.Sleep(delayTime); // waits for delayTime milliseconds
}

motor.Dispose(); //Disposes of the motor pin variable (okay because we redefi //ne it on every call)
}
} /// Close the Program Loop ///
} /// Close the Namespace Loop ///

Not Working?(3 things to try)

Motor Not Spinning? Motor Not Spinning? If you sourced your own transistor, double check with the data sheet that the pinout is compatible with a P2N2222AG (many are reversed).

Still No Luck?If you sourced your own motor, double check that it will work with 5 volts and that it does not draw too much power.

Still Not Working? Sometimes the Netduino board will disconnect from the computer. Try un-plugging and then re-plugging it into your USB port.

Making it Better?

Controlling speed:We played with the Netduino's ability to control the brightness of an LED earlier now we will use the same feature to control the speed of our motor. The Netduino does this using something called Pulse Width Modulation (PWM). This relies on the Netduino's ability to operate really, really fast. Rather than directly controlling the voltage coming from the pin the Netduino will switch the pin on and off very quickly. In the computer world this is going from 0 to 3.3 volts many times a second, but in the human world we see it as a voltage. For example if the Netduino is PWM'ing at 50% we see the light dimmed 50% because our eyes are not quick enough to see it flashing on and off. The same feature works with transistors. Don't believe me? Try it out.

Change the code after the while (true) line to:// motorOnThenOff(); motorOnThenOffWithSpeed(); // motorAcceleration();Then debug the application. You can change the speeds by changing the variables onSpeed and offSpeed.

Accelerating and decelerating:Why stop at two speeds, why not accelerate and decelerate the motor. To do this simply change the code after the while (true) line to: // motorOnThenOff(); // motorOnThenOffWithSpeed(); motorAcceleration();

Then debug the application and watch as your motor slowly accelerates up to full speed then slows down again. If you would like to change the speed of acceleration change the variable delayTime (larger means a longer acceleration time).