Example Code for micro:bit-Drive Motor

This article offers a comprehensive guide on using micro:bit to drive motors, including hardware setup, software preparation, wiring diagrams, and sample code for motor control with button inputs.

Software Preparation

Graphical block software library: https://github.com/DFRobot/pxt-motor

Sample Code

Drive Motor1

Drive Motor2

Result

The motor M1 and M2 rotate in positive direction at a speed of 150 for 2 seconds, then reverse for 2 seconds, in endless loop.

When the button A is pressed, M1 rotates forward and M2 stops; when the button B is pressed, M2 rotates forward and M1 stops; when both button A and button B are pressed at the same time, all motors stop.

Was this article helpful?

TOP