Raspberry Pi IoT In C - Basic Pulse Width Modulation
Written by Harry Fairhead   
Monday, 20 June 2022
Article Index
Raspberry Pi IoT In C - Basic Pulse Width Modulation
Clock rate
How Fast

How Fast Can You Modulate?

In most cases the whole point is to vary the duty cycle or the period of the pulse train, for reasons that will be discussed later. This means that the next question is how fast can you change the characteristic of a PWM line? In other words, how fast can you change the duty cycle? There is no easy way to give an exact answer and, in most applications, an exact answer isn't of much value. The reason is that for a PWM signal to convey information it generally has to deliver a number of complete cycles with a given duty cycle. This is because of the way pulses are often averaged in applications.

We also have another problem – synchronization. There is no way to swap from one duty cycle to another exactly when a complete duty cycle has just finished. All you can do is use a timer to estimate when the pulse is high or low.

What this means is that there is going to be a glitch when you switch from one duty cycle to another. Of course, this glitch becomes less important as you slow the rate of duty cycle change and exactly what is usable depends on the application. For example, set up the PWM to clock(2), 9.6MHz, which with range 256 gives a repeat time of 26.66µs. We can change the duty cycle from 0 to 256 and in this case we choose 128, i.e. 50%, and, 16, i.e. 6.25%, and switch between then after eight cycles:

#include <stdio.h>
#include <stdlib.h>
#include <bcm2835.h>
int main(int argc, char** argv) {
    if (!bcm2835_init())
        return 1;
    bcm2835_gpio_fsel(18, BCM2835_GPIO_FSEL_ALT5);
    bcm2835_pwm_set_mode(0, 1, 1);
    bcm2835_pwm_set_range(0, 256);
    for (;;) {
        bcm2835_pwm_set_data(0, 16);
        bcm2835_pwm_set_data(0, 128);
    return (EXIT_SUCCESS);


The result looks fairly good and notice that the change in duty cycle is correct, the last 50% unit is followed by a 6.25% unit:


The fastest PWM repetition rate that you can use is 26.6µs for an 8-bit resolution and you can change the duty cycle as frequently as once per pulse.

In Chapter but not in this extract

  • Uses Of PWM – Digital To Analog
  • Music
  • Controlling An LED
  • Changing LED Brightness Linearly
  • Controlling A Servo
  • What Else Can You Use PWM For?
  • Some Hardware Details



  • PWM, Pulse Width Modulation, has a fixed repetition rate but a variable duty cycle, i.e. the amount of time the signal is high or low changes.

  • PWM can be generated by software simply by changing the state of a GPIO line correctly, but it can also be generated in hardware so relieving the processor of some work.

  • As well as being a way of signaling, PWM can also be used to vary the amount of power or voltage transferred. The higher the duty cycle, the more power/voltage.

  • The Pi has two hardware PWM lines and these are capable of a range of operational modes.

  • The PWM lines are controlled by two counters – data, which sets the time the line is high, and range, which sets the repeat rate. Both work in terms of number of clock pulses.

  • You can select mark/space mode which distributes the high time more evenly across the repeat time to smooth out power/voltage transfer.

  • Hardware PWM can generate high speed pulses, but how quickly you can change the duty cycle is still software-limted.

  • PWM can be used to implement a DtoA converter simply by varying the duty cycle.

  • By varying the output of the DtoA you can create music.

  • In the same way, by varying the duty cycle, you can dim an LED. As the brightness of an LED is not linear with applied voltage, you have to modify the output using a cubic law to get linear changes in brightness.

  • Servo motors set their position in response to the duty cycle of a PWM signal.

  • The PWM lines in the Pi are sophisticated and there are many non-standard configurations you can use to push the performance beyond the norm.


Raspberry Pi And The IoT In C Second Edition

By Harry Fairhead


Buy from Amazon.


  1. Why Pi For IoT?
  2. Getting Started
  3. Getting Started With The GPIO
  4. Simple Output
  5. Some Electronics
  6. Simple Input
  7. GPIO The Linux Way
       Extract 1:The Linux GPIO Driver 
  8. Advanced Input – Events, Threads, Interrupts
       Extract 1: Events & Interrupts 
  9. Pulse Width Modulation - Servos And More
       Extract 1:Basic Pulse Width Modulation 
  10. Using The I2C Bus
  11. The DHT22 Sensor Implementing A Custom Protocol
  12. Exploring - 1‑Wire Bus Basics ***NEW!
  13. Using iButtons
  14. DS18B20 Temperature Sensor
      Extract 1: The DS18B20 Temperature Sensor 
  15. The Multidrop 1‑Wire Bus
  16. The Serial Port
      Extract 1: 1-wire Via Serial 
  17. Getting Started With The SPI Bus
  18. A to D With The SPI Bus
  19. Connecting With The Web - Sockets
  20. Memory-Mapped GPIO
  21. Almost Real-Time Linux
  22. Appendix I GPIO Sysfs Interface



To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.


Rust's Rapid Rise on TIOBE Index

Rust is making spectacular progress up the TIOBE index and JavaScript is also on the up and experiencing a personal best. Kotlin is maintaining its inclusion in the top 20 and the gap at the very top  [ ... ]

.NET 9 Preview 6 Improves ARM64 Code Generation

.NET 9 Preview 6 is now available with a wide range of improvements to the runtime, SDK and ASP.NET Core.

More News

kotlin book



or email your comment to: comments@i-programmer.info


Last Updated ( Saturday, 25 June 2022 )