PWM

119 views
Skip to first unread message

stl...@gmail.com

unread,
Jan 14, 2021, 5:51:46 PM1/14/21
to BeagleBoard
OK, I want to generate a PWM signal to send to a motor controller.  This is simply to communicate the desired speed, the motor controller will do all the heavy lifting.  

Unfortunately, the Adafruit_BBIO libraries appear broken and bonescript doesn't seem any better.

After digging through Google results I found myself in /sys/class/pwm/ using echo to write values into various files and through pure dumb luck was able to get a 2 Khz square wave up on a scope and change its duty cycle.  So at least my set up seems to be working at some level.  

However, I feel I am missing a bunch of information. Which pwmchip exports to which pwm-X:X and corresponds to which pin?  "duty_cycle" is not really duty cycle, it is nanoseconds high.  I can't change the period, even to sensible values like 250000 or 1000000.  Some of the information on Stack Exchange is worrying, will pwm-4:0 always correspond to P9_14 or can this change on boot?   

Is there a way to restore the functionality of the Adafruit BBIO libraries, short of waiting for an update?  I did pip3 install -U.  Requirement already satisfied: Adafruit_BBIO in /usr/local/lib/python3.7/dist-packages (1.2.0)

If that's not possible, is there any documentation on how the /sys/class/pwm is intended to function?

Thank you,
-Steve Lentz

jonnymo

unread,
Jan 14, 2021, 7:03:18 PM1/14/21
to Beagle Board
Which BeagleBoard are you using?

Jon

--
For more options, visit http://beagleboard.org/discuss
---
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 beagleboard...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/beagleboard/08130fba-0f75-464a-9041-d66a1c3a8cacn%40googlegroups.com.

Steve Lentz

unread,
Jan 14, 2021, 7:42:46 PM1/14/21
to Beagle Board
Black.  BeagleBoard.org Debian Buster IoT Image 2020-04-06.

FWIW, the Adafruit libraries let me control the pins as GPIO.


TY,
-Steve

jonnymo

unread,
Jan 14, 2021, 8:01:11 PM1/14/21
to Beagle Board
The SpreadSheet at the following link will show you which pins on the BB Black can be set for PWM.


As far as the Adafruit BBIO libs, have you gone through the example at the Adafruit site?

If this is not working, could you provide any errors or output of the issue you are seeing?

The output of the following might be helpful:
   /opt/scripts/tools/version.sh  

Cheers,

Jon
 

Steve Lentz

unread,
Jan 14, 2021, 9:28:34 PM1/14/21
to Beagle Board
OK, I rechecked and only Pin 8_13 is not working; the other three PWM pins are good using Adafruit.PWM.  Pin 8_13 does respond to GPIO.HIGH and GPIO.LOW, so I don’t think I’ve damaged the hardware.  Who knows what I have managed to do to the /sys/class files.  If it doesn’t clear up on restart, I will reinstall the image.  Of course 8_13 is the one I was planning to use, but I can just as well wire something to 8_19 for now.

Thanks for the help.

-Steve
  

randy cooper

unread,
Jan 15, 2021, 9:33:01 AM1/15/21
to BeagleBoard
P8_13 is working for me as a PWM output, and I am using the Adafruit BBIO library.   I’m using the latest image with LXQT (Debian 9.9 I believe).
Randy

Steve Lentz

unread,
Jan 15, 2021, 9:41:43 AM1/15/21
to Beagle Board
Thanks for that, I’ll look at it again today.  I may have mucked something up while digging into the /sys/class/pwm file structure or forgotten to do GPIO.cleanup() or some other combination of things.

-Steve

Steve Lentz

unread,
Jan 15, 2021, 10:25:35 AM1/15/21
to Beagle Board
OK, 3 of 4 PWM pins are working, but still no luck with P8_13.

I can see that Adafruit_BBIO is writing values into /sys/class/pwm/pwm-7:1/duty_cycle but no output is being generated.

