Read Duty Cycle of Incoming PWM Signal?

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
dpancoe
 
Posts: 18
Joined: Thu Sep 06, 2012 10:28 am

Read Duty Cycle of Incoming PWM Signal?

Post by dpancoe »

I am hoping to use a BBB in a PCBA test fixture and have figured out how to perform most of the functions I need from within Python using Adafruit_BBIO. One I am not sure about yet - I have to verify the duty cycle of a PWM signal generated by the Board Under Test (BUT). I need to reliably differentiate among multiple duty cycle values at ~10% spacing with a PWM frequency of 1 kHZ.

My questions:
• Would this work well enough using GPIO.RISING and GPIO.FALLING event detection, timers and callbacks?
• If yes, suggested examples would be appreciated.
• If no and it has to involve the PRUSS, suggested examples definitely appreciated!

Thanks,
Don

User avatar
drewfustini
 
Posts: 944
Joined: Sat Dec 26, 2015 1:19 pm

Re: Read Duty Cycle of Incoming PWM Signal?

Post by drewfustini »

The SoC in the BeagleBone is the AM3358 which has an eCAP peripheral which I believe can be used for this use case. Unfortunately, I have not used it myself.

I would suggest posting on the BeagleBoard.org mailing list to see if anyone else is currently using it and can provide additional details.
https://beagleboard.org/discuss

This reply by Robert Nelson may be of interest:
https://groups.google.com/d/msg/beagleb ... 8hnmSbCgAJ

User avatar
dpancoe
 
Posts: 18
Joined: Thu Sep 06, 2012 10:28 am

Re: Read Duty Cycle of Incoming PWM Signal?

Post by dpancoe »

Thank you for the information. I will have to look into the eCAP for the following reason.

In my own experimentation, it would seem that the BBIO event functions are not sufficient to measure the duty cycle of a 1kHz PWM signal, although I will have to look at the signal on an oscilloscope to be sure there is nothing else going on.

Here is the test code I used, but the supposed 1 kHz signal appears to have a frequency of about 500 to 600 Hz, and every duty cycle seems to be measured at about 50%. I also had a variant that used event callbacks rather than the "wait for edge" functions, but those results were similar.

Code: Select all

import Adafruit_BBIO.PWM as PWM
import Adafruit_BBIO.GPIO as GPIO
import time

pwmOut = "P9_22"
pwmIn = "P9_24"

PWM.start(pwmOut, 0, 1000)
GPIO.setup(pwmIn, GPIO.IN)

while (1):
        dutyCycleOut = input("Duty cycle to send (1 to 99): ")
        if dutyCycleOut < 1 or dutyCycleOut > 99:
                break
        PWM.set_duty_cycle(pwmOut, dutyCycleOut)
        time.sleep(.05)
        GPIO.wait_for_edge(pwmIn, GPIO.RISING)
        firstRise = time.clock()
        GPIO.wait_for_edge(pwmIn, GPIO.FALLING)
        midFall = time.clock()
        GPIO.wait_for_edge(pwmIn, GPIO.RISING)
        secondRise = time.clock()
        period = secondRise - firstRise
        pulseWidth = midFall - firstRise
        frequency = 1 / period
        dutyCycleIn = (pulseWidth / period) * 100.0
        print "Duty cycle: ", round(dutyCycleIn, 1), ", pulse width: ", pulseWidth, ", period: ", period, ", frequency: ", frequency
print("Duty cycle out-of-range, exiting program.")
PWM.stop(pwmOut)
PWM.cleanup()
GPIO.cleanup()

User avatar
dpancoe
 
Posts: 18
Joined: Thu Sep 06, 2012 10:28 am

Re: Read Duty Cycle of Incoming PWM Signal?

Post by dpancoe »

It occurs to me that the problem could be in the source of

Code: Select all

time.clock()
. Maybe I should try an update to Python 3.3 or greater so I could use

Code: Select all

time.perf_counter()
instead. What version of Python is included in the latest (10/7/18) build of Debian for BBB?

User avatar
drewfustini
 
Posts: 944
Joined: Sat Dec 26, 2015 1:19 pm

Re: Read Duty Cycle of Incoming PWM Signal?

Post by drewfustini »

That Debian image should have Python 3.5.

User avatar
dpancoe
 
Posts: 18
Joined: Thu Sep 06, 2012 10:28 am

Re: Read Duty Cycle of Incoming PWM Signal?

Post by dpancoe »

Hmmm, wonder why I got a "not a valid command" error or something similar when I tried to use time.perf_counter(). I will have to look into that further.

Thanks,
Don

User avatar
dpancoe
 
Posts: 18
Joined: Thu Sep 06, 2012 10:28 am

Re: Read Duty Cycle of Incoming PWM Signal?

Post by dpancoe »

As a follow-up, I have discovered libpruio, a library to handle the PRU GPIO including the eCAP pins. I ended up successfully testing with the C examples (pwm_cap.c) since I think I want to move my project in that direction, but they do have a set of Python examples as well. They released an updated version late last year and are active on the BeagleBoard Google Group.

Github:
https://github.com/DTJF/libpruio

Doxygen:
http://users.freebasic-portal.de/tjf/Pr ... index.html

Thanks,
Don

Locked
Please be positive and constructive with your questions and comments.

Return to “Beagle Bone & Adafruit Beagle Bone products”