Raspberry Pi CM5 IoT In C - - PWM Using GPIO5
Written by Harry Fairhead   
Monday, 12 May 2025
Article Index
Raspberry Pi CM5 IoT In C - - PWM Using GPIO5
Pulse Density Mode
The PWM Registers
Working with PWM
What Else Can You Use PWM For?

What Else Can You Use PWM For?

PWM lines are incredibly versatile and it is always worth asking the question "could I use PWM?" when you are considering almost any problem. The LED example shows how you can use PWM as a power controller. You can extend this idea to a computer-controlled switch-mode power supply. All you need is a capacitor to smooth out the voltage and perhaps a transformer to change the voltage. You can also use PWM to control the speed of a DC motor and if you add a simple bridge circuit you can control its direction and speed.

 

The amount of power delivered to a device by a pulse train is proportional to the duty cycle. A pulse train that has a 50% duty cycle is delivering current to the load only 50% of the time and this is irrespective of the pulse repetition rate. So the duty cycle controls the power, but the period still matters in many situations because you want to avoid any flashing or other effects. A higher frequency smooths out the power flow at any duty cycle.

With a sufficiently fast period you can also use PWM as a digital to analog converter. Simply add a low-pass filter to remove the pulsing and you have a steady voltage proportional to the duty cycle. This can be used to create an audio signal if the clock is fast enough.

Finally, you can use a PWM signal as a modulated carrier for data communications. For example, most infrared controllers make use of a 38kHz carrier, which is roughly a 26µs pulse. This is switched on and off for 1ms and this is well within the range that the PWM can manage. So all you have to do is replace the red LED in the previous circuit with an infrared LED and you have the start of a remote control, or data transmission, link.

 

Summary

  • 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.

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

  • All versions of the Pi have two hardware PWM channels which can be used and configured using Linux drivers.

  • The CM5 has two additional channels which cannot be easily used via the driver. The PWM chip has also changed from pwmchip0 to pwmchip2.

  • The PWM drivers do not provide control over the PWM clock frequency which determines how accurately you can set the duty cycle.

  • A typical use of PWM is to control a servo and this only requires a PWM frequency of 50Hz. The position of the servo depends on the duty cycle.

  • You can easily invert the sense of the PWM signal, which is useful when the device is being driven by a single transistor.

  • 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.

  • 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.

 

Raspberry Pi Compute Module 5
IoT In C
Using Linux
Drivers and Gpio5

By Harry Fairhead

CIoTCM5360
Buy from Amazon.

Contents

  1. The CM5 For The IoT
  2. Setting Up the CM5
  3. C and Visual Studio Code
  4. Drivers: A First Program
  5. The GPIO Character Drive
  6. GPIO Using I/O Control
  7. GPIO Events
  8. GPIO Hardware With Gpio5 
  9. Some Electronics
  10. The Device Tree
  11. Pulse Width Modulation
        
    Extract: PWM Using GPIO5
  12. SPI Devices
  13. I2C Driver and Gpio5
  14. Sensor Drivers – Linux IIO & hwmon
  15. 1-Wire Bus
  16. The PIO
        
    Extract: Getting Started With PIO ***NEW!!!
  17. Going Further With Drivers
  18. Almost Real-Time Linux
  19. Appendix I Gpio5

 

 

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.

Banner


Deno Not Giving Up Over JavaScript Trademark
01/07/2025

Deno has faced a setback in its attempt to get Oracle to relinquish the JavaScript Trademark. The US Patents Office Trademark Trial and Appeal Board (TTAB) dismissed Deno's fraud claim which is one th [ ... ]



Apple Updates Developer Tools
16/06/2025

Apple has announced a range of improvements to its developer tools at this year's WWDC (WorldWide Developers Conference), including a new foundation models framework and updates to Xcode, Apple's  [ ... ]


More News

pico book

 

Comments




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

 


Last Updated ( Tuesday, 13 May 2025 )