Buttons

Contents

2.7. Buttons#

The A and B buttons on the micro:bit have dedicated objects:

  • microbit.button_a

  • microbit.button_b

Both are instances of the Button class which have the following methods:

  • is_pressed() returns True while the button is held down

  • was_pressed() returns True if the button was pressed since the last time the method was called or since boot

  • get_presses() returns the total number of button presses and resets the count

2.7.1. Example#

from microbit import *

while True:
    display.clear()

    if button_a.is_pressed():
        display.set_pixel(0, 0, 9)

    if button_b.is_pressed():
        display.set_pixel(4, 0, 9)

    sleep(100)