Re: Unable to access PWM from userspace on BBB

Skip to first unread message

Jason Stapels

May 2, 2013, 10:48:29 PM5/2/13
Unfortunately they seemed to have changed the PWM driver in the newer linux kernels. I've been trying to find some good documentation on it myself, but nothing so far. I was able to play around with it a bit by digging through the current Bonescript source for analogWrite to see what it does and was able to PWM an LED via an NPN transistor.

But it was a little hard to follow for an amateur like me.

Koen Kooi

May 3, 2013, 2:15:19 AM5/3/13

Op 3 mei 2013, om 05:12 heeft het volgende geschreven:

> I found the same link and I also find it all a bit overwhelming. I've switched to Debian Squeeze with a 3.2 kernel following the instructions here: So far I see nothing on HDMI and I'm not seeing any serial connection detecting on my host linux system. Does anyone know if these instructions are valid for the BBB? Will a 3.2 kernel work

No, it won't

> (so I don't have to muck around with devicetree) or do I have to go with a 3.8 kernel?
> --
> For more options, visit
> ---
> You received this message because you are subscribed to the Google Groups "BeagleBoard" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> For more options, visit

Jason Stapels

May 3, 2013, 8:29:23 AM5/3/13
Unfortunately I'm in the process of replacing my BBB so I don't have one for probably another week, but I'm very interested in using the PWM as well (disclaimer: I want to try and make a quadcopter out of it). As soon as I get my bone back I'll be putting a lot of effort into understanding how the device tree works and such. I'd be happy with work with you on putting together a rock solid beginner tutorial on a wiki or block somewhere. I know that doesn't help you much now, but hang in there :).

I dug around in some of the other posts and it looks like in general, the entire device tree stuff is in a state of flux. Sounds like there either hasn't been a solid proposal going forward, or at least there hasn't been enough agreement quite yet. So it might very well be that there's not a whole lot of concrete docs yet because it hasn't been finalized. That said, this IS an open source project so we can easily just start dissecting the device drivers and figuring this stuff out ourselves.

I also want to figure out how these cool PRUs are supposed to work. Flying a quadcopter can be dangerous in a non-realtime OS but the Bone has lots of extras that should make it possible. We just have to unlock all their secrets :).

~ Jason

On Friday, May 3, 2013 3:40:11 AM UTC-4, wrote:

Okay, I have switched to a 3.8 kernel and now it boots Debian squeeze fine. Has anyone figured out the magic to access PWM's outside of the kernel on the BeagleBone black? I can see that the PWM support is being built into my kernel but I don't see any way to access/enable the functionality. I suspect it's something to do with the device tree configuration but I'm new to devicetree and I'm finding it has a pretty steep learning curve.

Kaustubh Bhalerao

May 3, 2013, 10:46:43 AM5/3/13
That is a great idea - count me in. I've done lots of arduino / AVR stuff, but this is my first linux / ARM system. I would be delighted to help with the tutorials. My interest is in developing laboratory instrumentation with these things - like pH meters, spectroscopes, impedance analyzers, data loggers etc. So would like to help with your tutorials.

I also teach a college-level instrumentation class, so I might be a user for your tutorials as well.

Jason Stapels

May 3, 2013, 1:21:43 PM5/3/13
If the goal is a tutorial, the SYSFS interface does provide the simplest way to ease someone into using PWM. I think that being able to simply access it from the command-line does make it very approachable from a beginner point of view. I agree that there is definitely more efficient means for working with the PWM, but realistically if you need that low level of interaction, you'll likely being using the /dev/mem device to direct access to the registers (or using a polished C library that does it for you).

Also, as I understand it, the SYSFS interface is the only way to mux the pins you'd need from userspace.

Michael Vernier

May 3, 2013, 3:11:43 PM5/3/13
I got PWM working on my BBB from the command line the other day.  Here's what I did on Angstrom (4/13 image).  

Add the am33xx_pwm module to the bone_capemgr.  You'll have to look at your BBB for the # of the cape manager.  Mine is bone_capemgr.8, but since I've only tested on a single board, I'm not sure if the numbers are the same across every board.
$ echo am33xx_pwm > /sys/devices/bone_capemgr.#/slots

Add the individual PWM module to the bone_capemgr. For example, EHRPWM2B is pin 13 on the P8 connector.  To enable this pin for PWM, use the following command
$ echo bone_pwm_P8_13 > /sys/devices/bone_capemgr.#/slots

This creates a link in /sys/devices/ocp.#/pwm_test_P8_13.#.  For my BBB, I have /sys/devices/ocp.2/pwm_test_P8_13.12.  The number in the pwm_test has changed for me for a couple times that I've used it, so you'll have to see what number it assigns for you.

All the settings for the PWM configuration are found in that folder similar to the old interface.  To generate a 50 Hz signal with 50% duty cycle, (the period and duty times are giving in nanoseconds)
$ echo 20000000 > /sys/devices/ocp.#/pwm_test_P8_13.#/period 
$ echo 10000000 > /sys/devices/ocp.#/pwm_test_P8_13.#/duty

