Example Code for micro:bit-LED Light Controlling
This article offers example code for controlling an LED light using micro:bit, including hardware setup, wiring diagrams, and programming techniques to toggle LED states with a button press, ideal for educational purposes.
Hardware Preparation
- Gravity: Digital Piranha LED Module - Red: 1
- Gravity: Digital Push Button (Green): 1
- micro:bit - an Educational & Creative Tool for Kids: 1
- IO Extender for micro:bit V2.0: 1
Wiring Diagram
As shown below:

Sample Code
As following picture shows:

Program Link: https://makecode.microbit.org/_byUb5pbTKDRw
Result
When the switch is pressed and released, the LED light will be lit, and the LED light will be turned off after one press and release again. In this routine, two while loops are used to check whether the button is released after the press is completed. The delay in the while loop is used to eliminate the shaking of the button.
Was this article helpful?
