• EUR€
  • £GBP
  • $USD
TUTORIALS micro:bitMicroPython

micro:bit MicroPython Tutorial: Controlling a DC motor

DFRobot May 06 2019 912

In this tutorial we will check how to control a DC motor using the micro:bit board and MicroPython.


In this tutorial we will check how to control a DC motor using the micro:bit board and MicroPython.

Since we can’t use the micro:bit to directly power the DC motor, we will use a ULN2803A integrated circuit for that. So, the micro:bit will be responsible for controlling the ULN2803A which, in turn, will allow the motor to be powered or not.

You can check the datasheet of the ULN2803A here. You can also check this previous tutorial, which explains in more detail how to use this integrated circuit to control a DC motor.

The electric diagram
Since the pins of the micro:bit can only source a maximum current of 5 mA [2], we cannot directly connect the device to a DC motor.

Thus, as already mentioned, we will be using a ULN2803A integrated circuit to provide the current for the motor to operate. The micro:bit will only control if the ULN2803A is providing current to the motor or not.

The connection diagram between the devices can be seen in figure 1. Note that I’m using a 5 V DC motor.

In sum, the ULN2803A will act as a switch, which will turn on / off the connection of the motor to GND, depending if the voltage applied to pin ln 1 is either high or low, respectively.

So, if pin 0 of the micro:bit is in a high level, the ULN2803A will connect the DC motor to GND and thus it will run. If pin 0 of the micro:bit is in a low level, then the ULN2803A disconnects the motor from GND and it will be stopped.

The code

The first thing we will do is importing all the functionalities from the microbit module. With this, we will have access to the objects that will allow us to control the state of the pins, as we will see below.

from microbit import *

We will write the rest of our code inside an infinite loop, so we can turn on and off the motor in a pattern that will repeat as long as the program is running.

while True:
    #loop code

In MicroPython, each pin of the micro:bit is represented by an object called pinX, where X is the number of the pin [1]. As previously hinted, these objects representing the pins can be imported from the microbit module, like we already did.

Note that, in our case, we have the pin 0 of the micro:bit board connected to the ULN2803A, which means that we will interact with the object named pin0.

In order to set the digital level of a pin, we simply need to call the write_digital method on the pin object, passing as input the value 1 to set it to high, or 0 to set it to low.

So, we will start by setting the digital pin to a high level, thus turning the motor on.


After this, we will delay the execution 4 seconds, so the motor stays on a bit. We will do it by calling the sleep method, which receives as input the number of milliseconds to wait.

This function also belongs to the microbit module. Since we imported all the content from this module in the first line of our code, then we can use the sleep function. You can read more about it here.


To finalize, we will now turn the motor off by calling again the write_digital method, passing as input the value 0. This will set the digital level of the pin to low.

We will also delay the execution 4 seconds, in order for the motor to stay off during some time.


Note that since we have written the previous 4 lines of code inside an infinite while loop, the motor will keep turning on and off as long as the program is running. The final code can be seen below.

from microbit import *
while True:

Testing the code

To test the code, simply run it on your micro:bit board, after all the connections shown in figure 1 are done. I’ll be using uPyCraft, a MicroPython IDE, to run the previous code on my device.

After the script starts running, the motor should start moving and then stopping in 4 seconds intervals, like shown in the video below.

Source Link: https://techtutorialsx.com/2019/04/07/microbit-micropython-controlling-a-dc-motor/