debian@beaglebone:/sys/class/pwm/pwm-7:1$ cat duty_cycle
200000
debian@beaglebone:/sys/class/pwm/pwm-7:1$ cat period
500000

Pin 8_13 can still be used for GPIO. I have not dug further into the device files. Does this seem like a hardware or software issue? Should I reinstall the base image, accept that the board is flaky, or is there some other possible solution?

Also, the PWM frequency is fixed at 2000 Hz (period = 500000 ns). Is this intended? I can work with 2000, so this is more out of curiosity.

Python returns the following errors:

>>> PWM.start(pin3,50,1000)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: Problem with a sysfs file

>>> PWM.set_frequency(pin3,1000)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: Could not write to the frequency file

Attempting to write into the period file directly returns the following:

debian@beaglebone:/sys/class/pwm/pwm-7:1$ sudo echo 400000 > period
[sudo] password for debian:
echo: write error: Invalid argument

The arguments 2000 (from Python) and 500000 (echo) are accepted without no error. Any other values are not accepted.



Thanks again,
-Steve


> On Jan 15, 2021, at 9:33 AM, randy cooper <RCo...@lanepilot.com> wrote:
>
> To view this discussion on the web visit https://groups.google.com/d/msgid/beagleboard/8d4b5048-1469-4557-bafa-1c8680aa39b1n%40googlegroups.com.

Robert Nelson

unread,
Jan 15, 2021, 10:34:58 AM1/15/21
to Beagle Board, Famous Grouse
On Fri, Jan 15, 2021 at 9:25 AM Steve Lentz <stl...@gmail.com> wrote:
>
> OK, 3 of 4 PWM pins are working, but still no luck with P8_13.
>
> I can see that Adafruit_BBIO is writing values into /sys/class/pwm/pwm-7:1/duty_cycle but no output is being generated.
>
> debian@beaglebone:/sys/class/pwm/pwm-7:1$ cat duty_cycle
> 200000
> debian@beaglebone:/sys/class/pwm/pwm-7:1$ cat period
> 500000
>
> Pin 8_13 can still be used for GPIO. I have not dug further into the device files. Does this seem like a hardware or software issue? Should I reinstall the base image, accept that the board is flaky, or is there some other possible solution?
>
> Also, the PWM frequency is fixed at 2000 Hz (period = 500000 ns). Is this intended? I can work with 2000, so this is more out of curiosity.
>
> Python returns the following errors:
>
> >>> PWM.start(pin3,50,1000)
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> RuntimeError: Problem with a sysfs file
>
> >>> PWM.set_frequency(pin3,1000)
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> RuntimeError: Could not write to the frequency file
>
> Attempting to write into the period file directly returns the following:
>
> debian@beaglebone:/sys/class/pwm/pwm-7:1$ sudo echo 400000 > period
> [sudo] password for debian:
> echo: write error: Invalid argument

That's invalid, "sudo echo....."

sudo sh -c "echo '400000' >> ./period"

Besides, pwm should be "root:gpio" permissoins..

Regards,


--
Robert Nelson
https://rcn-ee.com/

Steve Lentz

unread,
Jan 15, 2021, 2:43:22 PM1/15/21
to Robert Nelson, Beagle Board
I’ve investigated a bit more.  First, and I should have done this sooner, I removed all the capes I’m using and tested the bare Beaglebone black.  Success, all four PWMs work.

Then I began adding capes one at a time.  I found that when a Prototype Cape is used, PWM is no longer available on P8_13.  To be absolutely certain, I connected a brand new out of the box GHI Prototype Cape.  No PWM on P8_13.  Remove the cape and PWM works.  

If I connect a Power Cape, PWM works, this would seem to rule out a bad solder joint or something physical on the BBB.    

There is nothing near P8_13 on any of these capes, yet something is affecting that pin.  

Is there something in the device tree that allows the cape to grab one of the PWM devices, perhaps for the variable LED that’s on the cape?

Any other ideas what would cause this?

Thanks,
-Steve



