PWM dutycycle read in Circuitpython.

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Post Reply
User avatar
Kc2021
 
Posts: 25
Joined: Mon Jul 19, 2021 11:42 am

PWM dutycycle read in Circuitpython.

Post 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))

User avatar
rooppoorali
 
Posts: 98
Joined: Sat Jul 16, 2022 12:04 pm

Re: PWM dutycycle read in Circuitpython.

Post by rooppoorali »


User avatar
Kc2021
 
Posts: 25
Joined: Mon Jul 19, 2021 11:42 am

Re: PWM dutycycle read in Circuitpython.

Post by Kc2021 »

Hi, thanks for your reply. The article explains how to output PWM while I am attempting to read PWM duty cycle.

User avatar
Rcayot
 
Posts: 284
Joined: Sat Feb 08, 2020 6:48 pm

Re: PWM dutycycle read in Circuitpython.

Post 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

User avatar
Kc2021
 
Posts: 25
Joined: Mon Jul 19, 2021 11:42 am

Re: PWM dutycycle read in Circuitpython.

Post by Kc2021 »

That actually may work, I will try it. Thanks

User avatar
Rcayot
 
Posts: 284
Joined: Sat Feb 08, 2020 6:48 pm

Re: PWM dutycycle read in Circuitpython.

Post 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

Post Reply
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”