Pulse Width Modulation is a modulation technique where a digital pin alternates high and low. It is possible to configure the period (how frequent should the output oscillate) and the duty cycly (how much should the output stay high).

PWM signals can be used to:

  • control LEDs brightness
  • control colors on RGB LEDs
  • control motors, like servos
  • etc.

Step 1: enable PWM outputs

Enable the PWM features on the external pinout using the device tree editor.

Step 2: configure the outputs

Before configuring a PWM output, it is necessary to export it:

echo 0 > /sys/class/pwm/pwmchip0/export

Then set period and duty cycle. For example, to generate a 1kHz signal with 30% of duty cycle, use:

echo 1000000 > /sys/class/pwm/pwmchip0/pwm0/period
echo  300000 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle

Then enable the PWM output:

echo 1 > /sys/class/pwm/pwmchip0/pwm0/enable

Possible values to be used

period - The total period of the PWM signal (read/write). Value is in nanoseconds and is the sum of the active and inactive time of the PWM.

duty_cycle - The active time of the PWM signal (read/write). Value is in nanoseconds and must be less than the period.

enable - Enable/disable the PWM signal (read/write); 0 means disabled

PWM settings generator

Select the desired frequency and duty cycle:

PWM frequency
Duty cicle
Generated code
echo 123456789 > /sys/class/pwm/pwmchip0/pwm0/period
echo 987654321 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
This page was last updated on Friday, June 16, 2017 at 1:12 PM.