11. case 09 self-locking button

11.1. Introduction


  • The self-locking switch is a common use button switch. When we fisrt press the button and the circuit of switch will be connection, it means self-lock. And when we press the button again, the circuit of switch will be disconnection. In this case, we are going to use the self-locking switch to control on and off of the LED.

11.2. Hardware Connect


../../_images/2hsQnmL.png

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

11.3. Principles of Circuits


../../_images/VT0SVKN.png

  • The GND of slot on micro:bit is into innards of batteries’ GND to generate the current loop.

11.4. Introduction of Components


Self-locking switch

  • The self-locking switch is usually a normal switch with a mechanical lock built in. When pressed down and released, it doesn’t bounce back automatically. Only after a second press will it unlock and fully release.

  • The experiment box included a self-locking button with a red hat.

../../_images/3iIZPHP.png

- Note: Please note the positive and the negative when you are connecting.

11.5. Software


Step 1

../../_images/t34k5Zb.png

Step 2

  • Snap the set pin block into the on start to emit the edge events. Then, set pull pin P0 to up to set a default high level and to detect the edge of the level fluctuation.

../../_images/aIzHYGY.png

  • Every time you press the button, the voltage of P0 port P0 changes. From 0V to 3.3 V, we call this RISE. From3.3V to 0V, we call this FALL.

../../_images/kcnveNe.jpg

Step 3

  • Set a event to monitor rise and fall of P0 voltage. When the RISE comes, write 1 to P2 port to light up the LED.

../../_images/c6aX7T8.png

Step 4

  • When the FALL comes, write 0 to P2 port to turn off the LED.

../../_images/c6aX7T8.png

Program

---

11.6. Result


  • Press down self-locking switch, LED turns on; press again, LED turns off.

11.7. Think


  • How can we control the micro:bit screen with the self-locking switch ?

11.8. Questions


11.9. More Information