We can drive a DC motor with our transistor circuit for controlling DC power to inductive loads. Using a single digital output bit to control the transistor will turn the motor on, full speed, when the transistor is on and turn it off when the transistor is off. What about running the motor at different speeds ? The speed that a DC motor runs at (for a given load) is proportional to the magnetic field strength in the coils. This field is proportional to the applied current, so it would appear that we need to control the current (it is the way the equations for the coil current work out for series motors that make them tend to ``run away'' at no loads). But focusing on the primary requirement of controlling the magnetic field strength suggest an approach that is much more convienient from a digital control perspective. Recall that those coils store current (causing us to be preoccupied with ``fly-back'' or ``shunt'' circuits in order to handle this current when its released). Now we can use that current storage property to our advantage. When we turn on the controlling transistor we charge up the coil. If we then turn the transistor off, the voltage across the coil drops and the field collapses, but this takes time. So if we turn the transistor back on again before the field fully collapses then it starts to climb back up before it ever gets to zero. The shorter we make the off time the higher the average field strength, up to the point where the on-time is continuous and we get a fully on motor coil. So by controlling our transistor on and off rates properly we can vary the field strength from full on to full off.
The typical way to do this is with a pulse-width modulated signal. With this type of signal the cycle time is fixed (say 1 kiloHertz) and the duty cycle (the proportion of the cycle time during which the signal is on) is varied. Listing 1, pwm.fth provides a simple implementation of this type of control.