Using PWM outputs

The BeagleBone has 8 PWM outputs. These come from three EHRPWM modules (two output each) and two eCAP modules with one output each.

To use the PWMs using BoneScript

Use analogWrite(pin,value,freq,callback) to set the PWM frequency and duty cycle.

On the command line:
 - node -e "require('bonescript').analogWrite('P8_13', 0.870, 50, console.log);"

or in Cloud9:
 - var b=require('bonescript');
 - b.analogWrite('P8_13', 0.870, 50, console.log);"

Works OOTB with pins: P8_13, P9_14, P9_21, P9_42

To use the PWMs from any other environment

The first step is to enable the EHRPWM clocks. This can be done either by:
  • Modifying the memory mapped registers directly (see attached cpp source)
  • Building the source without the config flag CONFIG_OMAP_RESET_CLOCKS=y
The first alternative is by far the easiest. Just do:
  1. Copy the attached source to the BeagleBone
  2. Compile it with the command "g++ enable_clocks.cpp -o enable_clocks"
  3. Run it with the command "enable_clocks -e 1 2 3" to enable all three EHRPWMs
(I don't know how to enable the clocks for eCAP with this approach though, so you will only get 6 PWM outputs enabled) 
Controlling the PWM outputs can be done in user space, and is done the same way for both EHRPWM outputs and eCAP PWM outputs:
  1. cd /sys/class/pwm/ehrpwm.1\:0
  2. cat request
  3. echo 1 > request
  4. echo 1 > run
  5. echo 100 > period_freq
  6. echo 10 > duty_percent
Wien you are done, release the port:
  1. echo 0 > request

Note that the paired EHRPWMs share the frequency setting. I.e. ehrpwm.1:0 and ehrpwm.1:1 always have the same frequency.

For one servo I have, I've measured the following min/max duty cycles for the given period frequency: 
  • Period_freq 100, duty_percent: 5-22
  • Period_freq 90, duty_percent: 4-20
  • Period_freq 80, duty_percent: 4-18
  • Period_freq 70, duty_percent: 3-16
  • Period_freq 60, duty_percent: 3-13
  • Period_freq 50, duty_percent: 2-11


Subpages (1): enable_clocks.cpp