NCIR
03
.:Spin Motor Spin:.
.:Transistor & Motor:.
(NEDX) .:Netduino Experimentation Kit:. (NEDX)

What We're Doing
The 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 )


The Circuit
The Parts
NCIR-03
Breadboard Sheet
x1
2 Pin Header
x4
Transistor
P2N2222AG (TO92)
x1
Wire
Toy Motor
x1
Diode
(1N4001)
x1
10k Ohm Resistor
Brown-Black-Orange
x1


Schematic

 

Resources
.:download:.
breadboard layout sheet
http://nedx.org/NBLS03

 

Code (no need to type everything in just)
Download the Code from ( http://nedx.org/CODE03 )
(then simply 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 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

        public static void Main()   /// The Main loop (run at power up) ///
        {
            while (true)            /// Do Forever ///
            {
                motorOnThenOff();           
                //motorOnThenOffWithSpeed();
                //motorAcceleration();
            }
        }

        ///
        /// 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.Write(true);       // turns the motor On
            Thread.Sleep(onTime);    // waits for onTime milliseconds
            motor.Write(false);      // turns the motor Off
            Thread.Sleep(offTime);   // waits for offTime milliseconds

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

        /// 
        /// motorOnThenOffWithSpeed() - turns motor on then off but uses speed values as wel
                    //l 
        /// 

        static void motorOnThenOffWithSpeed()
        {
            PWM motor = new PWM(Pins.GPIO_PIN_D9);    //Defines the output pin as pin 9 (ope
                    //rates as a PWM pin)

            uint onSpeed = 100;  // a number between 0 (stopped) and 100 (full speed) 
            int onTime = 2500;  //the number of milliseconds for the motor to turn on for

            uint offSpeed = 50;  // a number between 0 (stopped) and 100 (full speed) 
            int offTime = 1000; //the number of milliseconds for the motor to turn off for

            motor.SetDutyCycle(onSpeed);        // turns the motor On
            Thread.Sleep(onTime);               // waits for onTime milliseconds
            motor.SetDutyCycle(offSpeed);       // turns the motor Off
            Thread.Sleep(offTime);              // waits for offTime milliseconds

            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 < 100; 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).