Page 1 of 1

PWM dutycycle read in Circuitpython.

Posted: Thu Aug 18, 2022 8:35 pm
by Kc2021
HI there, I have a script running in the pi pico using Micropython but I need it in Circuitpython. I have try several ways to read the duty cycle of a RC receiver using Circuit python but I can't make it work. These is the Micropython version. Can someone make it work in Circuit python pease?

from machine import Pin, PWM
import time

input_pin1 = Pin(17, Pin.IN) # this is GP17

while True:
for duty1 in range (3):
while input_pin1.value() == 0:
pass
start = time.ticks_us()
while input_pin1.value() == 1:
pass
end = time.ticks_us()
duty1 = end - start
print (str(duty1))

Re: PWM dutycycle read in Circuitpython.

Posted: Fri Aug 19, 2022 2:42 am
by rooppoorali

Re: PWM dutycycle read in Circuitpython.

Posted: Fri Aug 19, 2022 7:15 am
by Kc2021
Hi, thanks for your reply. The article explains how to output PWM while I am attempting to read PWM duty cycle.

Re: PWM dutycycle read in Circuitpython.

Posted: Fri Aug 19, 2022 9:14 am
by Rcayot
you can use a pin on the PICO with either pulseio, or countio. The pulseio will report the times of pulses, countio will tell you how many pulses (use time to get frequency)

Roger

Re: PWM dutycycle read in Circuitpython.

Posted: Fri Aug 19, 2022 9:34 am
by Kc2021
That actually may work, I will try it. Thanks

Re: PWM dutycycle read in Circuitpython.

Posted: Fri Aug 19, 2022 3:25 pm
by Rcayot
KC,

I have used both for monitoring the tachometer output of a fan controlled by PWM.

Here is the code I use with countio:

Code: Select all

pin_counter = countio.Counter(board.GP1, edge=countio.Edge.RISE)
async def fanspeed(interval):
    initial = time.monotonic()
    pin_counter.reset()
    while True:
        if time.monotonic() - initial >= fan_speed_time:
            revs = (pin_counter.count)/2
            rps = revs/(time.monotonic() - initial) #fan_speed_time
            fan_speed = str(rps*60)
            print("fan speed = ", fan_speed)
            F2tag =str(8)
            F2packet = str(F2tag + fan_speed) 
            initial = time.monotonic()
            pin_counter.reset()
            if not rfm9x.send_with_ack(F2packet.encode("utf-8")):
                print("No Ack F")
        await asyncio.sleep(interval)
Be aware that for countio to work, you need to put it on an ODD number pin. That took me a while to figure out! Also, you will need the latest version of circuit python. The version I loaded on my PICO when I got is did not support these functions.

Good luck. Hope it works.

Roger