Pi IoT In Python Using GPIO Zero - PWM
Written by Harry Fairhead & Mike James   
Monday, 28 February 2022
Article Index
Pi IoT In Python Using GPIO Zero - PWM
Software PWM
How Fast Can You Modulate

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

To illustrate the problem consider the following program:

from gpiozero import PWMOutputDevice
pwm=PWMOutputDevice(4)
pwm.frequency=1000
while True:
    pwm.value=0.1
    pwm.value=0.9

It generates a 1kHz PWM signal and attempts to change from 10% to 90% duty cycle as fast as possible. The result is that the number of pulses you get for ea
ch duty cycle is very variable:

pwm6

The conclusion is that, using software-generated PWM, there is no easy way to create an accurate waveform that changes its duty cycle in a controllable way unless the change is very slow.

More PWM Methods

Really, all you need to use PWM is a way to set the frequency and the duty cycle, but the PWMOutputDevice has a few more methods which you might want to use.

The on and off methods set the line high and low and stop all PWM output. Setting a frequency or a duty cycle starts the output again.

The toggle method is slightly odd in that it changes the output from a duty cycle of D to a duty cycle of 1-D. So, for example:

from gpiozero import PWMOutputDevice
from time import sleep
pwm=PWMOutputDevice(4)
pwm.frequency=1000
pwm.value=0.1
while True:
    sleep(0.5)
    pwm.toggle()

produces a signal that has a duty cycle of 10% for half a second and then 90% for the next half a second and so on.

pwm7

The pulse method may seem strange but it makes more sense after we have looked at how a PWM signal is used to control the flow of power to a device, an LED in particular.

pulse(fade_in_time=1, fade_out_time=1, n=None, background=True) 

What pulse does is to vary the duty cycle from 0 to 1 and then back to 0 again. For example:

pwm.pulse(fade_in_time=0.5,fade_out_time=1,n=3,background=False)

starts with a duty cycle of 0 and slowly increases it to 1 over half a second and then decreases it from 1 to 0 in 1 second. The n=3 means that it repeats this three times. If you set the duty cycle to 0, or just leave it out, the pulsing continues forever. The background=False makes Python wait for the number of pulses to complete. If you set it to True then your program carries on at once and the pulsing happens in the background.

The blink method is very similar to pulse but you can specify a full on and full off time. That is, the PWM signal starts from a 0% duty cycle, fades in up to 100%, stays at 100% for the on_time and then fades out to 0% duty cycle and stays off for the specified off_time.

blink(on_time=1off_time=1fade_in_time=0fade_out_time=0, n=Nonebackground=True)

Not Included In This Extract

  • Controlling an LED
  • RGBLED
  • Music – Tonal Buzzer
  • Uses of PWM – D to A
  • What Else Can You Use PWM For?

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.

  • 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, but GPIO Zero only supports software-generated PWM at the moment.

  • The fundamental PWM class is PWMOutputDevice and, although it seems not to be intended for regular use, it is very easy to use it for raw PWM.

  • By varying the duty cycle, you can dim an LED using PWMOutputDevice but it isn’t very accurate on frequency or duty cycle.

  • There is also a PWMLED which can vary the brightness of an LED.

  • The RGBLED class uses three PWMLED to create any color.

  • TonalBuzzer uses PWM to vary the frequency.

  • PWM can be used to implement a D to A converter simply by varying the duty cycle and by varying the output of the D to A you can create music.

Raspberry Pi IoT In Python Using GPIO Zero
Second Edition

By Harry Fairhead & Mike James

GPIOZero2E360

Buy from Amazon.

Contents

  1. Why Pi for IoT?
  2. Getting Started With Python And GPIO Zero
  3. Introduction to the GPIO
  4. Python - Class and Object
  5. Simple On/Off Devices
      Extract 1: On/Off Devices *
  6. Pins And Pin Factories
      Extract 1: Pins 
  7. Some Electronics
  8. Simple Input
      Extract 1:Getting Input ***NEW!!
  9. Complex Input Devices
      Extract 1: Complex Input *
  10. Pulse Width Modulation
      Extract 1:  PWM*
  11. Controlling Motors And Servos
      Extract 1: DC Motors *
  12. Working With Compound Devices
      Extract 1: Compound Devices*
  13. The SPI Bus
  14. Custom SPI Devices
  15. Using The Lgpio Library
  16. Appendix Visual Studio Code Remote Python
    *Extracts from first edition - will be updated.

 <ASIN:1871962870>

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


Kotlin Ktor Improves Client-Server Support
04/11/2024

Kotlin Ktor 3 is now available with better performance and improvements including support for server-sent events and CSRF (Cross-Site Request Forgery) protection.



Edera Releases Open Source Container Benchmark And Scanner
07/11/2024

Edera has released Am I Isolated, an open source container security benchmark that probes users runtime environments and tests for container isolation.


More News

espbook

 

Comments




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

 



Last Updated ( Monday, 28 February 2022 )