PWM (Part 1)

Part 1, 2, 3, 4

PWM stands for Pulse Width Modulation

This is a very convenient function for driving high latency loads such as heaters and even light emitting devices or specific modules such as servo motors. The principle of operation consists in applying trains (so as to say cycles) of energy to the load. Electronically speaking, this consists in switching the feeding power supply line on and off. The on versus off ratio determines the amount of energy applied to the load and refers to the ‘duty cycle’.

The picture bellow illustrates this principle. Imagine that you feed your boiler with electrical power along a certain amount of time (between t0 and t1).

In the first case, we adjust the amount of energy (analog mode), in the second case, we adjust the duration of full energy pulses (digital mode). Now we want to compare the power spent in each case. Power is the amount of energy over a period of time (expressed as: P=E/t, with P in Watts, E in Joules and t in seconds). All we have to do is to compute the integral of E over time, which may be simplified in computing the area under curve of both waves. Areas are the same, the same power has been dissipated in the boiler. c.q.f.d

The advantage of the digital mode lies in the little amount of dissipated energy accross the switching device, which can be one of relays (that’s rough), transistors (better, but Vce is still significant), thyrsitors/triacs and ultimately low drop MOS FETs. Instead of modulating the amount of power applied to the load, a switching power supply will modulate its on/off ratio so that the integral value of power shall be identical to what it may have been with the firstly described system.

Arduino offers PWM outputs which are controlled by the standard analogWrite()

On most Arduino boards (those with the ATmega168 or ATmega328), this function works on pins 3, 5, 6, 9, 10, and 11. On the Arduino Mega, it works on pins 2 through 13. Older Arduino boards with an ATmega8 only support analogWrite() on pins 9, 10, and 11.

Setting a duty cycle of 255 sets the related pin to a constant +5V, while a duty cycle of 0 sets the related pin to a constant 0V. As is, this function will suffice for most applications, except for those which require a specific base frequency. In anaogWrite(), the frequency of the PWM signal is approximately 490 Hz resulting from the simplified equation: F_CPU / clock prescaler / 2 / TOP = 16000000 / 64 / 2 / 256 = 488 Hz. From this equation, we observe some confusing parameters such as a ‘clock prescaler’, an arbitrary ’2′ and a friendly looking but meaningless ’256′. This observation may lead us to think that the analogWrite() function hides some programming tricks… and this is true. And their names are: timers! There are 3 of them in an ATmega328, all different in some aspects.

For those who are courageous, go to the timers section of the ATmega datasheet, or have a look at this nice thread which is a very good starting point.

Next post on same subject

Leave a Reply

You must be logged in to post a comment.