0

Counting pulses
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Counting pulses

by johnd2 on Tue Feb 16, 2021 8:36 am

Is there some sample code somewhere please for how to count pulses on an input pin in CircuitPython? I'd normally expect to use interrupts but AFAIK these aren't currently supported by CP.

johnd2
 
Posts: 2
Joined: Tue Feb 16, 2021 8:32 am

Re: Counting pulses

by dastels on Tue Feb 16, 2021 1:29 pm

What frequency are the pulses? Keep in mind that CircuitPython is slow compared to C++ when it comes to things happening in machine time (as opposed to people time). And what is generating the pulses? How noisy are the edges?

pulseio might do the job for you.

Dave

dastels
 
Posts: 5394
Joined: Tue Oct 20, 2015 3:22 pm

Re: Counting pulses

by tannewt on Tue Feb 16, 2021 2:45 pm

What board are you using? The `countio` module is likely what you need: https://circuitpython.readthedocs.io/en ... index.html

The download page for each board on circuitpython.org lists what native modules are in a board's build. countio will be listed there if it is supported.

tannewt
 
Posts: 2189
Joined: Thu Oct 06, 2016 8:48 pm

Re: Counting pulses

by johnd2 on Tue Feb 16, 2021 4:20 pm

Thanks for the answers. Was enquiring to use with a Pico/RP2040 board for a weather station application. The pulses would be slow for wind speed - no more than 50Hz as a maximum. And for rainfall - many days zero! And in a monsoon probably 0.1Hz maximum!

So, out of curiosity, is countio using interrupts internally? Is it just that they're not publicly implemented in CP yet? Or is it using some other counting technique that I can't quite envision?

johnd2
 
Posts: 2
Joined: Tue Feb 16, 2021 8:32 am

Re: Counting pulses

by tannewt on Tue Feb 16, 2021 5:49 pm

johnd2 wrote:So, out of curiosity, is countio using interrupts internally? Is it just that they're not publicly implemented in CP yet? Or is it using some other counting technique that I can't quite envision?


It varies. I suspect we'll be able to use PIO for it internally on the RP2040. On other ports we may use interrupts though. We don't have a python level API for interrupts because they are complicated and hard to do correctly.

tannewt
 
Posts: 2189
Joined: Thu Oct 06, 2016 8:48 pm

Please be positive and constructive with your questions and comments.