Driving a DC motor forwards and backwards with a PIC

dc motorh-bridgemicrocontrollermotorpic

Say I have a DC motor which runs clockwise when current flows in one direction, and counter-clockwise when the current flow is reversed.

I have 2 lines going in my circuit, +5V and ground. I have a PIC, and two motors.

I want to be able to control the direction each of the motors spin without any mechanical relays.

Say pin RA0 is motor 1 forwards, RA1 is motor 1 backwards, RA2 is motor 2 forwards, and RA3 is motor 2 backwards.

What is a good way to accomplish this? Should I build an H-bridge out of transistors?

Best Answer

One H bridge per motor is the normal way to do this. You then turn on opposite corners of the H bridge, one pair for clockwise and the other pair for counterclockwise. If the motor is also to be powered from 5V, then the high side switches could be just PNP transistors with a base resistor each.

The easiest way to do this is to use 4 PIC pins to drive the H bridge. It is important to do break before make when switching direction, so you don't want to just tie the two base (or gate) signals together for each side, especially since bipolar transistors will take longer to turn off from saturation than to turn on.

Don't forget the two reverse diodes to power and ground on each side of the motor, else the transistors could get destroyed due to inductive kickback.