Pushbutton with micro:bit

Learn how to add an external pushbutton to the micro:bit

Written By: Cherie Tan

Dash icon
Difficulty
Easy
Steps icon
Steps
13
While the micro:bit already has two on-board push buttons, button A and button B, you can add more buttons to control the micro:bit. 

In this guide, you will learn to connect an external push button to the micro:bit, and get it to turn turn an LED on and off. The push button module is comprised of a momentary push button switch and an in-built resistor. 

After completing this guide, you will know how to add more buttons to your micro:bit which will come in handy when you go on to make all sorts of  projects.

Step 1 The Module

Before we put together the circuit, let's take a closer look at the Pushbutton Module. There are three pins here:

S : This is the signal pin which we will connect to a GPIO pin on the micro:bit

3.3V : Though it is unlabelled on the module, this middle pin will need to be connected to 3.3V on the micro:bit

GND: In electronics, we define a point in a circuit to be a kind of zero volts or 0V reference point, on which to base all other voltage measurements. This point is called  ground or GND.

Voltage is the difference in potential between two points. As it is difficult to talk about voltage without a reference point, we need another point to compare it to. 

Step 2 Connect module to breadboard

Step 3 Insert the Micro:bit into the Breakout board

Insert the micro:bit into the breakout board . Make sure that the micro:bit is inserted in the right orientation, with the buttons facing inward as shown.

Step 4 P0 to S

Step 5 Connect middle pin to 3.3V

Step 6 Connect GND to LED (Negative lead)

Step 7 Connect GND to -

Step 8 Add a resistor

Step 9 Connect P1 to LED

Step 10 On start block

pins.setPull(DigitalPin.P0, PinPullMode.PullNone)
pins.setEvents(DigitalPin.P0, PinEventType.Edge)
Now open up MakeCode editor
Click on 'New Project'
Copy and paste the following code into the Javascript interface.

Step 11 On Pin P0 Pressed

input.onPinPressed(TouchPin.P0, function () {
    pins.digitalWritePin(DigitalPin.P1, 1)
})
pins.setPull(DigitalPin.P0, PinPullMode.PullNone)
pins.setEvents(DigitalPin.P0, PinEventType.Edge)
Copy and paste this code into the Javascript interface in the MakeCode editor.

Step 12 On Pin P0 Released

input.onPinPressed(TouchPin.P0, function () {
    pins.digitalWritePin(DigitalPin.P1, 1)
})
input.onPinReleased(TouchPin.P0, function () {
    pins.digitalWritePin(DigitalPin.P1, 0)
})
pins.setPull(DigitalPin.P0, PinPullMode.PullNone)
pins.setEvents(DigitalPin.P0, PinEventType.Edge)
After testing out the previous code, replace it with this in the Javascript interface.

Step 13 Download the hex file

To transfer the code to the micro:bit, follow these steps. Connect the micro:bit to your computer using a microUSB cable
Click on the 'Download' button in MakeCode editor. It should be on the bottom left-hand corner.
Locate the downloaded .hex file in your 'Downloads' folder.
Drag and drop the .hex file to the MICROBIT drive. 
Test the code out! Press the push button and the LED should light up briefly. Let it go and it should stop lighting up.