Sending Accelerometer Readings Between Two Microbits Using Radio #tt9

Tinkerer: Lory Livezey

In this tutorial, we're going to build on the last and send the accelerometer readings generated when the Micro:bit is tilted to a second Micro:bit.


Objectives

In this tutorial, you'll learn:

  • How Make Code stores your previous work
  • How to download and later upload your project
  • How to configure your Microbits to communicate over Radio waves
  • How to send accelerometer readings to the second Microbit

Starter Breakpoint

Hex from Accelerometer Basics Tutorial


Previous Step

  Microbit Accelerometer Basics #tt8

If you're not familiar with the basics of the MakeCode web site, please check out, Micro Bit Simple Hello World. It's fairly intuitive, and I will recap what we learned there. But if you find yourself having trouble with the code in this tutorial, you may want to go back and review Micro Bit Accelerometer Basics, where we do a very simple project to see what values the accelerometer gives us.


What you will need


Step 1 - Log Into MakeCode for Micro:bit

Browse to the Make Code Web Site

The site seems to keep track of what you do using cookies or something in the session. You won't have to log in, or save anything. It will remember what you worked on the last time, so long as you're on the same computer. You may, however, save your projects to a hex file, then import it back if you're using a different computer. It's a good idea to back up your work offline anyway. ;)


Step 2 - Start with the REMOTE from Micro:bit Accelerometer Basics Tutorial Code

This will be our starting point. It's fairly simple, so you can also recreate it from the picture below.
Final Blocks from the Accelerometer Basics Tutorial

Save the file as Tinker Pi Robot Remote, as this will become the remote control for our Robot in the next tutorial.

Save the file as Tinker Pi Robot Remote


Step 3 - Set the Radio Group

We're going to use radio communication to send commands from the remote Micro:bit to the robot Micro:bit. This is the same type of radio signals that are used in Remote Control (RC) cars.

Find the Radio Group, and drag the block called Radio Set Group over to the canvas:

Drag the Radio Set Group to Canvas

Change the number to a random number between 1 and 100. Behind the scenes, there is a radio frequency, but the MakeCode site makes this easy for us and translates the number that we choose into that signal. This number needs to match the one we program into the robot, so remember it!


Step 4 - Send the Accelerometer Reading over the Radio

From the Radio Group again, drag the block radio send value to the canvas:

Drag the Radio Send Value to Canvas

Change the value to x:

Change Name to X

Drag the acceleration block that we used in the our previous tutorial down to the puzzle piece in the radio send block, and then delete the show number block. It should look like this:

Add Acceleration


Step 5 - Save the Program and Upload to Micro:bit

You can either download the hex file to your computer and drag it over to your Microbit drive, or recently, they have added another option. When you click Download, a box will pop up. Right-click on the green box that has the name of your hex file in it, and select Save link as.... Then you can select your Microbit drive, and bypass saving it to your computer. The downside to this is that you will not have a backup if you need it later. So be sure to save your hex files every now and then.


Step 6 - Set Radio Group for Robot's Micro:bit

Next, we'll program the Robot's Microbit. Create a New Project by clicking Projects, then New Project.

Drag a Radio Set Group to the Start block. Set it to the same number as you set for the remote.

Set Radio Group for Robot Microbit


Step 7 - Program On Radio Received Event

In this step, we'll detect a signal received over radio and write it to a variable. Drag the on radio received block to the canvas.

Program On Radio Received Event

Add an if block to the on radio received event:

Add If Block

If you recall, we are sending a value such as x=100 from the other Micro:bit, which would indicate that the Micro:bit is tilted to the right to 100. This if block will check to see if it's x=, because later we'll also be sending y=.

Drag an 0 = 0 again from the Logic group into the puzzle piece in the if block:

Drag 0 equals 0 Block to If Block

Next, drag the variable called name to the left side of the 0=0:

Drag name variable

We want the evaluation to be name="x". In other words, if the variable called name being passed from the other Micro:bit is equal to "x", then ...

If you try to type x in the second puzzle piece, you'll find it won't work. Any idea why?

It is expecting a number! So, we need to tell it to expect a string.

Under the Advanced section, then Text, drag the block that looks like this to that second puzzle piece:

Drag text

You'll find that you can now type an x in it. Your blocks should look like this:

If Block

Drag the block set item to 0 and place it inside the if block:

Set Item to 0

Rename the variable called item by default to something that will make sense.

Rename Item Variable

Call it x since this will hold the value of the x coordinate of the accelerometer.

Rename Variable to x

Finally, we're going to set it to the value that's being passed in from the other Micro:bit.

Set value to x

Now, the variable x is set to the x axis coordinate from the remote Micro:bit.


Step 8 - Display the Value Received from the Micro:bit

In the Forever loop, we're going to constantly display the x coordinate value. So, as we did on the other Micro:bit in the previous tutorial, we're simply going to display the value.

Drag the show number block into the Forever loop. If you recall, it's under Basic.

Next, drag the x variable block from Variables, so that your blocks look like this:

Final Blocks


Step 9 - Upload the Program to the Robot Micro:bit and Test

Be sure to save the program, and this will also download. Drag the program to the Micro:bit drive. Plug both Micro:bits into power. Once they are programmed, they do not need to be plugged into the computer.

Test Results


Finish Breakpoint

Hex for Remote

Hex for Robot


Success!

In this tutorial we've learned about Radio communications, and building on the Accelerometer basics we learned in the last tuturial, as well as assigning and using variables.

Next, we'll be building on this to use one Micro:bit as the remote for the Tinker Pi Robot.


Next Up

Assembling the Tinker Pi Robot for Microbit #tt13
Driving a Motor from a Microbit using the Accelerometer and Radio #tt16

Be sure to subscribe to our channel!