Mini-Mapper 2: Motors and Motor Drivers
This second entry is a short article covering some background information about motors and motor drivers that we need to know before we decide on a driver to use.
(For things like this, where there are lots of sources available online and in books, I'll just post short summaries of the information that's most relevant to the Mini-Mapper project.)
What is a motor?
First, fundamental question: what exactly is a motor? It's a machine for turning electrical energy into mechanical energy, specifically (normally) using magnets.
We'll just talk about DC motors here. AC motors are a little different. Suppose we take two curved permanent magnets and arrange them as shown in the outside of the first figure below. (The other poles of each of the two magnets shown aren't shown because they're not relevant to us here.) We'll call these two stationary magnets the stator.
In the centre of the stator, we have an axis (marked with a cross) attached to which is another magnet, which is free to turn: we call it the rotor.
In the first figure, the North pole of the rotor is attracted by the South pole of the stator (on the left) and the South pole of the rotor is attracted by the North pole of the stator (on the right). The result is that the rotor turns clockwise. This rotation continues in the second figure.
If we left things as they are in the second figure, the rotor would eventually come to a stop in a horizontal position where the rotor is oriented symmetrically with respect to the stator. (There might be some wobbling around until the rotor settled down, but it would eventually stop moving.)
But what if we could "switch off" the magnetism of the rotor before we reached that point, as shown in the first figure below? Then the rotor would just coast around by its own inertia, passing through the symmetrical stopping point. If we could now switch the rotor magnet back on, but with the poles reversed compared to how we started (the second figure below), then the rotation would continue with the new rotor North pole being attracted to the stator South pole and the new rotor South pole being attracted to the stator North pole.
Critically, that switching around of poles on the rotor allows the rotation of the rotor to continue in a clockwise direction.
In a real motor, that switching of direction is managed using an electromagnet: direct current one direction through the coils of the electromagnet and you get one orientation of the poles on the rotor; send current through the coils in the opposite direction and get the opposite orientation of the rotor poles. The process of switching the direction of the magnetic poles of the rotor is called commutation.
This model of a motor is a simplification of what you would see if you opened up a real motor: the stator would be made of more than two magnets, the rotor might have multiple coils, and so on, but the general principle of operation would be the same.
The key point about commutation is that the switching of the current in the coils of the rotor has to be coordinated with the rotation of the rotor on its axis in the static magnetic field of the stator. There are two approaches to managing this coordination. First, the switching of currents through the coils of the rotor can be done electronically. This is done in brushless DC motors. The control needed for this switching can be quite complicated. (You often see these as the motors for cooling fans in PCs. Here, the switching is frequently done in the stator, and the rotor carries the permanent magnets.)
Second, commutation can be done mechanically. This is the approach used in brushed DC motors, which tend to be among the cheapest and simplest to drive of all motors, and are what we're going to use.
DC brushed motors
A brushed DC motor commutates the current to its rotor coil using a split ring that rotates with the rotor and a set of static brushes that rub against the halves of the split ring to make electrical contact to transfer current to the rotor coil. The figure below shows a schematic representation of this setup, highlighting how the DC current fed to the motor via the brushes is turned into an alternating current through the rotor coil, coordinating the alternation of direction of magnetic field in the rotor coil with the rotation of the motor's rotor on its axis.
This is electrically simple (good for us, since it makes these things much easier to drive than brushless motors), but it has the disadvantage of introducing a source of friction and mechanical wear, as the brushes need to rub against the split ring of the commutator. The brushes are often made of a soft carbon compound, sometimes with additions like copper to improve conductivity, in order to avoid sparking or scoring of the split ring. Larger brushed motors usually have more than two brushes and two elements to their split ring commutator, but the principle is the same as our simple example.
H-bridge motor driver
To make a brushed DC motor go, we need to feed DC current through it. Current in one direction will make the motor rotate in one direction, current in the other direction makes it rotate the opposite way. If we want to control a motor using a microcontroller (as we do), we're going to need some transistors to control the current. For one thing, we're definitely going to need more current than the microcontroller outputs can safely source. And for another thing, the coil of the motor is an inductive load that's going to generate all sorts of pulses of current as it starts and stops, and we don't want to expose the microcontroller's GPIOs to that.
So we use a motor driver. The usual arrangement for driving a brushed DC motor uses something called a H-bridge, which is four transistors arranged like this:
The motor M1 is in the centre, MOSFETs Q1 and Q3 are P-channel enhancement devices connected between the positive power rail and opposite sides of the motor, and MOSFETs Q2 and Q4 are N-channel enhancement devices connected between ground and opposite sides of the motor. To switch Q2 or Q4 on, the gate of the transistors has to be taken high, i.e. inputs IN2 or IN4 need to be driven high. To switch Q1 or Q3 on, the gate of the transistors has to be driven low (they're P-channel FETs!), so the inputs IN1 or IN3 need to be driven high. (The inverters on inputs IN1 and IN3 are there just to make all the inputs active high.)
We're using a mixture of N- and P-channel transistors here for simplicity, but integrated motor drivers often just use N-channel MOSFETs for all four transistors. That means you need some extra circuitry to be able to drive the gates of the high-side transistors Q2 and Q4 high enough to switch the transistors on.
In order to make the motor turn, we need to provide a path through the motor coils from the positive supply to ground. Suppose that we want to make the motor turn clockwise, and to do that we need to drive current through the motor from the positive to the negative terminal as marked on the schematic:
That means that we need to switch on transitors Q1 and Q4, and switch off transistors Q2 and Q3, i.e. we need to drive inputs IN1 and IN4 high and inputs IN2 and IN3 low:
To make the motor turn counterclockwise, we need to drive current through the motor in the opposite direction. To do that, we need to switch on transistors Q2 and Q3, and switch off transistors Q1 and Q4, i.e. we need to drive inputs IN1 and IN4 high and inputs IN2 and IN3 low:
In both of the "forward" and "backward" drive modes, we can control the motor speed by keeping one FET switched on, and driving the other with a PWM signal. The average current through the motor is then proportional to the PWM signal's duty cycle, and since the torque generated by a brushed DC motor is proportional to the current through its rotor coils, this gives us a mean to modulate motor speed and torque.
You can do some other things with a H-bridge. On the basic kind of H-bridge I've drawn here, switching off all four transistors (first image below) allows the motor to "coast", where it will run down just under friction. When using an integrated driver, things are often a bit different here: most integrated drivers have "freewheeling" diodes next to their MOSFETs that provide a route from ground to the positive rail for inductive currents from the motor coil to dissipate. Switching off all the FETs in the H-bridge causes currents to flow through these diodes, which leads to a "fast braking" of the motor.
Switching on both low-side FETs (Q2 and Q4) and switching off both high-side FETs (Q1 and Q3) also provides a route for inductive currents in the motor coil to decay, with a time constant determined by the on resistance of the Q1 and Q3 FETs (second image below). This is slower than the decay through the freewheeling diodes, so this mode is often called "slow braking".
One thing you don't want to do with a H-bridge is to switch both FETs on one side of the bridge on at once. That gives a route from the positive rail to ground through nothing but the on resistance of the FETs on that side. That will probably lead to currents high enough to destroy the FETs:
Integrated motor drivers
Fortunately, we don't need to build a H-bridge ourselves, since there are integrated solutions. And reading the datasheets and application notes that go with those integrated solutions is a good way to learn more about how these things work. Integrated motor drivers usually have a control interface that makes it impossible to get into the "bad" state pictured above, and that helps with sequencing of activation of the transistors in the H-bridge for PWM control.
Some drivers provide a straightforward PWM control interface while others do the PWM modulation themselves to maintain constant current conditions in the motor coils. For our purposes, I think I'd like to do the PWM control myself, so I'll look for a driver that allows that.
In addition, integrated drivers provide various protection mechanisms to avoid damage to both motors and drivers.
There are a few manufacturers that produce a range of these devices, but I'll probably go with a driver from TI, since they have a range of simple drivers for small low-cost applications like ours.
So I'm going to do some datasheet reading and component hunting, and will report back in the next part of this.