5. case 02 button#

5.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.

5.3. Hardware Connect#

../../_images/fLSfez6.png

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

5.4. Principles of Circuits#

../../_images/NSpS8c0.png

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.

5.5. Introduction of Components#

Button#

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.

../../_images/HgatY6t.png

5.6. Software#

Step 1#

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

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

../../_images/t34k5Zb.png

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.

../../_images/VuZAOrz.png

Step 3#

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

../../_images/0EHwnci.png

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.

../../_images/z9Yqpi3.png

Program#

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

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

5.7. Result#

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

5.8. Think#

Why we add a 500ms pause ?

5.9. Questions#

5.10. Questions#