On Jan 15, 2021, at 10:54 AM, Steve Lentz <stl...@gmail.com> wrote:

Robert:

Thanks.  

Still returns an error

debian@beaglebone:/sys/class/pwm/pwm-7:1$ sudo sh -c "echo '400000' >> ./period"
[sudo] password for debian:
sh: 1: echo: echo: I/O error
debian@beaglebone:/sys/class/pwm/pwm-7:1$ cat period
500000

I can live with 2000 Hz, I’m just curious as to why this seems to be the only allowed value for PWM frequency.  Is this intentional or is something missing from the PWM driver?

Adafruit’s library does some odd things, for example the following results in a 2000 Hz signal with 80% duty cycle:

>>> import Adafruit_BBIO.PWM as PWM
>>> pin3 = "P8_19"
>>> PWM.start(pin3, 25)
>>> PWM.set_frequency(pin3,1000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: Could not write to the frequency file
>>> PWM.set_duty_cycle(pin3,40)

The error makes it clear 2000 Hz was not accepted, but Python carries on thinking base frequency is now 1000 Hz and sets the duration of signal high accordingly.

At least people should be aware of this if they try to use it.  

-Steve

Robert Nelson

unread,
Jan 15, 2021, 2:59:26 PM1/15/21
to Steve Lentz, Beagle Board
On Fri, Jan 15, 2021 at 1:43 PM Steve Lentz <stl...@gmail.com> wrote:
>
> I’ve investigated a bit more. First, and I should have done this sooner, I removed all the capes I’m using and tested the bare Beaglebone black. Success, all four PWMs work.
>
> Then I began adding capes one at a time. I found that when a Prototype Cape is used, PWM is no longer available on P8_13. To be absolutely certain, I connected a brand new out of the box GHI Prototype Cape. No PWM on P8_13. Remove the cape and PWM works.
>
> If I connect a Power Cape, PWM works, this would seem to rule out a bad solder joint or something physical on the BBB.
>
> There is nothing near P8_13 on any of these capes, yet something is affecting that pin.
>
> Is there something in the device tree that allows the cape to grab one of the PWM devices, perhaps for the variable LED that’s on the cape?
>
> Any other ideas what would cause this?

Do you have a part number for the Prototype Cape used?

Sorry it doesn't ring a bell for me..

Steve Lentz

unread,
Jan 15, 2021, 6:40:07 PM1/15/21
to Robert Nelson, Beagle Board

Steve Lentz

unread,
Jan 15, 2021, 9:34:21 PM1/15/21
to Robert Nelson, Beagle Board
Robert:

I found a useful article on device tree overlays here: http://www.ofitselfso.com/BeagleNotes/Beaglebone_Black_And_Device_Tree_Overlays.php

After uncommenting the line disable_uboot_overlay_addr3=1 in /boot/uEnv.txt, PWM is working on pin 8_13, even with the Prototype Cape attached.

So I believe something in the overlay stored in EEPROM on this particular cape is interfering with PWM.  Here is a link to the cape I purchased in case you want to investigate further:  https://www.mouser.com/ProductDetail/958-PROCPE-BBBCAPE/

Disabling loading of cape’s overlay is a workable solution for me, I am using it only to hold some passive circuits and optocouplers, so I am happy with this approach.  

Thanks again.

Rgds,
-Steve Lentz



On Jan 15, 2021, at 2:58 PM, Robert Nelson <robert...@gmail.com> wrote:

TJF

unread,
Jan 16, 2021, 2:51:09 AM1/16/21
to BeagleBoard
stl...@gmail.com schrieb am Donnerstag, 14. Jänner 2021 um 23:51:46 UTC+1:
However, I feel I am missing a bunch of information. Which pwmchip exports to which pwm-X:X and corresponds to which pin?

There're 20 pins for hardware PWM output on BBB (11 on Pocket). Find a list (including hardware limits) at


Note: In order to benefit from all that capabilities, you've to use libpruio (Phyton binding available).

Regards
Reply all
Reply to author
Forward
0 new messages