Articles and Tutorials
Designing Embedded Hardware
For more information,
Motor Control and PWM
Using a DAC may seem the obvious way to generate an analog output voltage, but there is another way, using nothing more than a digital I/O line configured as an output. This technique is known as Pulse Width Modulation, or PWM.
Consider the average, garden-variety, square wave shown below:
The width of the high is equal to the width of the low, so this wave is said to have a 50% duty cycle. In other words, it is high for exactly half the cycle. Now, if the amplitude of this square wave is 5 V for example, the average voltage over the cycle is 2.5 V. It is as though we had a constant voltage of 2.5 V. Now consider this square wave:
This wave has a 10% duty cycle, which means that the average voltage over the cycle is 0.5 V.
A low-pass (averaging) filter on the PWM output will convert the pulses to an analog voltage, proportional to the duty cycle of the PWM signal. By varying the duty cycle, we can vary the analog voltage. Hey presto! - we have digital to analog conversion without a DAC. That’s the basic idea behind PWM.
One of the fun things you can do with an embedded computer is getting it to actually move something, whether it be an external system, or the embedded computer itself. The possible applications could range from controlling locomotives on your model railroad layout to experiments in robotics, and anything in between. A note of caution though: if your hardware and software are responsible for moving a physical object, then a bug can easily cause physical damage too. So be careful.
Let’s say that we have an electric motor than operates from a 12 V supply. Applying 12 V across the motor will cause it to turn at full speed. Similarly, by applying 6 V, we can get the motor spinning at half speed. By varying the applied voltage, we can vary the speed at which the motor turns. The speed of the motor is proportional to the output voltage. However, this technique has a major drawback. For very low speed operation, the required output voltage may be too low to actually cause the motor to turn. A better way is to use PWM, generated and controlled by a digital system.
Consider a PWM signal with an amplitude of 12V. With a 10% duty cycle, the effective analog voltage of that PWM signal is 1.2 V. Now, by itself, 1.2 V may not be enough to turn a motor. But, we’re not using 1.2 V across the motor, we’re actually pulsing the motor with 12 V, its maximum drive voltage. The duration of the pulses gives the equivalent speed of a motor voltage of 1.2 V. However, by using a full 12 V amplitude, we’re ensuring that the motor will turn. This is the advantage of PWM. To control speed, we vary the width of the pulse and not the amplitude. Using PWM, you can get very slow motor speeds, and very fine control. The pulses can cause a jerkiness to the motor if the overall frequency is low, but by choosing a high frequency, the jerkiness is averaged out.
Many microcontrollers have internal, software-programmable, PWM modules that make generating PWM signals easy. Even if a processor does not have a PWM module, you can still generate PWM under software control, simply by using a digital output line. Even better than a microcontroller is to use an FPGA, creating a core in VHDL that gives you maximum control over your PWM output.
Let’s now take a look at how you would interface an FPGA or processor to an electric motor using PWM. Due to the voltages and currents required by motors, you cannot simply hang a motor off the pins of an FPGA or processor and expect it to work. You need an interface circuit that will take your logic-level, PWM output, and use this to switch much higher voltages and currents.
The following figure shows a conceptual model (in a crude and simplified form) of such an interface circuit, for driving a small electric motor. This type of circuit is known as an H-bridge.
It’s not as confusing as it first looks. Don’t be too worried about the transistors (Q1 - Q4) in the circuit. They simply act as switches. Our motor operates from a supply voltage, V+. Apply V+ with one polarity and the motor turns in the forward direction. Reverse the polarity and the motor reverses too. To drive the circuit, we use four outputs from the FPGA or processor, two PWM (which I’ve called PWM-A and PWM-B) and two general I/O lines (which I’ve called A and B). Initially, all outputs are low, everything is turned off and the motor is stationary.
If we send A high, the transistor Q4 turns on, and connects the right “side” of the motor to ground. If we then send PWM-A high, the transistor Q1 turns on. Thus, the left “side” of the motor is connected to V+, and the motor spins. By generating a PWM signal on PWM-A, we can control the speed of the motor in that direction. Conversely, by leaving A and PWM-A low, and setting B and PWM-B high, transistor Q2 and transistor Q3 turn on, and the motor spins in the reverse direction. By generating a PWM signal on PWM-B, we can control the speed in the reverse direction.
Care must be taken in your software. If both Q1 and Q3 are turned on, or both Q2 and Q4 are turned on, then you effectively connect V+ to ground, with very little resistance in between! The results would be spectacular and short-lived! A proper H-bridge circuit normally contains protection to prevent such a state from occurring.
The actual implementation of an H-bridge is a little more complicated, and requires additional components such as protection diodes and so forth. Fortunately, H Bridges are available as integrated circuits, which makes using them in a circuit that much easier. Embedded Ltd uses the National Semiconductor LMD18200 H Bridge in our motor control products.
A basic schematic for the LMD18200 is shown below. The actual implementation in our systems is more complex than this for added robustness, but the principal is the same.
The supply voltage for the motor is a DC voltage in the range of 12V to 55V. The LMD18200 can supply motor currents of 3A sustained and up to 6A peak. A PWM output from the FPGA or processor is used to drive the PWM input of the LMD18200. Two GPIO lines are used to control direction and brake.
The H-bridge has internal over-current sensing, and will shut itself off if its temperature rises too high (such as would happen if the outputs were shorted together). The FLAG output of the LMD18200 indicates such a fault and is used to trigger an interrupt. (FLAG requires a pullup resistor in most cases, not shown in the schematic.)
Sensing Motor Speed
In a control application, it is very useful to be able to sense a motor’s speed. The physical system (load) that the motor is driving will affect the motor’s rotation. If the motor must move a heavy load, then its actual speed of rotation may be less than the speed intended. In such situations, it is useful to measure the actual speed so that the embedded control system can compensate.
The easiest way to measure a motor’s rotational speed is to use an optical encoder module, such as the Agilent HEDS-9000 or a similar device. The encoder consists of a light source (LED) and an array of photo-detectors, separated by a slotted disc known as a code wheel.
The disc is mounted on the rotating motor shaft. Each time a slot passes between the LED and a detector, the detector receives a flash of light and generates an electrical pulse. The rate at which the pulses are generated corresponds directly to the rotational speed of the motor. The resolution of the code wheel is known as its counts per revolution or CPR value. The HEDS series of encoders are available with CPRs ranging from 96 all the way up to 2048. The HEDS-9000 optical encoder operates from a 5 V supply, and has two outputs, A and B. These outputs are derived from two adjacent optical sensors. If the code wheel is rotating in one direction, output A will trigger before output B. Conversely, if the wheel is rotating in the opposite direction, then B will trigger before A, and the opposite phase relation will be generated. The rate at which the pulses arrive gives the motor’s speed, and the order in which they arrive shows the direction. This is known as quadrature encoding.
Many microcontrollers have timer/counter inputs that can measure external trigger events such as these. Under software control, you can use the timers to monitor these quadrature signals. With the FPGA-based em8600, you can tightly integrate the positioning supplied by the quadrature-encoded signals with your motor control algorithms, creating real-time motor control that is far more capable than a software-implemented system.