Driving a Motor from a Microbit using the Accelerometer and Radio #tt16

Image for Driving a Motor from a Microbit using the Accelerometer and Radio #tt16
Tinkerer: Lory Livezey

In this tutorial, we're going to be hooking up the Kitronik Motorboard to a Micro:bit. In the previous tutorial, we learned how to send accelerometer readings from one Micro:bit to another using Radio. We are going to build on that by controlling the speed of a motor as we tilt the Micro:bit remote control.

TAGS{ turbo_tinker=16 } RELATIONS{ COMES_AFTER=Sending Accelerometer Readings Between Two Microbits Using Radio #tt9 } CATEGORIES{ Microprocessor Boards->Microbit }


You'll learn:

  • What a breakpoint is
  • What User Interface Design is and why it's important
  • How to add a new section to the block menu
  • How to connect and program the Kitronic Motor board

Previous Step

  Sending Accelerometer Readings Between Two Microbits Using Radio #tt9

To complete this tutorial, you should follow the previous steps or be familiar with the following:

  • The basics the Make Code Web Site
  • How to use the Accelerometer and Radio on the Micro:bit
  • How to upload a program (hex) to the Micro:bit
  • How to create and use a variable
  • How to add a new component to the blocks menu
  • How to program the Kitronik motor board

Starter Breakpoint

A breakpoint in programming is a place in the code where you set the program to stop so you can figure out what's wrong with it. We use the term breakpoint to refer to the code that you can load to start you off at a certain point in our tutorials.

What you will need

If you want to support our tutorials and programs, please purchase from our links

Materials List

Step 1 - Log Into MakeCode for Microbit

Browse to the Make Code Web Site

If you are continuing from the previous step, you'll have the two projects we're going to need. Otherwise, use the import option to upload the hex files above.

Step 2 - Program the Remote Microbit

Our remote will determine the speed that the motor will turn. Since it seems more intuitive that we tilt the Micro:bit forward and back to speed up or slow down, then we will use the y axis. This is called User Interface Design - when you think about how you would expect something to work, and designing it intuitively. The goal is for people to know how to use something with as little instructions as possible, and a good design will make it as easy as possible.

Open the Tinker Pi Remote project. This is where we left off:

Final Blocks from the Previous Tutorial

We are simply going to change the xs to ys -- both of them.

This means rather than sending x=150 as we tilt left-to-right, we'll be sending y=255 as we tilt from front-to-back.

Save your project, download the hex and save it to your Micro:bit.

Final Blocks from the Previous Tutorial

Step 3 - Program the Robot's On Radio Received Event

Next, open the Tinker Pi Robot project. This is how we left it in the last tutorial:

Start Blocks from the Previous Tutorial

We will be getting a value such as y=255 from the remote, so we need to create a variable called y. To do this:

Variables > Make a Variable > y

Create Y Variable

Change the x's to y's:

Change Xs to Ys

Step 4 - Add the Kitronik Motor Driver

Click the gear in the upper right, and then Add Package:

Add Package

Search for kitronik and select the kitronik-motor-driver:

Kitronik Package

This group will be added:

New Kitronik Group

Add the Kitonik Block to the Forever block, and set the speed to the y variable:

Add Kitronik Block

So let's recap: The value of y=1024 (for example) will be sent from the remote to this Micro:bit. The y variable is set to 1024. The block we just placed will set motor 1 to a forward speed of y. The maximum speed of the motor, however, is 10. Do you see the problem?

Right It's too big. Since 1024 is the maximum that the accelerometer will go in any direction, we need to divide the y value by 10. This will cause the motor to be set to 10 when we rotate the Micro:bit completely forward.

We need a divide block. You can find this under Math:

Math 0/0

Drag the 0/0 block to replace the y variable in the speed:

Drag 0/0 block

Drag the block over the y and it will be displaced:

Replace the y variable

Set the speed to y divided by 10:

Set the speed

Output the value so we can see it on the screen. Do this by duplicating the block like we learned earlier. Your blocks should look like this:

Final Blocks

Save the program and upload it to the Microbit.

Step 3 - Wire Up the Motor

Next, plug the robot Microbit into the Kitronik Motor Board. Connect the red wire from the battery pack into the + terminal and the black into the -, as shown:

Final Blocks

Don't forget to insert the batteries

Next, plug the motor into the motor board, as shown:

Plug Motor Into Motor Board

Optionally plug a wheel into the motor. Make sure the battery pack is in on on position, and your motor/wheel should turn:

Plug Motor Into Motor Board

Finish Breakpoint


We now have these pieces working:

  • Accelerometer on Micro:bit #1
  • Radio communication to Micro:bit #2
  • Motor Driver and Motor working, controlled by Micro:bit #1

Next Up

Assembling the Tinker Pi Robot for Microbit #tt13

Be sure to subscribe to our channel!