Lesson 06 Self-lock Switch


Self-lock switch is a kind of common button switch. When we press its button for the first time, the switch is connected and remains in that status, which is called “self-lock”. When we press the button for the second time, the switch is disconnected. At the same time, the button will bounce back to its initial place. In this experiment, we are going to use self-lock switch to control an LED light.

What you need

  • micro:bit board
  • Micro USB Cable
  • micro:bit breadboard adaptor
  • Breadboard
  • Self-lock Switch
  • LED
  • 100 Ohm resistor
  • Male to Male Jumper wires

Self-lock Switch

Self-lock switch normally means switch with built-in mechanical lock function. Press down the switch and then release, it will not fully bounce up because it is locked. You have to press it again, then it will be unlocked and fully bounce up. This is the so-called Self-lock Switch. It is widely used to earlier televisions and monitors with function of directly completely power off.

Note: This kind of self-lock switch contains two groups of double-throw switch. In this experiment, we use a single group only. So we cut down the common footer of a group.

Hardware Connection

Please complete hardware connection according to the breadboard diagram below:

After connecting your circuit, you will see the following:


Please open Microsoft Makecode, write your code in the edit area. Try to program by yourself first.

Of course, you can see the whole program directly in the link below. Just click “Edit” on the top right corner of the interface to start edit your program. Once you completed edit, click “Download” in the bottom right corner of the interface to download your code directly into Micro:bit.

Link of the whole program: https://makecode.microbit.org/_Ms0RHhRxj9oq

Code Explanation

set events

Configure the type of events emitted by a given pin.

on events 

Raise an event in the event bus.

In this case, we designed 2 events: P0 voltage rise and fall. Every time we press down the switch, voltage in P0 port will change. When the voltage starts shifting from 0V to 3.3V, we call it “ Rise”. When the voltage starts shifting from 3.3V to 0V, we call it “Fall”.


Press down on the self-lock switch, LED turned on; press again, LED turned off.

Taking it further

Usually stair lights use double-throw switches just like our experiment. We can turn on the light upstairs and turn off the light of downstairs, vice versa. Suppose if we want to use 2 self-lock switch to create this stair light function, then how would you design this circuit and program it?

micro:bit Starter Kit Lessons

  • Lesson 01 – LED
  • Lesson 02 – Button
  • Lesson 03 – Trimpot
  • Lesson 04 – Photocell
  • Lesson 05 – RGB LED
  • Lesson 06 – Self-lock Switch
  • Lesson 07 – Temperature Sensor
  • Lesson 08 – Servo
  • Lesson 09 – Buzzer
  • Lesson 10 – Motor
  • Lesson 11 – Rainbow LED
  • Lesson 12 – Accelerometer
  • Lesson 13 – Compass
  • Lesson 14 – Ambient Light
First published at 1:57pm on July 9, 2019
Last updated at 1:57pm on July 9, 2019