How To Use Hardware PWM

Check HardwarePWM Pin

Hardware PWM had open at default dtb configuration.So you need to check which Physical pin is connect to it .

Switch to root user

Ordinary users cannot control GPIO, so they need to switch to the root user.

1
2
3
khadas@Khadas:~$ sudo -i
[sudo] password for khadas:
root@Khadas:~#

Set Hardware PWM

Setup and enable PWM

1
2
3
4
root@Khadas:~# echo 1 > /sys/class/pwm/pwmchip4/export
root@Khadas:~# echo 1000000 > /sys/class/pwm/pwmchip4/pwm1/period
root@Khadas:~# echo 500000 > /sys/class/pwm/pwmchip4/pwm1/duty_cycle
root@Khadas:~# echo 1 > /sys/class/pwm/pwmchip4/pwm1/enable
1
2
3
4
root@Khadas:~# echo 1 > /sys/class/pwm/pwmchip1/export
root@Khadas:~# echo 1000000 > /sys/class/pwm/pwmchip1/pwm1/period
root@Khadas:~# echo 500000 > /sys/class/pwm/pwmchip1/pwm1/duty_cycle
root@Khadas:~# echo 1 > /sys/class/pwm/pwmchip1/pwm1/enable
1
2
3
4
root@Khadas:~# echo 1 > /sys/class/pwm/pwmchip4/export
root@Khadas:~# echo 1000000 > /sys/class/pwm/pwmchip4/pwm1/period
root@Khadas:~# echo 500000 > /sys/class/pwm/pwmchip4/pwm1/duty_cycle
root@Khadas:~# echo 1 > /sys/class/pwm/pwmchip4/pwm1/enable

If you sueccess to enable it , you can see it with oscilloscope.

pwm-Oscilloscope

Note

If you use an oscilloscope to view the waveform, remember that the board and the oscilloscope need to share the GND.

Disable PWM

1
root@Khadas:~# echo 0 > /sys/class/pwm/pwmchip4/pwm1/enable
1
root@Khadas:~# echo 0 > /sys/class/pwm/pwmchip1/pwm1/enable
1
root@Khadas:~# echo 0 > /sys/class/pwm/pwmchip4/pwm1/enable