Lesson 02 – Button

Introduction

In the first lesson, we learned how to control 2 LEDs and make them flash alternatively. In this Lesson we are going to use a tactile button to control the LED flash. That means when we press and hold down the button, the two LEDs will flash alternatively. When the button is then released, the LEDS will then stop flashing. This is a great example of how we can use switches to control certain aspects of our circuitry giving us complete control.

Component List

  • micro:bit board
  • Micro USB Cable
  • micro:bit breadboard adaptor
  • Breadboard
  • LED x2
  • 100 ohm resistors x2
  • Male to male jumper wires
  • momentary push button switch

Pushbutton Switch

This is a common component for controlling electronic devices. It is mostly used to connect or cut off control circuits, so that it can be used to control motors or other electronic equipment. Momentary pushbutton switch usually stays open by default. When it is pressed down, the circuit gets connected and stays connected; when it is released, it will bounce back to the default status (open).

Momentary pushbutton switch has 4 pins, which can be divided into 2 groups: pin 1 is connected to pin 2, pin 3 is connected with pin 4.

Hardware Connection

Connect your components according to the breadboard diagram below:

After completing the circuit, you should see something like the below image:

Programming

Click to open up Microsoft Makecode, then add the following code to the edit area:

You can see the whole program in the page below. Click “Edit” on the right top corner, and then “download”, you can download your code into Micro:bit.

Here is the link to the whole program: https://makecode.microbit.org/_0JjR7AJpDJW4

Code Explanation

on start

An event that runs when the program first starts. The on start is a special event that runs when the program starts, before any other event. Use this event to initialize your program.

set pull

Configure the electrical pull of the specified pin. Many micro:bit pins can be configured as pull-ups. For example, a pull-up can set a pin’s voltage to high (3.3 volts, or 1 when calling digital read pin). If one end of a button is connected to pin 0 (set to high) and the other end is connected to GND (0 volts), then when you press the button, pin 0 is driven to 0 volts, and the micro:bit software can detect a button press.

if

If starts a condition judgement sentence. When the expression behind if is true (or 1), then it will operate the program within. When the expression behind if is false (or 0), it will jump out of the program circulation. Here is If sentence structure:

digital read

Digital read to read the voltage of pin. When reading high voltage, it shows 1. When reading low voltage, it presents 0.

Results

When you press down the button, you can see 2 LEDs flashing by turns; release the button, and they stop flashing.

Taking it further

If we want to light a red LED when pressing down on the button and light a green LED when releasing the button, then how would you program this?

micro:bit Starter Kit Lessons

  • Lesson 01 – LED
  • Lesson 02 – Button
  • Lesson 03 – Trimpot
  • Lesson 04 – Photocell
  • Lesson 05 – RGB LED
  • Lesson 06 – Self-lock Switch
  • Lesson 07 – Temperature Sensor
  • Lesson 08 – Servo
  • Lesson 09 – Buzzer
  • Lesson 10 – Motor
  • Lesson 11 – Rainbow LED
  • Lesson 12 – Accelerometer
  • Lesson 13 – Compass
  • Lesson 14 – Ambient Light
First published at 6:33pm on July 8, 2019
Last updated at 6:33pm on July 8, 2019