Introduction: QuickStart - One Transistor DC Motor Controller

This is a PWM (Pulse Width Modulated) motor controller.
It uses a single small transistor to run a small DC motor.

While the circuit is very straight forward (some say boring?) it has a
pretty cool trick in the software - Pulse Width Modulated (PWM)
speed control - with speed ramp up and down.

It's only one transistor... I had to do something to make it more

The transistor (2N3904 NPN type) is there to handle the higher
current required by the motor.  (see schematic for connections).

I've added a couple of other toys to the simple transistor circuit to make it
easier to see what's going on here.

The RED LED is software controlled. It is turned on only while the motor is
accelerating (speeding up), and turned back off just before the motor starts
slowing down. 

The BLUE LED is connected across the transistor collector - emitter pins.
It turns on only when the transistor is turned OFF.  So it's basically showing
the off period between the motor drive pulses.

Or - connect the blue LED between the transistor's collector and V+
so you can actually SEE the EMF kickback voltage!

A short video clip of this demo in operation...

SPIN may look a bit strange at first, but it's an easy language to learn.

First of all, you need the Propeller Tool, which includes the editor, help
files, examples, common objects, and the Propeller Manual.
See the link below to grab that.

There are three loops in this little demo program.
The outer loop - Loop Forever.
And two inner loops that make the ramp pulse trains - ramp up and ramp down.

Ramp up starts off with very short pulse widths and increases the width of each
successive pulse in the train.

Ramp down does the opposite, shortening each subsequent pulse in the train.

The motor reacts as power is turned on, running faster as the power is left on
for a longer period. 

That's really the heart of all pulse-width modulation schemes

Propeller programming tool is free from Parallax at:

The program structure is determined by indentation. So be careful of that!
This example can be copied and pasted directly into the Prop Tool.


{Motor.spin    Richard Lamb - June 15, 2013 }
_XINFREQ =5_000_000

' define times
MSec   = _XINFREQ / 1_000
USec   = _XINFREQ / 1_000_000
Tlong = Usec * 600                                          
Tshort = Msec * 300                                  

' define IO pins
P_LED = 06
P_Motor = 07              
ON = 1
OFF = 0

' define Program constants
Tsteps   = 120                            ' number of steps per ramp

Long X

PUB  PWM_Motor_Demo

dira [P_LED] := 1                        '
dira [P_Motor] := 1                      ' 
outa [P_Motor] := 0                      ' start low

Repeat                                                         ' repeat forever
    Outa[P_LED] := 0                                   ' LED on while accelerating

    Repeat X from 1 to Tsteps step 1        ' Ramp up loop
        outa[P_Motor] := 1                               ' turn motor power ON    
        waitCNT ((X*Tlong) +cnt)                   ' longer delay each step makes a longer pulse
        outa[P_Motor] := 0                                ' turn motor power OFF  
        waitCNT ((Tsteps-X+1)*Tlong+cnt)  ' +1 to prevent 0 wrap the counter to 8 minutes.

  outa[P_Motor] := 1                                      ' short delay to lengthen the ON time.
  waitcnt(Tshort*5  +cnt)

  Outa[P_LED] := 1                                   ' LED off while decelerating

    Repeat X from 1 to Tsteps step 1     ' Ramp down loop    
        outa[P_Motor] := ON
        waitCNT ((Tsteps-X+1)*Tlong+cnt)  ' shorter delay each step makes a shorter pulse
        outa[P_Motor] :=  OFF                            
        waitCNT ((X*Tlong) +cnt)

  waitcnt (Tshort*5 +cnt)                               ' short delay while off off