12. case 09 selflocking button

12.1. Introduction

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

12.3. Hardware Connect

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

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

12.4. Principles of Circuits

../../_images/VT0SVKN.png

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

12.5. Introduction of Components

Selflocking switch

The selflocking 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 selflocking button with a red hat.

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

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

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

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

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

12.7. Result

Press down selflocking switch, LED turns on; press again, LED turns off.

12.8. Think

How can we control the micro:bit screen with the selflocking switch ?

12.9. Questions

12.10. More Information