Tuesday, June 17, 2014

Arduinos & Motors: Why Use A Diode?

[Tonight's post is by Ed Smith, participant in the Humboldt Microcontrollers Group]

As you go through various electronics tutorials you'll notice that almost every wiring diagram for electric motors has a diode across the motor terminals. Here's an example of what I'm talking about, from Jeremy Blum's excellent Arduino Tutorial series:
Diagram courtesy Jeremy Blum.

You'll notice that he has a capacitor across the terminals as well; we'll get to that.

Diodes are very simple, very crucial little bits of silicon. They allow electricity to flow in one direction, but not the other. If you wired the one in Blum's diagram backwards, something would explode the moment you turned the transistor on, as the diode would allow the electricity to bypass the motor and flow straight from input to GND (ground, return, earth, common, whatever you'd like to call it). Blam! Oriented the correct way, no current flows through it from VCC to GND.

The diode is in the circuit because electricity that is flowing tends to want to keep flowing, just like water. If your transistor (switch, MOSFET, whatever) is turned on, current is flowing and the motor is running and you suddenly switch off the transistor then the current tries to keep flowing, slams into the turned off switch and stacks up. If you've ever turned a garden hose off quickly and noticed how the hose jumped, or turned a faucet off quickly and heard pipes banging, this is roughly the same effect. The water has mass, and when it crashes into the valve/faucet it generates a pressure spike. We measure electrical pressure in voltage rather than the PSI (pounds per square inch) we use for water (and it's not the electrons' mass that causes issues; the actual cause is more complicated than I want to get into here), but the result is the same: it eventually causes damage. In your house it will knock the pipes loose and/or burst them. In this circuit it will destroy the transistor, likely leading to a direct short to GND, a motor that runs indefinitely, and a transistor that may end up on fire.

The diode allows that spike to flow back around to the input of the motor. Then the electricity can happily go in a circle like it wants to, without either slamming into the transistor and spiking the voltage or flowing to ground through the transistor and running the motor.

(Unrelated comment on the diagram: In the video in which the above circuit is shown, Jeremy says the resistor is used to isolate the transistor/motor from the Arduino; this is not actually the case. The resistor limits the amount of current the Arduino puts through the transistor when it is switching it on. Without this resistor the Arduino and possibly the transistor will die. Don't forget that resistor!)

A PropScope USB oscilloscope was used to get some shots of this effect. I used an Arduino putting out a 490Hz PWM (pulse-width modulation) signal to switch an IRLZ34N MOSFET (metal-oxide-semiconductor field-effect transistor), driving a small motor out of a cassette tape player. (The MOSFET is rated at 55 volts, so it can take a spike the level that I was generating without damage. It's also far, far, far overkill for driving the little motor I used.) In each set of pictures below, the first picture is at an ~16% duty cycle (on 16% of the time, off 84%), and the second is at a 50% duty cycle. Input voltage was ~5.1 volts from an ATX power supply. The red trace is the signal to the MOSFET (5V turns it on and allows the motor to run, 0V turns it off). The blue trace is the voltage after the motor just before the MOSFET.

Here's no diode, no capacitor:


That's a 15.9 volt spike; if we were using a 10 volt transistor we would have issues! Generally a 2X over-rated part is nice and safe, but not this time. Were we driving a 12V motor this spike would be a lot higher, of course.


At a higher duty cycle the spike is both lower and shorter duration, I don't know why. Still, 12.27 volts is a big jump over the five volt input! Please note that this spike is due to current flowing through the motor coils, not due to the physical rotation of the motor. Any electrical device with a coil of any kind in it (relays are a common one) will cause this sort of spike.

The diode used is a UF4007, rated for 1000 volts and 1 amp; again, just a bit overkill. Note that the diode rating is how much it can block before it breaks down and allows two way flow, not how much can safely flow through it. With the diode in place, per the oscilloscope trace below,


5.8 volts is a much more reasonable spike; that's only a 0.7V gain. The reason for this gain is the diode's "forward voltage", which is a measure of how much of a voltage drop the diode causes to the voltage going through it. The higher the current, the higher the drop. For example, this diode has a ~1.7V drop when its maximum rating of 1 amp is flowing through it. Note how long the spike lasts! The energy that would have gone into the spike is now flowing in a circle through the motor, and it takes the motor a while to use it.


This time the longer duty cycle didn't change the spike's voltage much. Instead the lower energy level manifests as a shorter duration spike. Also interesting is that in both cases the motor's RPM was higher with a diode than without. This is likely because without the diode the motor has a reverse voltage across it trying to turn it in the other direction. With a diode, that energy is trying to turn the motor in the same direction it's already rotating.

Now about the capacitor I mentioned earlier! In the video in which the diagram was shown, Blum just said that the diode and capacitor protect things from noise and spikes. The diode takes care of spikes, as we saw. This leaves the noise for the capacitor to address. In this situation, "noise" typically speaks of electromagnetic noise rather than audible noise. Brushed motors are very noisy electrically and electromagnetically speaking, as the brushes connect and disconnect there are small arcs that broadcast themselves nicely.

I didn't have a 1µF capacitor sitting around, so I used a 10µF. Everybody loves overkill right?
Here's what I got on the scope:


That's a bit different, isn't it? Also interesting is that the motor ran much faster at this PWM duty cycle than it did in the previous examples. Note that we never actually got up to 5V; that spike is long gone.


Less time, and even further from getting to 5V! The motor ran much faster, as well. The reasons behind this I'm not entirely sure of, but here's my best guess.

When the MOSFET (or transistor) turns on, the negative side of both the motor and the capacitor are quickly pulled to 0 volts. When the MOSFET turns off, the capacitor's negative/ground pin is still at 0V, and the capacitor still has a 5V charge in it. This drives the motor until that charge has equalized. The curve continues up and comes back down in both situations. I think that this is due to that same spike being partially dumped into the capacitor, charging further. I am far from sure on that. Also of note, the low duty cycles were audibly quieter as well as electrically quieter.

One thing to be aware of before this starts looking like a wonderful idea is that the stored energy in the capacitor (its internal charge is equal, but its 'ground' pin is still a volt or three above what the MOSFET considers to be ground) will be pulled violently to ground when the MOSFET turns on. With a 10µF capacitor at a couple of volts and a MOSFET rated for 30 amps at 55 volts this is not an issue, but with a larger capacitor and/or a smaller MOSFET, it could be.

In closing, any time you are switching a device that contains a coil of any sort (relays, motors, solenoids, inductors, coil guns, transformers, etc.) you need a diode to direct that voltage spike to somewhere safe. The capacitor is more optional, and you're better off using a smaller capacitor than a large one unless you know you are having problems with electrical noise that you cannot solve in any other way.

**********

No comments:

Post a Comment