DC Motor with micro:bit

Difficulty
Easy
Steps
14

A DC motor is a simple two-lead, electrically controlled device that converts direct current electrical energy into mechanical energy. It comes with a rotary shaft on which wheels, gears, and propellers, etc can be mounted on.

In this guide, learn to connect a DC motor to a micro:bit with a relay module and build a mini fan, then program it in MakeCode.

Complete this guide to learn how to use a DC motor with the micro:bit.

DC Motor

A DC motor is a simple two-lead, electrically controlled device that converts direct current electrical energy into mechanical energy. It comes with a rotary shaft on which wheels, gears, and propellers, etc can be mounted on.

  • It can be made to rotate clockwise or anticlockwise by reversing the polarity applied to the leads.
  • At low speeds, DC motors are impractical for position-control applications as there would be little torque and minimal position control. For that, we recommend using a servo motor with the micro:bit.
  • Similar to the servo motor, however, a controller such as the micro:bit can be used to govern the DC motor's operation through PWM.

It has two wires:

  • Ground (Black) 
  • 3V Power (Red)

In the following steps, you will learn to control a DC motor with a mini fan propeller and the micro:bit.

Pulse Width Modulation (PWM)

 

  • While you could change the speed of a DC motor by varying its supplied voltage, there are limitations to this:
    • Dropping a supplied voltage to 50% of the specified recommended voltage will cause the motor to cease to rotate
    • Applying a voltage that exceeds 30% of the recommended operating voltage may cause overheating and damage to the motor
  • The best way to efficiently control the speed of a DC motor is through PWM.
  • What PWM does is it turns the motor rapidly ON and OFF
  • The width of the pulse and period between pulses controls the speed of the motor.

Step 1  Motor to Relay connections

Step 2  Pushbutton module to breadboard

Step 3  P0 to SIG

Step 4  3.3V to button module

Step 5  GND to -

Step 6  5V to DC+

Step 7  GND to DC-

Step 8  P1 to IN

Step 9  5V to NO

Step 10  Attach fan propeller to DC motor

  • Next, attach the propeller to the DC motor.

Step 11  MakeCode for DC Motor and Fan!

It's time to program the micro:bit. Head to Microsoft Makecode and click on 'New Project'.

In 'on start' : 

  • Digital pin P1 is connected to the 'IN' pin on the relay, so set it as 0 with a 'digital write pin P1 to 0' block. 
  • Next, from 'Pins' tab, get a 'set pull pin ... to ...' block, make sure it is set to 'set pull pin P0 to up'

In 'forever' : 

  • From 'Logic' tab, get an 'if ... then ' block. Next, get out a '... = ...' block. 
  • Make a conditional statement with these blocks so that 'if digital read pin P0 = 0 then'
  • Next, place a 'digital write pin ... to ...' block from 'Pins' tab
  • Make sure it is set to 'P1' and '1'
  • Complete the conditional statement with a 'digital write pin P1 to 0' block as shown below

Step 12  Upload the code to micro:bit

Finally, to upload the code to the micro:bit and test it out: 

  • Connect the micro:bit to your computer using a microUSB cable 
  • Click on the 'Download' button on the bottom left corner in MakeCode editor 
  • Once downloaded, you can find the hex file in your 'Downloads' folder. 
  • Drag and drop it to the MICROBIT drive