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.
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
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.
Save the file as
Tinker Pi Robot Remote, as this will become the remote control for our Robot in the next tutorial.
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.
Radio Group, and drag the block called
Radio Set Group over to the 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
Radio Group again, drag the block
radio send value to the canvas:
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:
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
Radio Set Group to the
Start block. Set it to the same number as you set for the
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.
if block to the
on radio received event:
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
if block will check to see if it's
x=, because later we'll also be sending
0 = 0 again from the
Logic group into the puzzle piece in the
Next, drag the
name to the left side of the
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
Advanced section, then
Text, drag the block that looks like this to that second puzzle piece:
You'll find that you can now type an
x in it. Your blocks should look like this:
Drag the block
set item to 0 and place it inside the
Rename the variable called
item by default to something that will make sense.
x since this will hold the value of the
x coordinate of the accelerometer.
Finally, we're going to set it to the
value that's being passed in from the other Micro:bit.
Now, the variable
x is set to the x axis coordinate from the
Step 8 - Display the Value Received from the Micro:bit
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.
show number block into the
Forever loop. If you recall, it's under
Next, drag the
x variable block from
Variables, so that your blocks look like this:
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.
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.
|Assembling the Tinker Pi Robot for Microbit #tt13|
|Driving a Motor from a Microbit using the Accelerometer and Radio #tt16|