You can easily work out the number of duty cycles available at any given frequency:
PWM Frequency
Number of clock pulses
Number of different duty cycles
Resolution in bits
10MHz
2
3
1.6
5MHz
4
5
2.3
2.5MHz
8
9
3.2
1.25MHz
16
17
4.1
625kHz
32
33
5.0
312.5kHz
64
65
6.0
156.25kHz
128
129
7.0
nkHz
20000/n
20000/n - 1
Log2(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
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 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 IoT In PythonUsing Linux Drivers Second Edition
Zitadel has announced a major funding round that will be used to expand technical teams and fund further product development. The company is the creator of an open source project for cloud-native iden [ ... ]
Apache has announced the release of Tomcat 11, as well as marking the 25th anniversary of the first commit to the Apache Tomcat source code repository since becoming an ASF project.