Micro:bit MicroPython: Getting the status of the buttons

In this tutorial we will check how to interact with the two buttons of the Micro:bit board, using MicroPython.

Introduction

In this tutorial we will check how to interact with the two buttons of the Micro:bit board, using MicroPython.

The board has two buttons in the front (button A and button B) which can be used in the development of the application that will run on the Micro:bit [1].

MicroPython offers a very simple to use interface to interact with both these buttons and check if they are /were pressed. As we will see below, we have available very high level methods, which means we don’t need to worry about handling bouncing effects of the buttons or interrupts.

You can check here the documentation for these functionalities.

The code

We will start by importing the microbit module, which will make available the objects we need to interact with the buttons from the Micro:bit board.

import microbit

In this module there are two instances of the Button class that we can use to obtain the status of the buttons. Since the Micro:bit board has two buttons (button A and button B), there are two instances of the Button class representing them (named button_a and button_b, respectively) [2].

To check if a button is currently being pressed, we simply need to call the is_pressed method on the Button instance we want to analyze. This method takes no arguments and returns a Boolean value indicating if the button is currently being pressed.

microbit.button_a.is_pressed()

microbit.button_b.is_pressed()

Figure 1 shows all the possible outcomes of calling this method for both buttons. In the first invocation for button A, the button was not being pressed at the time, which is why it returned the value False.

On the second invocation I was already pressing button A, which is why the is_pressed method returned True.

Then I’ve done the same for button B. In the first invocation I was not pressing the button, which is why the value False was obtained, and in the second invocation I was pressing the button, which is why the value True was returned.

Getting the current status of a button of the Micro:bit, using MicroPython.
Figure 1 – Getting the current status of a button of the Micro:bit

There’s also another very interesting method on the Button class which is the was_pressed.

This method takes no arguments and it will return True if the button was pressed since the Micro:bit started or since the last time the method was called. Otherwise, it will return False [3].

When we call this method, the “pressed state” will be cleared. This means that we need to press the button again for the method to return True again [3]. In other words, if we press the button once and we call the was_pressed method twice, only the first invocation will return True and the second one will return False.

microbit.button_a.was_pressed()

Figure 2 illustrates a test to this method. The first thing I did before sending any command was pressing button A. Then I called the was_pressed method and, as can be seen, it returned True.

After that I’ve invoked this method two more times without pressing the button before. Consequently, the method now returned False both times. This happened because the first invocation cleared the “pressed state”, like we already mentioned.

Finally, I’ve clicked button A again and called the method one last time. Then it returned True again, as expected.

Testing if a button from the Micro:bit board was pressed, using MicroPython
Figure 2 – Testing if a button was pressed.

To finalize, there’s one additional method mentioned in the documentation that can be useful. This method is called get_presses and it returns the total number of button presses since this method was last called [4]. Like before, a call to the method will reset the counter and next calls without clicking the button will return 0.

microbit.button_b.get_presses()

Figure 3 illustrates an example of calls to this method. The first thing I did was clicking the button 3 times. Then, I have called the get_presses method, which returned exactly the number 3.

After that I called the method again, without pressing the button again. This time, it returned the value 0.

Then I’ve pressed the button 2 more times and called the method afterwards. It returned the value 2, as expected.

Getting the number of presses on a button from the Micro:bit, using MicroPython.
Figure 3 – Getting the number of presses on a button.

References

[1] https://tech.microbit.org/hardware/

[2] https://microbit-micropython.readthedocs.io/en/latest/button.html#buttons

[3] https://microbit-micropython.readthedocs.io/en/latest/button.html#Button.was_pressed

[4] https://microbit-micropython.readthedocs.io/en/latest/button.html#Button.get_presses

Leave a Reply

%d bloggers like this: