Example Code for micro:bit-Motor Control System

Last revision 2025/12/31

This guide provides an example code for controlling a motor using micro:bit, detailing hardware setup, software preparation, and wiring diagrams to adjust motor speed from 0 to 255 using touch keys.

Hardware Preparation

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

Software Preparation

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

Wiring Diagram

Motor connected to M2.

Other Preparation Work

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

Sample Code

Motor Control System
Motor Control System Example Code Download

Result

Use touchkey 0~9 to conctrol the speed of M2 motor. When key 0 is touched, motor stops; key 9 touched, motor rotates at full speed. Motor speed range from 0 to 255.

Was this article helpful?

TOP