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.2. Hardware Connect


../../_images/fLSfez6.png

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

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

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

4.5. Software


Step 1

../../_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

---

4.6. Result


  • Press the button, LED starts to flashing.

  • Release the button, LED keeps lighting.

4.7. Think


  • Why we add a 500ms pause ?

4.8. Questions


4.9. Questions