I found all this from digging through the BoneScript source code found here  bonescript/node_modules/bonescript/index.js has the core implementation of the module.

Hope this helps.

Robert Nelson

May 3, 2013, 6:30:25 PM5/3/13
On Fri, May 3, 2013 at 4:27 PM, <> wrote:
> Thanks a lot. I was able to get an LED to blink using Angstrom with these
> steps. Notably the Debian Squeeze install I made with the instructions here:
> only partially worked.
> The link for the PWM showed up but there were no "duty" and "period" files
> in the folder.

So, can you expand a little on "only partially" worked, at what point
did the directions fail for you?


Robert Nelson

Robert Nelson

May 3, 2013, 7:06:03 PM5/3/13
On Fri, May 3, 2013 at 5:41 PM, <> wrote:
> The instructions were great in general and was able to get them to work
> almost letter for letter. The only hiccup I had was that I had to append
> "--no-check-certificate" on all the wget commands, it was complaining about
> the certificates being self signed.

Weird, better fire up a new vm and try it without first git cloning
one of the github tree's, as that probably okay'ed the certificate on
my system... Thanks!

> The problem I had was just with enabling the PWMs. Following Michael's
> instructions I got a "/sys/devices/ocp.2/pwm_test_P8_13.12" folder but there
> was were no "duty" or "period" files in the folder. It's possible I made a
> typo since I didn't do too much troubleshooting before I rebooted into
> Angstrom when I didn't see the links I needed to control the PWM.


May 21, 2013, 6:50:32 PM5/21/13
Did you ever find a fix to this? It appears that ubuntu also suffers the same problem. (I trying to write a proper c++ library for the servos) 

On Friday, May 17, 2013 7:18:41 AM UTC-4, wrote:
same her with ARCH Linux ARM.
So there's a difference in the pwm driver (kernel module?) being used by the BBB distro and Arch or Debian ? I'm not really (no, really not at all) very smart when it comes to this. I'm lost when Google does't know the answer to my problems (or the question which suits my problems ;-) )

Anyway the BBB Angstrom Distro should be fine for testing. I tried ARCH since BBB Angstrom comes with so much stuff I don't want (Cloud9 etc etc)

Just in case somebody is curious: I followed these (very simple) instructions to set up ARCH Linux ARM on a SD Card for the Beaglebone Black.

Am Mittwoch, 8. Mai 2013 17:11:13 UTC+2 schrieb Markus:

