Computer 2

Monday, March 23, 2026

Micro:bit Buttons and Accelerometer Input

Agenda

  1. Learn how to detect button presses on the Micro:bit.
  2. Read the accelerometer to detect left and right tilt.
  3. Create a program that uses button and tilt combinations.

Reading Button Input

Control the LED matrix with buttons A and B.

The Micro:bit has two main buttons: A and B. In Python, you can check whether a button is being pressed with button_a.is_pressed() or button_b.is_pressed(). These expressions are either True or False, which makes them perfect for use in an if statement.

Because button presses can happen at any time, you will usually place your button checks inside a while True loop so the Micro:bit keeps watching for input.

Basic Example

from microbit import *

while True:
    if button_a.is_pressed():
        display.show(Image.HEART)
    elif button_b.is_pressed():
        display.show(Image.HAPPY)
    else:
        display.clear()

In this program, pressing button A shows a heart. Pressing button B shows a happy face. If neither button is pressed, the display turns off.

Reading the Accelerometer

Control the LED matrix with tilt values.

The Micro:bit accelerometer measures motion and tilt. One useful value is the x reading, which tells you whether the Micro:bit is tilted left or right.

You can read that value with accelerometer.get_x(). Positive values mean tilted to the right, negative values mean tilted to the left, and values near zero mean mostly level.

Example: Tilt Detection

This example reads the accelerometer and shows a letter for right tilt, left tilt, or level. It is the same pattern as your accelerometer.py file.

from microbit import *

while True:
    reading = accelerometer.get_x()
    if reading > 20:
        display.show("R")
    elif reading < -20:
        display.show("L")
    else:
        display.show("-")

The code compares against 20 and -20 instead of zero to create a dead zone in the middle so the display does not flicker constantly when the Micro:bit is almost flat.

Example: Combine Buttons and Tilt

You can mix button checks with accelerometer checks to build more interactive behavior.

from microbit import *

while True:
    reading = accelerometer.get_x()

    if button_a.is_pressed() and reading > 20:
        display.show(Image.ARROW_E)
    elif button_a.is_pressed() and reading < -20:
        display.show(Image.ARROW_W)
    elif button_b.is_pressed():
        display.show(Image.HAPPY)
    else:
        display.clear()

Order still matters. The most specific combinations should go first, then the more general cases.

Using Your Own Images

Just as in the movie project, you are not limited to the built-in images. You can create your own image variables and show them inside the if statements.

from microbit import *

diamond = Image('00900:'
                '09090:'
                '90009:'
                '09090:'
                '00900')

square = Image('99999:'
               '90009:'
               '90009:'
               '90009:'
               '99999')

while True:
    if button_a.is_pressed():
        display.show(diamond)
    elif button_b.is_pressed():
        display.show(square)
    else:
        display.clear()

This gives you a simple interactive program: button A shows one picture, and button B shows a different one.

Classwork: Buttons + Tilt Picture Board

Show different images using both buttons and accelerometer values.

Assignment

Write a Micro:bit program that shows different images based on combinations of button presses and tilt direction.

Requirements

Save your work as lastname_button_tilt.py and upload it to your OneDrive folder by the end of class.

If you finish early, try adding more combinations or creating more of your own images. You can also check out the example programs here and try to figure out how they work: c2examples.