Microbit Accelerometer Basics #tt8

Tinkerer: Lory Livezey

The Micro:bit has a build-in accelerometer, that allows you to detect it's movements on the X, Y, and Z planes. In this tuturial, we're going to do a very simple project that will detect the values as the Micro:bit is tilted. We'll use this to build on other projects.


Objective

In this lesson you will learn:

  • How to find blocks that you may not be familiar with using the search and color codes
  • That certain types of blocks will only fit into certain "slots"
  • How the accelerometer is used to detect when the Microbit is moved
  • How to save a project

Previous Step

  Microbit Simple Hello World #tt6


Starter Breakpoint

  • You will be starting from scratch

What you will need


Step 1 - Log Into MakeCode for Micro:bit

If you're not familiar with the basics of the MakeCode web site, please check out the previous tutorial, Micro Bit Simple Hello World.

Browse to the Make Code Web Site


Step 2 - Set Up On Start Block

I always display something OnStart so I know the Micro:bit is working. Put any icon you like on the screen:

Basic > show icon

Image Showing Adding Image to Start

Show On Start Block


Step 3 - Detect Movement in Forever Block

To detect movement, we'll need the Accelerometer code block. I can tell you where it is, but i'm going to teach you a handy trick. Type the first few letters in the Search box, then hit enter.

Image showing search results

Do you see how the block is dark pink and the Input section is also that color? Next time, you can find the acceleration block in Input. Drag the block over to the canvas.

Image show acceleration does not fit into Forever block

You may notice that the block doesn't fit into the Forever block. We need something else... We'll just let that block hang out on the canvas until we find it a home.

Let's display the number we get from the accelerometer to the screen.

Basic > show number

Notice that when you hover over a block, it will give you more information about it. Drag Show Number over to the canvas. See how this one fits into the Forever Block?

Image showing show number has a hover tool tip

Drop the Show Number block into the Forever block:

Image showing the show number block

The Acceleration block will now snap nicely into the Show Number block. Set it to X first, then Axis you want to test:

Set Acceleration Axis

Your blocks should look like this:

Final Blocks

Now we're ready to run it on our Micro:bit to see what it displays.


Step 4 - Upload Code to Micro:bit

Name the project something descriptive and save it:

Name the project and save it

Go to your Downloads folder and drag the file that was created to your MICROBIT drive.

Drag Hex file to Microbit Drive


Step 5 - Test the Micro:bit on the X Axis

The Micro:bit will now display the value of the X axis. First tilt it all the way to the left, then keep it still. You should see a number close to -1000 (-1024 is the max). Tilt it all the way to the right and you will see around +1000 (+1024 is the max).

Testing the Accelerometer


Step 6 - Repeat these steps for the Y Axis

Go back to Step 3 and change the Axes to Y. Upload the code and see how the numbers output.


Finish Breakpoint

Hex for Finish Code


Discussion

  • Why do you think only certain types of blocks and be added to a container?
  • If the X axis detects movement from side to side, and the Y axis detects movement back and forth, what type of movement do you think the Z axis detects?
  • What uses for the accelerometer can you think of, or that you may use every day?

SUCCESS!

You now have a basic understanding of how the accelerometer works and how the numbers change as the Micro:bit is tited on the X and Y axes. This knowledge will be important as we move on to more advanced projects. See you soon!


Next Up

Sending Accelerometer Readings Between Two Microbits Using Radio #tt9

Be sure to subscribe to our channel!