countio – Support for edge counting
countio module contains logic to read and count edge transitions
For more information on the applications of counting edges, see this Learn Guide on sequential circuits.
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
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.
- class countio.Counter(pin: microcontroller.Pin, *, edge: Edge = Edge.FALL, pull: digitalio.Pull | None = 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
fallmust be True. The default is to count only falling edges, and is for historical backward compatibility.
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
import board import countio # Count rising edges only. pin_counter = countio.Counter(board.D1, edge=countio.Edge.RISE) # Reset the count after 100 counts. while True: if pin_counter.count >= 100: pin_counter.reset() print(pin_counter.count)
- __exit__() None
Automatically deinitializes the hardware when exiting a context. See Lifetime and ContextManagers for more info.