You can easily work out the number of duty cycles available at any given frequency:
Number of clock pulses
Number of different duty cycles
Resolution in bits
20000/n - 1
In many applications 8-bit resolution for the duty cycle is considered the minimum acceptable and this sets the highest frequency to about 75kHz, which is high enough for most things.
For example, if you want to control a servo motor, see later, then you need a PWM signal with a frequency of 50Hz and at this frequency you can specify the duty cycle down to about 18 bits or around 250 thousand increments – more than enough for any real servo motor.
In Chapter But Not In This Extract
Controlling An LED
How Fast Can You Modulate?
Controlling a Servo
What Else Can You Use PWM For?
Complete PWM Program
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 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.
Software Developer is again top of the U.S. News and World Report's annual best jobs rankings and while GlassDoor's Best Paces To Work 2023 includes lots of tech companies, Meta and Apple are not [ ... ]