Buttons ======= There are two press buttons, A and B, on the upper edge of the mPython Board. A pressed button reads low level; otherwise it reads high level. The process of pressing buttons A and B on the mPython Board is described below. When a button is pressed, the level changes from high to low; the instant 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 instant 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. .. image:: /_static/products/mpython-s3/_static/image/tutorials/falling.png :align: center ------------------------------------------------------ Get the Button State +++++++++++++++++++++ .. literalinclude:: /_static/products/mpython-s3/_static/examples/button/button_ctl_led.py :caption: Example - press A to turn the LED on, press B to turn it off :linenos: Before use, import the mpython module:: from mpython import * Buttons A and B pressed:: button_a.value() == 0 # Button A pressed button_b.value() == 0 # Button B pressed .. Note:: ``button_a`` is the object name of button A, and button B is named ``button_b``. They are derived classes of ``machine.Pin`` and inherit the Pin methods, so the ``value`` function can be used to read the pin value: it returns ``1`` for a high-level signal and ``0`` for a low-level signal. Therefore, value==1 when the button is not pressed, and value==0 when it is pressed. Button Interrupts ++++++++++++++++++ .. admonition:: What is an interrupt? While a program is running, a situation arises that the CPU must handle immediately; the process in which the CPU temporarily suspends program execution to handle this new situation is called an interrupt. When needed, the CPU must pause what it is doing, handle something else, and then go back to the paused work after finishing. .. literalinclude:: /_static/products/mpython-s3/_static/examples/button/button_irq_ctl_led.py :caption: Example - press button A to turn on the onboard LED and buzzer, press button B to turn them off :linenos: .. Note:: In the program above, by default the program waits idly in the loop without executing any instruction. When an interrupt of button A or B is detected, the corresponding function is called back. ``button_a.irq(trigger=Pin.IRQ_FALLING, handler=ledon)`` invokes the function corresponding to the interrupt handler. `trigger` configures the event that can trigger the interrupt; the possible values are: `Pin.IRQ_FALLING` falling-edge interrupt; `Pin.IRQ_RISING` rising-edge interrupt; `Pin.IRQ_LOW_LEVEL` low-level interrupt; `Pin.IRQ_HIGH_LEVEL` high-level interrupt. `handler` is an optional function that is called when the interrupt is triggered, returning a callback object. For detailed usage, see :ref:`button_[a,b].irq`. .. Attention:: When defining an interrupt handler function, the function must contain **one** arbitrary parameter, otherwise it cannot be used. The parameter in the ledon() and ledoff() functions is ``_``.