countio
– Support for edge counting¶
The countio
module contains logic to read and count edge transistions
All classes change hardware state and should be deinitialized when they
are no longer needed if the program continues after use. To do so, either
call deinit()
or use a context manager. See
Lifetime and ContextManagers for more info.
Available on these boards
- class countio.Edge¶
Enumerates which signal transitions can be counted.
Enum-like class to define which signal transitions to count.
- RISE :Edge¶
Count the rising edges.
- FALL :Edge¶
Count the falling edges.
- RISE_AND_FALL :Edge¶
Count the rising and falling edges.
- class countio.Counter(pin: microcontroller.Pin, *, edge: Edge = Edge.FALL, pull: Optional[digitalio.Pull] = None)¶
Count the number of rising- and/or falling-edge transitions on a given pin.
Create a Counter object associated with the given pin that counts rising- and/or falling-edge transitions. At least one of
rise
andfall
must be True. The default is to count only falling edges, and is for historical backward compatibility.- Parameters
pin (Pin) – pin to monitor
edge (Edge) – which edge transitions to count
pull (Optional[digitalio.Pull]) – enable a pull-up or pull-down if not None
For example:
import board import countio # Count rising edges only. pin_counter = countio.Counter(board.D1, edge=Edge.RISE) # Reset the count after 100 counts. while True: if pin_counter.count >= 100: pin_counter.reset() print(pin_counter.count)
- count :int¶
The current count in terms of pulses.
- __exit__() None ¶
Automatically deinitializes the hardware when exiting a context. See Lifetime and ContextManagers for more info.