Buttons

There are two press buttons A and B on the upper edge of the mPython Board. When a button is pressed, the level is low; otherwise, it is high.

The process of pressing buttons A and B on the mPython Board is described below. When pressed, the level changes from high to low. The moment when the high level (1) changes to the low level (0) is called the falling edge. When the button is released, the level changes from low to high. The moment when the low level (0) changes to the high level (1) is called the rising edge. We can get the current button state by detecting the level change.

../../../../../_images/falling.png

Get Button State

Example - press A to turn on LED, press B to turn off LED
 1from mpython import *
 2
 3while True:
 4    # Button A reads low level when pressed
 5    if button_a.value() == 0 :    
 6        # Button debounce delay
 7        sleep_ms(20)
 8        # Check again whether it is pressed
 9        if button_a.value()==0:
10            # Set to red
11            rgb[0] = (255,0,0)  
12            rgb[1] = (255,0,0)
13            rgb[2] = (255,0,0)
14        rgb.write()
15    # Button B reads low level when pressed
16    if button_b.value() == 0 :    
17        # Button debounce delay
18        sleep_ms(20)
19        # Check again whether it is pressed
20        if button_b.value()==0:
21            # Turn off the light
22            rgb[0] = (0, 0, 0)    
23            rgb[1] = (0, 0, 0)
24            rgb[2] = (0, 0, 0)
25            rgb.write()

Before use, import the mpython module:

from mpython import *

Button A and Button B pressed:

button_a.value() == 0      # Button A pressed
button_b.value() == 0      # Button B pressed

Note

button_a is the object name for Button A, and button_b is for Button B. They are derived classes of machine.Pin, inheriting Pin methods, so you can use the value function to read the pin value. A return of 1 indicates a high-level signal, and 0 indicates a low-level signal. Therefore, when the button is not pressed, value==1; when pressed, value==0.

Button Interrupt

What is an interrupt?

During program execution, if a situation arises that must be handled immediately by the CPU, the CPU temporarily suspends program execution to handle this new situation. This process is called an interrupt. When needed, the CPU must pause the current task, handle something else, and then resume the paused task after completion.

Example - press Button A to turn on the onboard LED and buzzer, press Button B to turn them off
 1from mpython import *     # Import the mpython module
 2import music              # Import the music module
 3
 4def ledon(_):             # Define the interrupt handler: turn on the light and buzzer
 5    rgb.fill((128,0,0))   # Turn on the onboard light, set all to red at half brightness
 6    rgb.write()           # Output the color to the light
 7    music.pitch(1000)     # Turn on the buzzer: 1000 Hz
 8
 9def ledoff(_):            # Define the interrupt handler: turn off the light and buzzer
10    rgb.fill((0,0,0))     # Turn off all onboard lights
11    rgb.write()           # Output the color to the light
12    music.pitch(0)        # Turn off the buzzer
13
14button_a.irq(trigger=Pin.IRQ_FALLING, handler=ledon)     # Set button A interrupt, triggered on falling edge, to turn on the light and buzzer
15
16button_b.irq(trigger=Pin.IRQ_FALLING, handler=ledoff)    # Set button B interrupt, triggered on falling edge, to turn off the light and buzzer
17
18while True:                            # When there is no interrupt, the program runs in this loop
19    pass

Note

In the above program, by default, the program waits idle in a loop without executing any instructions. When a button A or B interrupt is detected, the corresponding callback function is called.

button_a.irq(trigger=Pin.IRQ_FALLING, handler=ledon) calls the interrupt handler function. trigger configures the event that can trigger an interrupt. Possible values are: Pin.IRQ_FALLING for falling edge interrupt; Pin.IRQ_RISING for rising edge interrupt; Pin.IRQ_LOW_LEVEL for low-level interrupt; Pin.IRQ_HIGH_LEVEL for high-level interrupt. handler is an optional function called when the interrupt is triggered, returning a callback object. For detailed usage, refer to button_[a,b].irq.

Attention

When defining an interrupt handler function, the function must include one parameter, otherwise it cannot be used. The parameter in ledon() and ledoff() functions is _.