|Pi IoT In Python Using GPIO Zero - PWM|
|Written by Harry Fairhead & Mike James|
|Monday, 28 February 2022|
Page 3 of 3
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
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.
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:
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.
Not Included In This Extract
Raspberry Pi IoT In Python Using GPIO Zero
By Harry Fairhead & Mike James
Buy from Amazon.
or email your comment to: email@example.com
|Last Updated ( Monday, 28 February 2022 )|