Driving a motor

A board pin cannot run a motor. One transistor will switch one; an L293D will turn one round. What each costs you, and why.

A small gearbox motor wants a few hundred milliamps standing still. An Arduino pin gives about twenty. Wire the motor straight to a pin in Mokxi and you will see it barely twitch, for exactly the reason it barely twitches on the bench, so the circuit that makes it turn is the lesson, and there are two of them.

The motor itself

DC motor is the yellow gearbox kind: two terminals and nothing else. The model is the standard brushed-motor one, with the mechanics taken as a single lag:

  • the armature is resistance ohms of winding, 6 by default, which is what a small motor measures across its terminals standing still;
  • a spinning motor makes a back EMF that opposes the current, so one up to speed draws a fraction of what a stalled one does;
  • the current through the winding is the torque, so the speed chases the applied voltage with a first-order lag of inertia milliseconds.

That matters because the motor presents itself to your driver as the winding in series with that EMF. Stalled it is 6 ohms and pulls most of an amp off a 6 V supply; spinning it pulls a tenth of that. A driver stage sees the difference, and so does the analog island around it.

The shaft on the canvas turns at the speed the model says, and the direction it turns is the direction the current runs.

One transistor: on and off

The simplest driver, and the one every kit starts with.

  pin 9 ──[ 1 k ]── base
                    2N2222   collector ── motor ── 5 V
                             emitter   ── GND
                    and a 1N4007 across the motor, band to 5 V

A little base current through the 1 k lets a much larger collector current through, and a PWM on the pin switches it hundreds of times a second while the motor's own inertia averages it into a speed.

The diode is the part people leave out. A motor is a coil, and the instant its current is cut the coil tries to keep it flowing and the collector flies up until something breaks down, usually the transistor. The diode gives that current a way around: reverse biased while the motor runs, conducting only on the kick.

What this cannot do is reverse. A transistor only knows how to pull one end of the motor down, and the other end is bolted to the supply.

See it: Motor on a transistor.

An L293D: both ways

Turning a motor round means putting either end at the supply and the other at ground, which takes four switches arranged in an H. The L293D has eight of them (four half bridges) in a sixteen-pin package, so one chip drives two motors or one motor and has a pair spare.

Each half bridge has an input, an output and a share of an enable:

enable input output
low either nothing at all: the motor coasts
high high the motor supply, less the drop
high low ground, plus the drop

Wire OUT1 and OUT2 to the motor's two terminals and IN1 and IN2 are the direction:

IN1 IN2 what happens
1 0 forward
0 1 reverse
0 0 brake
1 1 brake

Brake and coast are not the same thing and the difference is worth watching. Both inputs the same shorts the motor's own back EMF through the chip and it stops in a fraction of a second. Taking the enable low lifts both ends off it and it winds down for seconds. Open Motor both ways and watch the shaft do each in turn.

Two supplies

VSS (pin 16) runs the logic and wants 5 V. VS (pin 8) is the motor rail and will take anything from 4.5 V up to 36. The board's 5 V and a separate battery are both ordinary, and a separate battery is usually the right answer.

With VSS missing nothing happens at all. With VS missing the logic still decodes and the outputs still switch, to nothing, which is the "my motor does not turn and the chip is not even warm" fault.

The four pins in the middle of the package are one piece of metal (the heat sink), so grounding any one of them grounds the lot, and Mokxi straps them together the way the package does.

The volts it keeps

This is the number people are surprised by, so it is worth saying plainly. The output stages are Darlingtons, and a Darlington does not saturate: the datasheet gives about 1.2 V typical dropped in the high side and the same again in the low side. A motor across a 5 V VS therefore sees about 2.6 V and runs at about half the speed you were expecting.

That is not the model being coy. It is on the datasheet, it is why an L293D gets warm, and it is why anything that matters uses a MOSFET bridge instead. drop is a property, so you can set it to 0 and see what a perfect bridge would have done.

And the D

The D on the end is eight clamp diodes inside the package. An L293 without it needs eight external ones; an L293D needs none, which is why the Motor both ways circuit has no flyback diode in it while the transistor one does.

Nothing in Mokxi needs those diodes, because the DC motor model has the winding's resistance and its back EMF but not its inductance, so there is no kick to clamp. That is the model's limitation rather than the chip's: a circuit that would have destroyed a bare L293 on the bench will run quite happily here.

What is not modeled, either way

The chip's 800 ns propagation delay and the switching speed its Darlingtons impose, the 600 mA per channel limit and the thermal shutdown behind it, and paralleling two channels for more current. On the motor: the mechanical load (there is nothing on the shaft), brush noise, and stiction, so a very small voltage turns it very slowly rather than not at all.

The 1.2 V Darlington drop is a constant. On a real L293D it is a function of the current through the output (the datasheet's figure is quoted at 0.6 A and it is smaller at a tenth of that), so a lightly loaded bridge loses less than this model says. The output resistance is a flat 0.6 ohm on top.

The chip's supply limits are the datasheet's. VSS has to be at least 4.5 V and so does VS: below that the outputs are dead, so a bridge run off two AA cells turns nothing here and turns nothing on the bench. Above 36 V (an absolute maximum rating, not a recommendation) the outputs go dead too, because a real chip past it is destroyed and nothing in Mokxi lets out smoke. There is no supply current, no quiescent draw and no package temperature.

See it: Motor on a transistor, Motor both ways.