2

Introduction:


In our previous experiment , we have learned how to use Micro:bit to control 2 LEDs and make them twinkle alternatively. This time we are going to use a button to control LED flash. That means when we press down the button,the 2 LEDs flash in turns; when release the button, the LEDs stop flashing.

Components List:


Hardware:

Tips: If you want to buy all components above, you may need Elecfreaks Micro:bit Starter Kit.

Major Components Introduction


Momentary Pushbutton Switch

This is a common component for controlling electronic devices. It is mostly used to connect or cut off the control circuit, it can achieve the control for motors or other electronic equipments.

Momentary Pushbutton Switch usually stays on. When it is pressed down, the circuit is connected; when it is released, it will bounce back to the status of disconnection.

Momentary Pushbutton Switch has 4 pins which can be divided into 2 groups: pin 1 short connects pin 2, pin 3 short connects pin 4.

Experimental Procedure


Hardware Connection

Connect your components according to the picture below:

You would see as below after you finish the connection:

Software Programming

Click to open Microsoft Makecode, write the following code in the editor.

Program as the picture shows:

Details for the code:

Reference

Links: https://makecode.microbit.org/_T585WeYwVWtv

You can also download the links directly:


Result


When the button is pressed, you can see the 2 LEDs flashing alternatively; When the button is released, they would stop flashing. If not, you need to go back and check your operations.

Exploration


If we want to light on the red LED when press the button and light on the green LED when release the button, how can we program?

常见问题FAQ