Free Trial

Chapter 3

How to Code Micro Bit Buttons as Inputs

The micro bit can accept various physical inputs using either its buttons or sensors.

Inputs is used to trigger the codes to run.

In this exercise, you will explore how to use button inputs run the codes.

We will also be covering variables (algebra) in this exercise.

• Button Inputs
• What is Variables?
• Create a New Variables
• Changing the Variable
• Project - Counter

Duration: 30 mins

micro bit chapter 3

What You Need

1 x BBC micro:bit
1 x AAA battery pack
1 x USB to MicroUSB cable
1 x Laptop/PC
1 x Micro:Bit Auto Uploader (Software)

micro bit
usb cable
battery pack

Buttons on Micro Bit

The micro bit has three buttons in total.

• Button A at the left side
• Button B at the right side
• Reset Button at the back side

The micro bit can also detect the pressing of the button A + B together.

Pressing the reset button will restart the micro bit and force it to re-run its codes from the beginning.

micro bit buttons

On Button Pressed

The on button pressed code block will run the codes nested inside it when the microbit detected that the button is pressed.

You can select wither A, B or A+B.

micro bit button code block

Lets build a simple project that:

• display A when button A is pressed
• display B when button B is pressed
• display C when button A+B is pressed

Download the codes to the micro bit and play with the buttons.

button code block example

Create a New Variable

Before we move on with our project, we need to learn how to create a variable.

A variable is an algebra that store values (numbers) in it.

It is very important for coding as variable to used to count how much money you have in your bank, how many fruits is harvested etc.

We will start off by going through the steps to create a new variable "roboto" (you can name it whatever you like).

make a variable

Step 1: Click Make a Variable

variable name

Step 2: Enter the name of your variable

variable roboto

Step 3: New variable will appear

Controlling the Variable

There are two things we can do to the variable created.

1) Set the variable to a target value (number).

2) Change the variable value by a certain number (+1 , -1 or +5 etc)

ccontrolling variables

Project - Digital Counter

Let's use whatever that we have learnt so far to create a micro bit project.

In this project, we will create a digital counter that:

1) forever display a number
2) starting value of the number is 0
3) when button A is pressed - increase the number by 1
4) when button B is pressed - decrease the number by 1
5) when button A + B is pressed - set the number back to 0

Such a counter can be used to count the number of customers into the cinemas etc.

First, make a new variable. Name it counter or whatever you want.

Use forever loop and show number to display the variable continuously.

Use on button pressed inputs to change the variable value as per the project.

Remember to set the counter to value 0 before entering the forever loop.

Well Done!

Learn how to code your project using buttons as inputs in the next chapter.