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. .. image:: /_static/products/ledong-v2/_static/image/tutorials/falling.png :align: center ---------------------------------------------- Get Button State ++++++++++++++++ .. literalinclude:: /_static/products/ledong-v2/_static/examples/button/button_ctl_led.py :caption: Example - press A to turn on LED, press B to turn off LED :linenos: 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 ++++++++++++++++ .. admonition:: 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. .. literalinclude:: /_static/products/ledong-v2/_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 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 :ref:`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 ``_``.