PWM stands for Pulse Width Modulation. Microprocessors cannot produce pure analog signals, nor can they provide enough current to drive motors

Through calculating the average value of a sequence of pulses, microprocessors emulate the generation of analog signals

The wiring i/o board counts with 7 pins that can simultaneously produce PWM signals. Each one of those pins is mapped twice on the board, once on the analog port, and another time somewhere else. We took advantage of this feature for designing our motor control board as small as possible

(in the picture, a detail of one of the motor board PCB prototypes)

 

 

One important aspect of a motor is that it is possible to control its speed through changing the voltage and amount of current at its input. From the technical point of view, we control everything through changing the voltage; it is much simpler at a practical level than to control the amount of current. For example, in a power supply or a battery we know the nominal value of voltage that will be given at their outputs: If a power supply is characterized e.g. by DC-DC 9V/0,8A this means that it will provide us with a supply of 9 volts and up to 0,8 amperes

Figure 1: power supply

We cannot control whether the power supply will provide with 0,3 or 0,75 amperes, it will give as much as needed up to 0,8. It is technically possible to design a power supply pushing out a certain amount of current for a certain voltage value, but this is definitely not cheap when design ing a prototype

This explanation is nothing but an introduction to how to think when trying to control the speed of a DC motor. Since we have no control on the current, the only way to do it is through changing the voltage. A way for characterizing DC motors is through the revolutions they can spin in a minute (rpm) versus the voltage applied to their pins

Depending on the medium, the attribution of slow and quick can mean many things. We clearly need reference level to compare speed. The spinning plate of a microwave oven moves usually at a 7-10 rpm, and that is pretty slow for us. A LongPlay vinyl at 45 rpm is clearly over-accelerated. Those same 45 rpm represent a normal speed on a bike, about 13 km/h . A car at 120 Km/h is making the wheels rotate at the amazing amount of 707 rpm! Can you imagine a microwave at 45 rpm, a biker at 707 rpm, and a LP at 7 rpm?

Therefore what we understand as quick and/or slow depends on the situation. Not all the motors can spin to all the possible values we may need for an application. As a matter of fact, there is not only a limitation on how quick the motor can spin, but even more dramatic is the lower limit end: which is the minimus speed a motor is able to perform

The issue here is then how to control the speed of the motor

Let's take it for granted that we have found a motor that fits our specifications. Toy motors, recycled parts from printers, CD players, etc are in-between the range when working with the kind of applications we will meet in our professional design career

Figure 2: variable power supply like the one that can be found at many laboratories, simple knobs to vary voltage or current

One way to vary the speed is to make use of a variable power supply. By rotating the knob we change the amount of voltage between the motor's pins and that makes it spin faster or slower. This gives an idea of what we need to do to control the speed.

Unfortunately it is not that easy for many of the applications we will develop. The variable power supply is a complex, bulky and expensive piece of technology from which we usually just have a few in the lab. It is not the typical piece of technology we would like to hack and include in any of our prototypes.

Technology provides us with a solution. In this case we can make use of a trick that comes again from lighting techniques. PWM stands for Pulse Width Modulation and it is a technique that we can easily apply when using microprocessors. The typical output from a microprocessor is a digital signal that is 0 volts when LOW and 5 volts when HIGH. This is how it works for the wiring board, the basic stamp, and many other educational tools

Figure 4 shows the representation of a microprocessor's output vs. time. This could be the output of a wiring program like:

/* 
* Looping pin * by DojoDave <http://www.0j0.org> * * turns on and off an output pin connected * to a digital pin, at intervals of 1/2 second *
* Created 26 April 2005
*/

int digPin = 10; // digital pin10

void setup() { pinMode(digPin, OUTPUT); // pin as output }
void loop() {
    digitalWrite(digPin, HIGH); // sets the pin HIGH
    delay(500);                 // waits for a second
    digitalWrite(digPin, LOW);  // sets the pin LOW
    delay(500);
} 

This code example is the equivalent to the Blinking LED example that comes with Wiring, with the only modifications of the pin selected to be switching on and off, and the time intervals the pin will be HIGH or LOW.

If we keep the pin changing constantly, at a certain pace, then we could look into it as a signal with a certain frequency. The frequency is defined as the amount of pulses per second. If you look into the program, since it turns the pin to HIGH once per second, the frequency at that pin happens to be of 1 pulse per second. This is what we call a 1 Hertz frequency pulse. By changing the total delay in our wiring program, we can also change the frequency of the signal. If we change the two lines that include delay(500) to delay(250) , we will be multiplying the frequency by two, thus sending twice as many pulses per second as before.

This kind of signal is what we call a PWM signal, where we can change the frequency of a train of pulses generated by a microprocessor. The frequency can also be represented through the period, which is the mathematical expression of the inverse to the frequency, as the next equation shows.

Figure 3 : mathematical definition of the period

The period is measured in seconds, thus the unit in which we measure frequency (hertz) is the inverse to the time-units (seconds).

Going back to the problem of driving the motor, imagine you could turn the motor on and off so quick that its own inertia would keep it running before turning it on again. If you decrease the frequency between turning the motor on and off the inertia would be smaller and the motor would also move slower. This is then a trick to change the speed of the motor, to turn the voltage at its pins on and off very quick. The way to do this from wiring is to use the already mentioned PWM. PWM in wiring is a signal of constant frequency that changes the time the signal is on or off through the method analogWrite() .

 

Figure 4 : signal used by wiring at the analogWrite() method

If we connect a chip with darlington pairs (like the chip ULN2003A) which are a couple of transistors plugged together in order to drive high values of current, then we can easily play with the motor's speed by using the analogWrite() method as mentioned earlier. The question may be asked here: why not using the relay? The answer is just that relays cannot switch at frequencies over 2Hz, thus cannot be turned on-off more than twice a second, and that is not enough to drive the motor using its inertia.