iTranslated by AI
How to Use Hardware PWM on Raspberry Pi 4B
PWM Control on Raspberry Pi 4B
Introduction
This article summarizes how to use the hardware PWM on the Raspberry Pi 4B.
You can find the code I used for testing here.
PWM (Pulse Width Modulation) is a technique of turning an electrical signal ON and OFF at a constant frequency. It is used for applications such as adjusting LED brightness, controlling motor speed, and transmitting infrared signals. I personally used it to transmit infrared remote control signals.
While you can easily use hardware PWM in Python by utilizing the RPi.GPIO library, I wanted to control hardware PWM using other programming languages...
So, I investigated how to control hardware PWM from any programming language, and since there was little information available, I have summarized it here.
Hardware PWM vs. Software PWM
The notable differences are as follows. There are other minor differences, but I will omit them.
| Comparison Item | Hardware PWM | Software PWM |
|---|---|---|
| Accuracy | High | Low |
| Frequency Range | MHz order | kHz order |
For example, software PWM is sufficient for adjusting motor speed. However, for applications requiring precise timing, such as transmitting infrared signals, you must use hardware PWM.
Incidentally, infrared remote control signals used to operate home appliances are often modulated at 38kHz. Since the software PWM on the Raspberry Pi 4 is limited to about 5-8kHz at best, hardware PWM is required to transmit infrared remote control signals.
Hereinafter, hardware PWM will be referred to simply as PWM. I will no longer touch upon software PWM.
How to Use
Configuration
Configuration involves two steps:
- Enable the hardware PWM feature.
- Add yourself to the
gpiogroup.
First: On Raspberry Pi OS (Raspbian), the PWM feature is disabled by default. To enable it, append the configuration to the [all] section of /boot/firmware/config.txt and reboot. (For older OS versions like Debian 12 or earlier, it may be /boot/config.txt.)
I used GPIO 19 on the Raspberry Pi 4B, so I configured it as follows:
[all]
dtoverlay=pwm,pin=19,func=2
For more details, reading this documentation is recommended.
Second: You need root privileges or membership in the gpio group. Add your user to the gpio group using the command below and log in again:
sudo usermod -aG gpio $(whoami)
Now you are ready to use PWM. If it still does not work, set the permissions for /sys/class/pwm/. Using udev for automatic configuration is recommended.
Operation
Hardware PWM can be controlled by writing various values to files.
The mapping between operations and file paths is as follows:
Note: The numbers in pwmchip0 or pwm0 may vary depending on the environment or GPIO pin. For example, the Raspberry Pi 5 uses pwmchip1. Also, changing the GPIO pin may result in pwm1 instead of pwm0. Refer to the official documentation for details.
| Operation | File Path | Value |
|---|---|---|
| Enable PWM | /sys/class/pwm/pwmchip0/export |
Channel Number |
| Disable PWM | /sys/class/pwm/pwmchip0/unexport |
Channel Number |
| Toggle ON/OFF | /sys/class/pwm/pwmchip0/pwm0/enable |
1 or 0 |
| Period | /sys/class/pwm/pwmchip0/pwm0/period |
Integer (nanoseconds) |
| Duty Cycle | /sys/class/pwm/pwmchip0/pwm0/duty_cycle |
Integer (nanoseconds) |
| Polarity | /sys/class/pwm/pwmchip0/pwm0/polarity |
normal or inversed |
Let me explain them in order.
-
Enable / Disable PWM
For example, when using GPIO pin 18 (PWM0) on the Raspberry Pi 4B, the channel number is
0. Note that you cannot access anything underpwm0/until you enable it. -
Toggle ON/OFF
Writing
1starts the PWM signal output, and writing0stops it. -
Period
If you want to output a 38kHz PWM signal, write
26315. This value is calculated as follows:\text{Period (ns)} = \frac{1}{\text{Frequency (Hz)}} \times 10^9 \\ = \frac{1}{38000} \times 10^9 \\ \approx 26315 \text{ ns} Note: 1 second = 1,000,000,000 nanoseconds (
ns)10^9 -
Duty Cycle
The signal will be ON for the time specified in this file and OFF for the remaining time. Write an integer between 0 and the period. Setting it to 0 keeps it always OFF, while setting it to the same value as the period keeps it always ON. For example, if you want a 50% duty cycle at a 38kHz period, write half of the period, which is
13157.\text{Duty Cycle (ns)} = \text{Period (ns)} \times \text{Duty Cycle (\%)} \\ = 26315 \times 0.5 \\ \approx 13157 \text{ ns} -
Polarity
Setting this to
inversedflips the ON and OFF states.
Once you understand this, just write values to the files using your preferred method. For example, testing with the echo command is the quickest way.
echo 0 > /sys/class/pwm/pwmchip0/export
echo 26315 > /sys/class/pwm/pwmchip0/pwm0/period
echo 13157 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
echo 1 > /sys/class/pwm/pwmchip0/pwm0/enable
Discussion