Pulse Width Modulation (PWM)#
What is it#
PWM
or Pulse Width Modulation
, is a technique used to control the amount of power delivered to an electronic device by breaking up the power signal into discrete ON and OFF periods.
The amount of time the signal spends ON during each cycle determines the output power level (brightness of the LED).
Configuring PWM overlay#
To enable any of the PWM Pins, we have to modify the following file: /boot/firmware/extlinux/extlinux.conf
. We can check the available list of Device Tree Overlays using command below,
ls /boot/firmware/overlays/ | grep "beagley-ai-pwm"
When executed the above command should give output show below,
debian@BeagleBone:~$ ls /boot/firmware/overlays/ | grep "beagley-ai-pwm"
k3-am67a-beagley-ai-pwm-ecap0-gpio12.dtbo
k3-am67a-beagley-ai-pwm-ecap1-gpio16.dtbo
k3-am67a-beagley-ai-pwm-ecap1-gpio21.dtbo
k3-am67a-beagley-ai-pwm-ecap2-gpio17.dtbo
k3-am67a-beagley-ai-pwm-ecap2-gpio18.dtbo
k3-am67a-beagley-ai-pwm-epwm0-gpio12.dtbo
k3-am67a-beagley-ai-pwm-epwm0-gpio14.dtbo
k3-am67a-beagley-ai-pwm-epwm0-gpio15.dtbo
k3-am67a-beagley-ai-pwm-epwm0-gpio5.dtbo
k3-am67a-beagley-ai-pwm-epwm1-gpio13.dtbo
k3-am67a-beagley-ai-pwm-epwm1-gpio20.dtbo
k3-am67a-beagley-ai-pwm-epwm1-gpio21.dtbo
k3-am67a-beagley-ai-pwm-epwm1-gpio6.dtbo
Using hat-08 (GPIO14) as pwm#
Add the line shown below to /boot/firmware/extlinux/extlinux.conf
file to load the gpio14 pwm device tree overlay:
fdtoverlays /overlays/k3-am67a-beagley-ai-pwm-epwm0-gpio14.dtbo
Your /boot/firmware/extlinux/extlinux.conf
file should look something like this:
label microSD (default)
kernel /Image
append console=ttyS2,115200n8 root=/dev/mmcblk1p3 ro rootfstype=ext4 resume=/dev/mmcblk1p2 rootwait net.ifnames=0 quiet
fdtdir /
fdt /ti/k3-am67a-beagley-ai.dtb
fdtoverlays /overlays/k3-am67a-beagley-ai-pwm-epwm0-gpio14.dtbo
initrd /initrd.img
Now reboot you BeagleY-AI to load the overlay,
sudo reboot
How do we do it#
To configure HAT pin8 (GPIO14) PWM symlink pin using beagle-pwm-export
execute the command below,
sudo beagle-pwm-export --pin hat-08
Let’s create a script called fade.sh
that cycles through LED brightness on HAT pin8 by changing PWM duty cycle.
touch fade.sh
Now open the file with nano editor,
nano fade.sh
In the editor copy paste the script content below,
#!/bin/bash
PWMPIN="/dev/hat/pwm/GPIO14"
echo 1000 > $PWMPIN/period
echo 0 > $PWMPIN/duty_cycle
echo 0 > $PWMPIN/enable
sleep 1
for i in {1..500};
do
echo $i > $PWMPIN/duty_cycle
echo 1 > $PWMPIN/enable
echo $i
sleep 0.0005
done
for i in {500..1};
do
echo $i > $PWMPIN/duty_cycle
echo 1 > $PWMPIN/enable
echo $i
sleep 0.0005
done
Close the editor by pressing
Ctrl + O
followed byEnter
to save the file and then press toCtrl + X
exitNow execute the
fade.sh
script by typing:
bash fade.sh
You can exit the
fade.sh
program by pressingCtrl + C
on your keyboard.