my BBB is running on Debian 7 (netinstall 3.8.8-bone14 #1 SMP Wed Apr 24 08:55:00 UTC 2013 armv7l GNU/Linux)
 and, following Micahel's instructions, I've got the same problems as Neils reported.
I noticed that there's only one capemgr device (/sys/devices/bone_capemgr.9/).

When I echo bone_pwm_PX_XX > /sys/devices/bone_capemgr.9/slots I end up with pwm_test folders that look like this:


Any ideas what's wrong on my system?


John Leichty

Jun 1, 2013, 10:14:33 PM6/1/13
Derek, could you elaborate on what you mean by the Ubuntu image not having EHRPWM_TEST set? I'm experiencing the same problem of the missing duty and period files in the Ubuntu image and am trying to figure out where to make these changes (and then how to recompile and and reinstall the kernel).

On Friday, May 24, 2013 6:30:47 PM UTC-7, wrote:
I found the source of the problem. The Ubuntu image did not have the EHRPWM_TEST set as opposed to the Angstorm image. I enabled it and reinstalled the kernel using the script here:

This brought up the period and duty files that were missing.

It should also be noted that when compiling the module, I ran into compilation error (caused by a missing "}"). Changes I made can be found here:

On Thursday, May 23, 2013 7:52:04 PM UTC-4, wrote:
I'm also running into the same problems. I'm running the 12.10 image from and I don't have any files named duty or period show up.

Mark A. Yoder

Jun 3, 2013, 12:01:09 PM6/3/13
I have some detailed notes[1] I've made up for my students that might be helpful. I think pwm a moving target at this time.



On Monday, June 3, 2013 6:31:59 AM UTC-4, wrote:
echo am33xx_pwm > /sys/devices/bone_capemgr.#/slots

On Saturday, June 1, 2013 9:38:29 PM UTC+2, Thomas Carter wrote:
ready for a noob question?  I have been trying to not ask you this for the last hour, or two, but just how exactly do I "add the am33xx_pwm module" to the bone_capemgr.  Are you using a cape? Do I have to ?  Ready for it to get really dumb?  I cannot see anywhere on the board where it describes a cape manager number... I am lost, sorry.

Jun 24, 2013, 3:06:21 PM6/24/13
Hi John

Did you figure out how to set the EHRPWM_TEST?

Thomas Carter

Jun 25, 2013, 1:48:36 PM6/25/13

Thanks for sweet tutorial.  Only one thing, I get invalid argument after entering : Beagle$ echo 1000000 > period_ns   ....  any ideas?

For more options, visit
You received this message because you are subscribed to a topic in the Google Groups "BeagleBoard" group.
To unsubscribe from this topic, visit
To unsubscribe from this group and all its topics, send an email to

Mark A. Yoder

Jun 25, 2013, 3:50:31 PM6/25/13
Try setting the duty cycle to 0 before changing the period.


Jul 3, 2013, 6:00:03 AM7/3/13
I've succeeded in setting this EHRPWM_TEST flag. Do the following: download demo image for beaglebone black by following instructions from:
After that,  run script ./ After the building is done, in folder KERNEL, edit the file defconfig - find the flag CONFIG_EHRPWM_TEST, uncomment it and set to:

Now just rebuild the kernel with ./tools/
Finally insert your mmc car with previously installed ubuntu, set mmc variable in file (uncomment and set for example MMC=/dev/mmcblko) and run the script ./tools/ The kernel will be updated and now you can follow instructions from: or you can write:

echo am33xx_pwm > /sys/devices/bone_capemgr.*/slots
echo bone_pwm_P8_13 > /sys/devices/bone_capemgr.*/slots

echo <period> /sys/device/ocp/pwm_test_P8_13*/period
echo <duty> /sys/device/ocp/pwm_test_P8_13*/duty
Message has been deleted

Jul 3, 2013, 5:22:32 PM7/3/13
Just a small correction - to install kernel on mmc card run ./tools/ and not ./tools/

Sep 9, 2013, 9:29:05 AM9/9/13
$ echo bone_pwm_P8_13 > /sys/devices/bone_capemgr.*/slots
$ echo 20000000 > /sys/devices/ocp.*/pwm_test_P8_13.*/period 
$ echo 10000000 > /sys/devices/ocp.*/pwm_test_P8_13.*/duty
Wildcards are the best way to do this.

Oct 24, 2013, 4:33:40 PM10/24/13
John, did you ever figure out what Derek was referring to here? If so, could you post more info?

Nov 13, 2013, 8:14:06 PM11/13/13

I completely agree with your comments on there needing to be a C/C++ equivalent to the bonescript.  With beaglebone 3.8.13, I am able to successfully control a PWM actuator following Mark Yoder's example: (, almost exactly the procedure Michael Vernier posted above.  However, it will only work after I run a bonescript analogWrite() on the PWM pin.  It took some digging to reverse engineer the \bonescript-master\node_modules\bonescript\index.js::analogWrite() to determine what it is doing that afterwards allows me to successfully use the PWM device. 

The analogWrite sets the PWM polarity to zero during the configuration of the PWM:  "fs.writeFileSync(pwm_test+'/polarity', 0);"

A similar operation from my C++ side resolved my issue and allowed me to use the PWM device successfully: system("cd /sys/devices/ocp.2/pwm_test_P9_21.14; echo 0 > polarity");


On Friday, May 3, 2013 4:27:20 PM UTC-5, wrote:
Thanks a lot. I was able to get an LED to blink using Angstrom with these steps. Notably the Debian Squeeze install I made with the instructions here: only partially worked. The link for the PWM showed up but there were no "duty" and "period" files in the folder.

I was going to investigate reverse engineering bonescript but it wasn't clear to me that PWM was supported there in the first place. I think ideally there ought to be an official C/C++ library to complement the bonescript API. When I first booted the board I really expected there to be a libbeaglebone or some such thing for me to link to. I see a number of such libraries cropped up for the last revision of beaglebone but they didn't seem really seem to be promoted by the beagleboard folks. I would really like to encourage them to make some effort to make C/C++ a first class citizen when it comes to IO. At the very least I think it's important that reverse engineering bonescript is not the best option for getting going in C/C++. A simple cheat sheet of instructions like Michael presented linked on the website would be enough to feel like I was being supported.


William Hermans

Nov 14, 2013, 1:05:59 AM11/14/13
Well if you all need / want a C/C++ PWM library. Sounds like you all best get started :)


Jan 8, 2014, 6:36:00 AM1/8/14
I am new to Beaglebone black & embedded linux programming. I am looking for C/C++ library for PWM.

I am compiling the C++ library given by SaadAhmad but i am unable to run it. when i am compiling with arm-angstrom-linux-gnueabi-gcc, i am getting errors. And when i am compiling with g++ -c command, it is giving no errors, but unable to execute the binary file.

Any body got a C/C++ library for PWM & ADC.

JaeEun Choi

May 30, 2014, 11:27:51 PM5/30/14
i have problem of to find period folder


May 31, 2014, 6:30:31 AM5/31/14

I'm not really an expert, but the information I've picked up from reading various post is that not all kernels have full PWM support.  Are you on Ubuntu?

If your are missing period and duty then it is possible the Kernel was compiled without the flag CONFIG_EHRPWM_TEST.

I'm not sure about the fix, but I think you might need to recompile and uncomment the CONFIG_EHRPWM_TEST defconfig.

there are some clues in the posts above.

good luck,
Reply all
Reply to author
0 new messages