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.