Making a FET "H" bridge (revised)
One thing most robots need is the ability to control motors. This circuit will work from 7.2V to 15V with PWM frequencies up to 25KHz and drive motors with a stall current of 20A. Features include not only speed and direction but it also has electronic braking, fuse protection and current sensing. Click on the schematic for a larger image. I have now attached some files with information on current shunts and heatsink calculations.
This circuit was my own exercise to teach myself about "H" bridges. It cannot work at high PWM frequencies, it works perfectly with Arduino's default PWM frequencies. In most cases I suggest you buy a ready made "H" bridge.
As this schematic has been updated you may need to reset your browser cache to see the new schematic. For Firefox users this is done by pressing [Ctrl] + [F5]. This circuit is now smoke proof.
I finally had time to build and test this circuit thoroughly using a DSO (Digital Sampling Oscilloscope) to check that it all worked properly. I have replaced the LM324 with an LM339. The outputs of the LM339 change a lot faster than the LM324 making it better suited for higher frequencies but they are open collector outputs so they need a pullup resistor to work properly.
The current sensing part of the circuit was discussed in detail in a previous walkthrough where it was suggested that a fuse could be used as the "Shunt resistor". I have used that in this circuit and included a LED to indicate if the fuse has blown. Although not as precise as a proper Shunt resistor the fuse is a lot cheaper and performs a vital service. I have added a 200K pot for adjusting the gain allowing this circuit to work with any shunt resistor.
The fuse should be a "slow blow" fuse with a rating less than your stall current. If your software is working well it will detect the stall current and shutdown the motors before the fuse blows. If something goes wrong then the fuse will do it's job.
Note that suitably heavy wire should be used between the batteries, the motor controller and the motor to get maximum power to your motors and to ensure the fuse does it's job. With the 20A circuit shown I would use at least 1.5mm² stranded copper wire.
I have shown IRF540 Nch and IRF9540 Pch FETs in this circuit as used by Jip in his robot "Tiny Tim" because they are relatively cheap and will handle stall currents of 20A. Other FETs could be used in their place depending on the requirements of your robot.
The maximum Vgs of the FETs shown is +/- 20V. I recommend not using batteries higher than 14.4V (12 x NiMh) as the peak voltage of the batteries during charging and just after charging can be a fair bit higher.
No "Flyback" diodes have been included in this circuit because in most cases the FET's internal "Body Diode" is usually sufficient. Additional diodes may be added if required.
The comparators perform 2 functions, they translate the logic level outputs of your microcontroller to the higher voltages needed to drive the FETs and they prevent your FETs from aciddently turning on in such a way as to short out your battery.
The voltage translation is done by comparing your logic level inputs with voltages derived from your 5V supply. The input is compared to 1V and 4V levels generated with a voltage divider. If your microcontrollers output is higher than 4V then the appropriate Nch FET will turn on. Lower than 1V will turn on the Pch FET. Voltages in between 1V and 4V will turn off both FETs preventing any possible Short circuit.
Because the outputs of your microcontroller can change state so quickly I have added 10nF capacitors and 100Ω resistors in series with inputs A and B. As the capacitors must charge and discharge via the 100Ω resistors this slows down the inputs rate of change enough to guarantee that both Nch and Pch FETs have time to turn off during transistions.
Below is a screen shot from the DSO. This shows the LM339 outputs to the Nch and Pch FETs when input A is PWM at 50% duty cycle @ 16KHz with a battery voltage of 15V. Input B is tied high. As shown on the truth table this is alternating between 50% brake and 50% reverse. The yellow signal goes to the Nch FET while the Cyan signal goes to the Pch FET.
When both signals are high, the Nch FET is on and the Pch FET is off. When both signals are low then the Nch is off and the Pch is on. During switching, you can see the Nch turns off before the Pch turns on and that the Pch turns off before the Nch turns on.
Each input has a diode and transistor on it. This strange setup is known as MML (Mickey Mouse Logic) which is basically logic gates cobbled together from resistors, diodes and transistors.This MML generates open circuit outputs when both inputs are low but allows all other combinations to pass through allowing both "brake" and "no power" states as shown in the truth table.
When both inputs are low then the diodes block the inputs and the + inputs of the opamps are held at 2.5V via the 4K7 resistors. In this state the transistors on the inputs are both off.
When both inputs are high then the + inputs of the opamps are driven high via the diodes turning the PCh FETs off and the Nch FETs on causing the motor to brake. Both input transistors are off since their base and emitter pins are both high.
When one input is high and one is low then the high input passes through it's diode and the low input passes through it's transistor which is now on.
Speed is controlled by holding one input low and PWM the other input. Electronic braking of the motor can be controlled by PWM both inputs together so that they are both high or both low at the same time.
Although 5V logic is assumed in this circuit it will work with 3.3V logic just as easily. Just replace the 4.7V zener in the current sensing circuit with a 3.3V zener to protect the LM358 input.