Pymate-User-Button
User button
The as the name suggest it, the user button of Pymate is free for use.
The Pymate Board class offers access to various hardware resources like the user button, the user LEDs and the buses.
Inherited from MicroPython core, the switch is made available via the Switch class.
The user button can be programmed in two way:
- by reading its current state
- by attaching IRQ (interrupt request) routine.
The Switch class is documented on the micropython.org .
Coding
Reading value
The following script reads the state of the user button with switch.value() then apply the value to the user LED1.
switch.value() does returns 1 or 0 depending if the button is held down or not.
from pymate.board import Board
import time
brd = Board()
brd.led1.off()
while True:
brd.led1.write( brd.switch.value() )
time.sleep_ms(100)Interrupt Handler
In this example, the switch_callback() callback function can also be associated with the user buttton.
The callback function is called each time the Pymate detect a button press. The number of falling edge detected (button pressure) is showed in the REPL session.
As the callback call is based on Interrupt Request (IRQ) process, the script execution is suspended and callback triggered when a falling edge is detected on button pin.
switch_callback() is an interrupt handler and some rules applies to it:
- It cannot allocate memory,
- It must execute its content as fast as possible,
- It should avoids Exception raising.
See Writing Interrupt Handlers documentation.
from pymate.board import Board
import time
brd = Board()
pressed = 0 # Count the number of button press detected
def switch_callback():
global pressed
pressed += 1
brd.led1.off()
# register the callback
brd.switch.callback( switch_callback )
while True:
if pressed>0:
print( "pressed %i" % pressed )
pressed = 0
brd.led1.on()
time.sleep_ms(100)
brd.led1.off()
time.sleep_ms( 10 )Which show the following result in REPL
pressed 1 pressed 1 pressed 2 pressed 2 pressed 2 pressed 2 pressed 1 pressed 1 pressed 1 pressed 2 pressed 1 pressed 1 pressed 1 pressed 2 pressed 2 pressed 3
As shown in the result here above, multiple button press may be detected even when it is pressed once by the user.
This is due to the transient period where contacts are bouncing on each other before getting firmly connected together.
To avoids such issue, the code must debounce the input (reject second activation within the 10mS following initial activation).
Debounced Interrupt Handler
This revision of the Interrupt Handler script checks that subsequent interrupt call do not occurs within 10ms time frame.
from pymate.board import Board
import time
brd = Board()
pressed = 0 # Count the number of button press detected
last_pressed = time.ticks_ms() # Set it in the past
def switch_callback():
global pressed, last_pressed
if time.ticks_diff( time.ticks_ms(), last_pressed ) <= 10:
return
pressed += 1
last_pressed = time.ticks_ms()
brd.led1.off()
# register the callback
# See:
# https://docs.micropython.org/en/latest/library/pyb.Switch.html
brd.switch.callback( switch_callback )
while True:
if pressed>0:
print( "pressed %i" % pressed )
pressed = 0
brd.led1.on()
time.sleep_ms(100)
brd.led1.off()
time.sleep_ms( 10 )This time, the REPL always shows 1 activation.
pressed 1 pressed 1 pressed 1 pressed 1 pressed 1 pressed 1
PymateIO all right reserved © 2026 - Written by MCHobby for PymateIO