Example Code for micro:bit-Anti-thief Lock

Last revision 2025/12/31

The article offers a detailed guide on constructing a micro:bit-powered anti-thief lock, including necessary hardware components, software tools like Makecode and Mind+, wiring instructions, and example code to program the lock. Users can learn how to set up the system, enter passwords through a touch key, and receive feedback via LED patterns and buzzer sounds, ensuring a practical and effective security solution for various applications.

Hardware Preparation

  • Math & Automatic expansion board for micro:bit, Quantity: 1
  • micro:bit, Quantity: 1
  • Servo, Quantity: 1
  • Buzzer, Quantity: 1

Software Preparation

Makecode graphical programming, Mind+ platform.
Makecode Library Address.
About how to install a software library?

Wiring Diagram

Servo connected to S1; Buzzer is integrated on the board.

Other Preparation Work

Install the Makecode library from https://github.com/DFRobot/pxt-Math-Automatic.
About how to install a software library?

Sample Code

Anti-thief Lock
Anti-thief Lock Example Code Download

Result

Touch Key DFlogo to enter 3-digit passward, if the passward is correct, the micro:bit LED matrix show "√" pattern, the buzzer plays the "correct beep sound", and the servo rotates 180 degrees and backs to 0 degree 1s later. Otherwise, micro:bit displays "×" pattern, the buzzer makes the "error beep sound", and the servo keeps still. Press the DFlogo key to enter passward again. Initial passward: 726.

Was this article helpful?

TOP