Control a DC Motor with micro:bit, Web Bluetooth and Kitronik Motor Driver

Ferry Djaja
3 min readJul 18, 2017

--

Here is a quick guide on how to drive and control the speed of the DC motor with Kitronik motor driver, web bluetooth and BBC micro:bit.

Kitronik Motor Driver with BBC micro:bit

Part Lists

1 x micro:bit

1 x Kitronik motor driver version 2

1 x 4AA battery box

1 x 5 Volt DC motor

Kitronik Motor Driver Board Wiring Configuration

Connect the DC motor to P12 and P8 breakout terminal on the Kitronik motor driver board and connect the battery box to power input of the motor driver board as show in the below diagram.

Wiring Configuration

Let’s Code

We need to create two applications: web bluetooth app (an HTML app that you can run from the smartphone or PC with Chrome browser) and firmware app (to be installed on micro:bit).

Web Bluetooth App

Web Bluetooth App

Establish the bluetooth event service with the following UUIDs:

EVENTSERVICE_SERVICE_UUID: e95d93af-251d-470a-a062-fa1922dfa9a8MICROBITEVENT_CHARACTERISTIC_UUID: e95d9775-251d-470a-a062-fa1922dfa9a8CLIENTEVENT_CHARACTERISTIC_UUID: e95d5404-251d-470a-a062-fa1922dfa9a8CLIENTREQUIREMENTS_CHARACTERISTIC_UUID: e95d23c4-251d-470a-a062-fa1922dfa9a8

And register the Event Id 8888 with function initEvent(). You can create any Event Id number as you wish.

Create the function mouseDown() and mouseUp() to detect when user press or release the button:

  • When user press the button “Left”, write the event value 1001
  • When user press the button “Right”, write the event value 1002
  • When user release the button, write the event value 1003

Those event values will be sent to firmware app and it will be translated to the movement of the motor.

Firmware app

Let’s write a firmware app to listen to the bluetooth event service on the Event Id 8888.

We need to register an event handler to listen to the incoming event value from the Web Bluetooth app. The value that is coming from the Web Bluetooth app would be in the following format:

<direction><speed>

with <direction> as follows:

1001 — Button “Left” has been pressed, motor turns to the left direction1002 — Button “Right” has been pressed, motor turns to the right direction1003 — Button was released, set motor to stop

and <speed> is from 0 to 1000. We can control the speed of the motor with this variable.

Here is an example:

[8888, 1001]   : When user press the button "Left". 8888 is the Event Id.[8888, 500]    : speed is 500, convert the speed from 0-100 to 0-1024 (approximate). Speed 50 is converted to 500.[8888, 1003]   : when user release the button

To drive the motor to turn left with particular speed:

  • Set P12 as an analog output and set the output value with the speed variable.
  • Set P8 as a digital output and set the pin value to High

To drive the motor to turn right with particular speed:

  • Set P8 as an analog output and set the output value with the speed variable.
  • Set P12 as a digital output and set the pin value to High

To stop the motor:

  • Set P8 and P12 as a digital output and set both pins value to Low

Here is the code:

JavasScript code to control motor direction

Source Code

Demo Video

That’s it. Please do let me know if you have any questions or comments.

--

--