Step03 – PWM

Pulse Width Modulation is used for servo control, or LED brightness control, etc. It is a handy set of outputs to have. The BeagleBone has 8 PWM outputs in total.

The PWM drivers can again be operated through the filesystem. PWM is available at:

[code]/sys/class/pwm/[/code]

Under here you’ll find all of the pwm outputs. Each has it’s own directory. We’ll concentrate on the ehrpwm1.0 folder. This pwm output controls the GPIO pin EHRPWM1A on the P9 expansion header of the BeagleBone (Pin 14).

[code]cd /sys/class/pwm/ehrpwm.1:0[/code]

Before we start modifying the PWM control files though, we need to sort out the pin mux. As default the GPIO pin for P9 (Which is pin U14 of the processor) is not set to PWM output. We need to change the pin mux for that pin. The pin in question is referenced via the filesystem at /sys/kernel/debug/omap_mux/gpmc_a2. The mode we require is 6. So we can set the pin mux to PWM output by typing:

NOTE: For Pin MUX and expansion header pinout information look in the BeagleBone reference manual Pages 54-58 of the current A6 manual detail the P8 header pinout and mux settings, and Pages 59-63 describe the same for the P9 expansion header.

[code]echo 6 > /sys/kernel/debug/omap_mux/gpmc_a2[/code]

This doesn’t produce any PWM on the pin, but it does connect the pin to the output of the PWM module. Once we setup and enable the PWM module, we’ll see PWM appear on the pin. If we haven’t connected the PWM module to the pin we won’t see any PWM output.

We must first request access to the PWM channel:

[code]echo 1 > request[/code]

Now nothing can grab the driver and interfere with what we’re doing. We should setup a period frequency. This is the base frequency of the PWM signal. Let’s set it to 100Hz:

[code]echo 100 > period_freq[/code]

Set the duty cycle percentage to 50%. This means that the wave will have a high value for 50% of the period and a low value for 50% of the period:

[code]echo 50 > duty_percent[/code]

Now enable the module and observe the output of that pin (you’ll need an oscilloscope or something to observe the output!)

[code]echo 1 > run[/code]

Now change the duty cycle and observe the output of the pin again. You’ll see that the wave will have a high value for 25% of the period and a low value for 75% of the period.

[code]echo 25 > duty_percent[/code]

To release the PWM channel so that other resources can use it, simply unrequest it:

[code]echo 0 > request[/code]

I might add another example C source file here, but you should be able to adapt the one from Step02 – User LED Control to write the necessary files in order to control the PWM output of any of the eight PWM channels.

Leave a Reply