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.
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)
Limitations: On RP2040,
Counteruses the PWM peripheral, and is limited to using PWM channel B pins due to hardware restrictions. See the pin assignments for your board to see which pins can be used.
- __exit__() None ¶
Automatically deinitializes the hardware when exiting a context. See Lifetime and ContextManagers for more info.