This example assumes you’re using a DC motor that runs on low voltage DC, in the 5-15V range. Connect leads to its terminals, and run if from a benchtop power supply if you have one. Try changing the voltage on it, and seeing what effect it has. Don’t go over the motor’s rated voltage. Connect a switch in series with the motor and use it to turn on the motor.
Connect the base of a TIP120 transistor to one pin of your microcontroller. Connect the motor to the transistor as follows:
Note the second power supply. Most motors take a great deal more current than a microprocessor, and need their own supply. The example below uses a 9V battery as a separate power source. Whatever motor you use, make sure the power source is compatible (i.e. don’t use a 9V battery for a 3V motor!). Although the example shown is on a BX-24, you can use it with any microcontroller. Just connect the 1K resistor to the output pin of your microcontroller.
Note that we’ve added two capacitors on either side of our regulator. They smooth out the power, as the motor will cause spikes and dips when it turns on and off.
Here’s the schematic for the capacitors and the regulator:
The simplest program for this would be as follows (on the BX-24):
Sub main() call delay(0.5) ' start program with a half-second delay do call putPin(13,1) call delay(1.0) call putPin(13,0) call delay(0.5) loop end sub
In PicBasic Pro, the code would be as follows:
pause 500 ' start program with a half-second delay main: high PORTD.2 pause 1000 low PORTD.2 pause 1000 goto main
For pBasic and mBasic, use the PicBasic Pro example and change the pin names.
If your power supply for the microcontroller is compatible with your motor, you can wire the motor supply in parallel with the 5V regulator. For example, I use a 12V DC 1000 mA power adaptor, so I can use a 12V motor, if the power from the motor is wired in parallel with the 5V regulator’s input, like so:
Note that the motor and the BX24 need a common ground (in our case, they get it through the transistor’s base; see above schematic).
A motor controlled like this can only be turned in one direction. To be able to reverse the direction of the motor, an H-bridge circuit is required. For more on controlling DC motors with H-bridges, see the notes on DC motor control. Then get an H-bridge such as the Texas Instruments SN754410, or the L293 (which is identical to the TI chip), or make your own. Use it to control the direction of a motor.