Article Index





Now On Sale!



For Errata and Listings Visit: IO Press




Chapter List

  1. Introducing Pi (paper book only)


  3. First Steps With The GPIO

  4. GPIO The SYSFS Way

  5. Input and Interrupts

  6. Memory Mapped I/O

  7. Near Realtime Linux

  8. PWM

  9. I2C Temperature Measurement

  10. A Custom Protocol - The DHT11/22

  11. One Wire Bus Basics

  12. iButtons

  13. The DS18B20

  14. The Multidrop 1-wire bus

  15. SPI Bus

  16. SPI MCP3008/4 AtoD  (paper book only)

  17. Serial (paper book only)


  19. WiFi (paper book only)


The GPIO lines at their most basic output function can be set high or low by the processor. How fast they can be set high or low depends on the speed of the processor. 

Using the GPIO line in its Pulse Width Modulation (PWM) mode you can generate pulse trains up to 4.8MHz, i.e. pulses as short as just a little more than 0.08 microseconds. You can even go faster if you are prepared to do some additional work.

The reason for the increase in speed is that the GPIO is connected to  a pulse generator and once set to generate pulses of a specific type the pulse generator just gets on with it without needing any intervention from the GPIO line or the processor. In fact the pulse output can continue after your program has ended if you forget to reset it. 

Of course, even though the PWM line can generate pulses as short as 0.1 microseconds, it can only change the pulses it produces each time that processor can modify it. For example, you can't use PWM to produce a single 0.1 microsecond pulse because you can't disable the PWM generator in just 0.1 microsecond. 

Some Basic Pi PWM Facts

There are some facts worth getting clear right from the start, although some of the meanings will only become clear as we progress.

First what is PWM?

The simple answer is that a Pulse Width Modulated signal has pulses that repeat at a fixed rate - say one pulse every millisecond but the width of the pulse can be changed. 

There are two basic things to specify about the pulse train that is generated - its repetition rate and the width of each pulse. Usually the repetition rate is set as a simple repeat period and the width of each pulse is specified as a percentage of the repeat period the duty cycle.

So for example a 1ms repeat and a 50% duty cycle specifies a 1ms period which is high for 50% of the time i.e. a pulse width of 0.5ms. The two extremes are 100% duty cycle i.e. the line is always high and 0% duty cycle i.e. the line is always low. 

Notice it is the duty cycle that carries the information in PWM and not the frequency.

What this means is that generally you select a repeat rate and stick to it and what you change as the program runs is the duty cycle. 

In many cases PWM is implemented using special PWM generator hardware that is built either into the processor chip or provided by an external chip. The processor simply sets the repeat rate by writing to a register and then changes the duty cycle by writing to another register. This generally provides the best sort of PWM with no load on the processor and generally glitch free operation. You can even buy addon boards that will provide additional channels  of PWM without adding to the load on the processor. 

The alternative to dedicated PWM hardware is to implement it in software. You can quite easily work out how to do this. All you need is to set a timing loop to set the line high at the repetition rate and then set it low again according to the duty cycle. You can implement this either using interrupts or a polling loop or even in more advanced ways such as using a DMA channel . 

In the case of the Pi the PWM lines are implemented using special PWM hardware. 

The big problem is that although there are two PWM implementations both are only available on the Pi2/3 and even in this case the second PWM is used for sound generation.

However as long as you don't want to use sound you can use both PWM channels and this is enough to run two servos or motor say.  

The main PWM channel zero is brought out on the connector on pin12 via GPIO 18.  The secondary channel which is also used by the system for sound generation is brought out on the connector on pin 35 via GPIO 13. As already mentioned you can only use GPIO 13 if you don't want sound generation. 

Both PWM lines are driven by the same clock and this sometime causes people to conclude that the two lines must work at the same repeat rate. Although there are some restrictions both PWM lines can be set to their own repeat rate.

Two PWM lines is limiting and often forces users to either use hardware expansion or implement PWM in software. 

As you can guess  - there are no PWM inputs, just the one output. If for some reason you need to decode or respond to a PWM input then you need to program it using the GPIO input lines and the pulse measuring techniques introduced in the previous chapters.

The PWM Modes Mark/Space and Balanced

The PWM hardware is more sophisticated than you might have encountered before. 

The first big difference is that it can work in two modes - mark/space or balanced. In fact there are even more options but these are the only two that have a common use. 

In mark/space mode you simply have a repeat rate and a duty cycle. This is precisely what you need if you want to drive a traditional PWM device such as a servo motor. However for other applications where the PWM signal is being used as a simple D to A convertor then this is not the best way to send out a pulse train with a given duty cycle. 

For example suppose you want 1KHz pulse train with a 50% duty cycle pulse train to say deliver 50% power or voltage to a device. The mark/space way of doing this switches the GPIO line on for 500 microseconds and off for 500 microseconds. The fluctuations in voltage are very slow and this causes problems for the driven device. A better way would be to divide the time into say 1 microsecond blocks and spread the on and off times throughout the block. In this case the device would be high for 1 microsecond and low for one microsecond and the filtering problems would be much easier to solve. 

This idea of distributing the duty cycle over the full repeat period is what balanced mode implements. It is much more suitable to applications such as DtoA conversion and driving loads such as LEDs or motors. 

The clock that is supplied to the PWM hardware doesn't set the repeat rate but the smallest unit of change on the PWM output line. In balanced mode it specifies the shortest high or low times for the pulse train. 

The way that this works is that you specify a repeat rate in terms of a range parameter which is used to count down to zero at the clock rate. For example, if you set a clock rate of 1KHz and a range of 1024 then the repeat rate is 1/1024 KHz i.e.  0.99Hz - because the PWM cycle starts over after 1024 clock pulses. 

In mark/space mode a 50% duty cycle would simply mean holding the line high for 512 clock pulses and then low for 512 clock pulses and so on. 

In balanced mode a 50% duty cycle would mean pulses 500 microseconds high and 500 microseconds low. Note that the repeat rate would still be 0,99Hz but the signal would be much higher frequency even though it was a 50% duty cycle. 

It is clear that the range sets the repeat rate. What sets the duty cycle? The answer is there is a data parameter which specifies how long the line should be held high. So for example if range is 1024 and data is 512 the duty cycle is 50%. In mark/space mode the line is held high for the first 512 counts and low for the remainder of the 1024 counts. In balanced mode the line is held high for 512 counts spread over the the total 1024 counts.

Obviously using this way of specifying repeat rate and duty cycle means that you can select more than one range and data value for any given duty cycle. For example a range of 4 and data of 2 is also a 50% duty cycle. So how do you choose a suitable pair of values. 

The range gives you the possible number of duty cycle steps you can use. For example for a range of 2 you modify the duty cycle to 0, 50 or 100% by setting data to 0,1 or 2. A range of 512 gives you 512 steps and is usually called 8 bit PWM and a range of 1024 gives you 1024 duty cycle steps and is usually called 16 bit PWM.

So to summarize

  • The PWM clock gives the smallest unit of change of the PWM line. 
  • The range gives the repeat rate of the pulse train
  • repeat frequency =    clock frequency/(2*range)
  • The range gives the resolution of the duty cycle 1/range% or the number of duty cycle steps that are available. 
  • If you want to set duty cycle d% then data=range*d/100

You can now see why the clock frequency being supplied to both PWM units doesn't constrain them to the same repeat rate. They can work at different rates as long as they can tolerate different duty cycle resolutions and hence range values.