How to control a DC motor using the Micro:bit board | Motor controlling with Micro:bit

Hello and welcome back. In this tutorial, we will learn how to control a DC motor using the Micro:bit board. A DC motor is a very useful component for small projects. For this project, I used a joystick module to control the motor. When we move the joystick forward and backward, the motor also turns left or right. Also, I connected a small propeller to the motor. With this setup, we can use it as a simple table fan. You can also change the speed of the motor using the joystick module.
For programming this project, I used the MakeCode platform. With this platform, we can easily create the program using code blocks. It is very simple for beginners. If you want, you can also use Python or JavaScript to write the same program. Also, I used two Li-ion batteries to power up this circuit, but you can also use a 9V battery.
Ok, let’s do this project step by step. The required components are given below.
- Micro: bit V2 or V1 board x 1 — Our store / Amazon
- Micro: bit sensor shield x 1 — Our store / Amazon
- DC Motor x 1 — Our store / Amazon
- Joystick module — Our Store / Amazon
- Jumper wires — Our store / Amazon
- Keyestudio BBC Micro: bit starter kit — Click on me
Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.
Step 1
Firstly, identify these components.





Step 2
Secondly, connect the Micro:bit board to the sensor shield.

Step 3
Thirdly, connect the joystick module and motor to the Micro:bit sensor shield. Use the circuit diagram below for that.







Step 4
Next, connect the Micro:bit board to the computer. After that, go to the MakeCode website and create a new project. You can name it as you like.



Step 4
Afterward, copy and paste the Python code into the Python tab. Then, you will see the auto-generated code blocks.
- Program — Download
value = 0
speed = 0
def on_forever():
global value, speed
value = pins.analog_read_pin(AnalogReadWritePin.P0)
# Joystick center ~510
if value < 510:
# Clockwise
speed = Math.map(value, 0, 510, 1023, 0)
# Stronger push → faster motor
pins.analog_write_pin(AnalogPin.P1, speed)
pins.analog_write_pin(AnalogPin.P2, 0)
elif value > 510:
# Counterclockwise
speed = Math.map(value, 510, 1023, 0, 1023)
pins.analog_write_pin(AnalogPin.P1, 0)
pins.analog_write_pin(AnalogPin.P2, speed)
else:
# Stop at center
pins.analog_write_pin(AnalogPin.P1, 0)
pins.analog_write_pin(AnalogPin.P2, 0)
basic.forever(on_forever)




Step 5
Then, connect the Micro:bit board with the MakeCode IDE. In this step, make sure the board is connected to the computer. Finally, click the download button.






Step 6
Next, unplug the USB cable and connect the external power supply to this setup. I used two Li-ion batteries for this project, but you can also use a 9V battery.



Step 7
Finally, you can test this project by moving the joystick module. The full video guide is below. We hope to see you in the next project. Have a great day.


How to control a DC motor using the Micro:bit board | Motor controlling with Micro:bit