Micro:bit Experiment 02: Button —— Elecfreaks Mirco: Bit Starter Kit Course

by ElecFreaks in Circuits > Electronics

128 Views, 0 Favorites, 0 Comments

Micro:bit Experiment 02: Button —— Elecfreaks Mirco: Bit Starter Kit Course

2封面图片.jpg

In our previous Experiment 1 , we have learned how to use Micro:bit to control 2 LED beads 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, 2 LED beads flash in turns; release the button, LED beads stop flashing.

Components:

Hardware:

1 X Micro:bit Board

1 X Micro-B USB Cable

1 X Microbit Breadboard Adapter

1 X Transparent Breadboard - 83 * 55 mm

2 X LED

2 X 100 Ohm Resistors

1 X Momentary Pushbutton Switch

1 X Breadborad jumper wire 65pcs pack

Tips: If you are looking for all of the components above, you may need Elecfreaks Micro:bit Starter Kit.

Software:

Microsoft Makecode Online Editor

Introduction of Major Components

Momentary Pushbutton Switch

This is a common component for controlling electronic devices. It is mostly used to connect or cut off control circuit so that it can achieve motor or other electronic equipment control.

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

Momentary Pushbutton Switch has 4 footers which can be divided into 2 groups: footer 1and 2 in a group, footer 3 and 4 in a group.

Hardware Connection

Connect components as the picture showed below:

After connection, you will see:

Programming

Open Microsoft Makecode, write your code in editor window. We would like to suggest you try this code writing by yourself first.

Of course, you can see the whole program in the link below. Just click the Edit on the right top corner , and then click Download on the right bottom corner to download code into Micro:bit.

Code Explanation

on start:

An event that runs when the program 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 P0 (set to high) and the other end is connected to GND (0 volts), then when you press the button, P0 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 operate program in than. 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 read out high voltage, it shows 1. When read out low voltage, it presents 0.

Experiment Result:

When you press down the button, you can see 2 LED beads twinkle by turns; release the button, they stop flashing.

Question:

If we want to light red LED when press down the button and light green LED when release the button, then how to program? We would like to see your comments or further discussions with us.

Relative Readings: