If you're building a robot or other microcontrolled gadget, you will need to drive DC motors forwards and backwards. In this instructable, I'll demonstrate a simple and inexpensive circuit that controls a DC motor from two I/O pins. It requires no integrated circuits, and uses commonly available parts. I recommend you build it on a breadboard the first time. I designed this circuit, but I'm not the inventor of this type of motor controller. I got interested in motor control circuits like this one when I saw the amazingly precise movements of the Makerbots and CNC routers at Maker Works in Ann Arbor.
Step 1: Parts List
Here are the parts you'll need. All of them should be available at your local RadioShack or hobby store.
(1) DC motor
(4) MOSFET transistors. I used the IRF540N, but any N-channel MOSFET will do.
(2) NPN bipolar transistors. I used the BC548.
(2) PNP bipolar transistors. I used the BC327.
(4) 2200 ohm resistors (red-red-red)
(4) 10K ohm resistors (brown-black-orange)
Some jumper wires and a breadboard, if desired
The resistor values are not critical. Values that are fairly close will most likely work fine.
Step 2: The Finished Circuit
Here's a picture of the complete circuit on a breadboard, with some additional part labels.
Step 3: How To Use It
This circuit is designed to run a motor from the same power source as your microcontroller. Setting I/O pin 1 high makes the motor spin in one direction, and setting pin 2 high makes it spin in the other. Setting both pins low stops the motor, so speed control can be achieved through a PWM signal to a pin. I should also mention that setting both pins high at the same time shorts your battery, and should be avoided. I used a 12 volt power supply I made, but you could go as high or low as your transistors can handle. If you are driving very large motors, I recommend putting the MOSFETS on a heatsink or fan. Attached is a video of the circuit in action.
I'm connecting the transistor gates to positive by touching them with a jumper wire in this video, but they also are easily switched by two microcontroller I/O pins. I put a piece of red tape on the motor shaft to make it easier to see.
Step 4: How It works
When you set pin one high with your microcontroller, the NPN transistor Q7 switches on. This connects the base of the PNP transistor Q5 to ground, turning it on as well. Q5 then connects +12 volts to the mosfets Q1 and Q4, which connect the motor to positive and ground. Setting pin 2 high connects the motor to positive and ground in the opposite polarity. The four diodes protect your transistors from voltage surges that sometimes occur when a DC motor is abruptly stopped. The 10K ohm resistors pull the bases of the transistors to ground when your I/O pin goes low, and the 2200 ohm resistors limit the current that can be drawn from your I/O pins to protect them. Have fun spinning motors! I used two of this circuit for the drive train of my robot butler.