Pushbutton with micro:bit


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.

Parts Used in This Guide


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.
    • Note: 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 -

Step 7  Connect GND to LED (Negative lead)

Step 8  Add a resistor

Step 9  Connect P1 to LED

Step 10  On start block

Now open up MakeCode editor and click on 'New Project'!

  • Click on Pins tab, and then click on '... more'.  
  • Drag and drop a 'set pull pin ... to ...' to the 'on start' block
  • Change it so that it is 'set pull pin P0 to none'. 
  • Grab a 'set pin ... to emit ... events ' block
  • Change it to:  set pin P0 to emit edge events.

Step 11  On Pin P0 Pressed

  • From Input tab, drag and drop a 'on Pin P0 pressed' block. 
  • Then grab a 'digital write pin ... to ...' block from Pins tab. 
  • Place this in 'on Pin P0 pressed' block.
  • Change its values to P1 and 1 respectively.

Step 12  On Pin P0 Released

  • From Input tab, click on '... more' and drag and drop a 'on Pin P0 released' block.
  • Then grab a 'digital write pin ... to ...' block from Pins tab. 
  • Place this in 'on Pin P0 released' block.
  • Change its values to P1 and 0 respectively.

Step 13  Download the hex file

To transfer the code to the micro:bit, follow these steps:

  1. Connect the micro:bit to your computer using a microUSB cable
  2. Click on the 'Download' button in MakeCode editor. It should be on the bottom left-hand corner.
  3. Locate the downloaded .hex file in your 'Downloads' folder.
  4. 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!