4. case 02 button#

4.1. Introduction#

We learned about how to use micro:bit to let LED flashing in the previous case.

In this case, we are going to use a button to control the flashing LED.

4.3. Hardware Connect#


Connect circuit as above picture and put 2 AAA batteries to the batteries pack.

4.4. Principles of Circuits#


The GND of slot on micro:bit is into innards of batteries’ GND to generate current loop. When the button pressed, the circuit connects, P2 port of the micro:bit connects GND and level of P2 be pulled down.

4.5. Introduction of Components#


This is a common component used to control electronic devices. It is used to connect or cut electrical circuits.
This momentary push button switch is usually open. When it’s pressed down, it completes the circuit and turns the device on. When it’s released, it bounces back to the original state of disconnect. There is a button with blue hat on the experiment box.


4.6. Software#

Step 1#

Click makecode https://makecode.microbit.org/#

Click on “New Project” and set a new Project.


Step 2#

Snap the set pull pin in to the on start and set pull pin P2 to up to let its default be 1. That’s say, default high level.


Step 3#

If P2 is in low voltage, which means the button is pressed, the program will start to run in block then.


Step 4#

If the value of P2 is equal to 0, write 0 to P0 port and pause 500ms. Then, write 1 and pause 500ms, LED starts to flashing.



Program link:https://makecode.microbit.org/_5UriK0fC7LWr

You also could directly download program by visiting website as below:

4.7. Result#

Press the button, LED starts to flashing. Release the button, LED keeps lighting.

4.8. Think#

Why we add a 500ms pause ?

4.9. Questions#

4.10. Questions#