micro:bot Kit Experiment Guide

Pages
Contributors: D___Run___, TheDarkSaint, bboyho, Ell C
Favorited Favorite 5

Experiment 4: Using the Accelerometer

Introduction

Now, what happens when something bumps into your robot? It just stays there and take it from this bully? No! It should move out of the way, or do something. In this experiment you will use the accelerometer on the micro:bit to trigger the robot to move out of the way. If you flip the robot on its back or stand it on end it will stop driving (play dead)!

Parts Needed

You will need the following parts:

  • 1x micro:bit board (Not Included with Kit)
  • 1x Micro-B USB Cable (Not Included with Kit)
  • 1x moto:bit carrier board
  • 2x Wheels
  • 1x Assembled Shadow Chassis
  • 2x Hobby Gear Motors
  • 1x 4xAA Battery Holder
  • 4x AA Batteries (Not Included with Kit)

Didn’t get the kit? Have no fear! Here are the parts you will need to complete this experiment. You may not need everything though depending on what you have. Add it to your cart, read through the guide, and adjust the cart as necessary.

Hobby Gearmotor - 140 RPM (Pair)

Hobby Gearmotor - 140 RPM (Pair)

ROB-13302
$5.50
6
Wheel - 65mm (Rubber Tire, Pair)

Wheel - 65mm (Rubber Tire, Pair)

ROB-13259
$3.50
2
USB Micro-B Cable - 6 Foot

USB Micro-B Cable - 6 Foot

CAB-10215
$5.50
15
SparkFun moto:bit - micro:bit Carrier Board (Qwiic)

SparkFun moto:bit - micro:bit Carrier Board (Qwiic)

DEV-15713
$24.95
Battery Holder - 4xAA to Barrel Jack Connector

Battery Holder - 4xAA to Barrel Jack Connector

PRT-09835
$2.75
Shadow Chassis

Shadow Chassis

ROB-13301
$13.95
7
Panasonic Alkaline Battery - AA

Panasonic Alkaline Battery - AA

PRT-15201
$0.55

micro:bit Board

DEV-14208
10 Retired

Suggested Reading

What is Electricity?

We can see electricity in action on our computers, lighting our houses, as lightning strikes in thunderstorms, but what is it? This is not an easy question, but this tutorial will shed some light on it!

Accelerometer Basics

A quick introduction to accelerometers, how they work, and why they're used.

Getting Started with the micro:bit

The BBC micro:bit is a compact, powerful programming tool that requires no software installation. Read on to learn how to use it YOUR way!

Introduction to the Accelerometer

On the back of the micro:bit you can see a number of small chips. One of them is the accelerometer. The micro:bit has an onboard accelerometer that measures gravitational force. Depending on the version that you have, the accelerometer and compass can be on separate ICs or combined into a single IC.

v1.0 w/ Accelerometer on Separate IC v1.5 w/ Combined Accelerometer and Magnetometer
v1.0 w/ Accelerometer on Separate IC v1.5 w/ Combined Accelerometer and Magnetometer

An accelerometer is a sensor that measures the gravitational forces pulling on it in all three dimensions of the chip's X, Y and Z axes.

Triple Axis Accelerometer Visualization

Visualization of a Common Accelerometer (ADXL345) with Three Axes

Accelerometers are devices that measure acceleration, which is the rate of change of the velocity of an object. They measure in meters per second squared (m/s2) or in G-forces (g). A single G-force for us here on planet Earth is equivalent to 9.8 m/s2, but this does vary slightly with elevation (and will be a different value on different planets due to variations in gravitational pull). Accelerometers are useful for sensing vibrations in systems or for orientation applications. Vibration is what we are looking for!

Hardware Hookup

You have had the hardware hooked up the whole time! It is on the micro:bit! Feel free to move on.

Running Your Script

Be sure to add the moto:bit package as instructed in the Installing the moto:bit Package in MakeCode section of this tutorial.

Now, you can either download the following example script below and drag and drop it onto your micro:bit, or use it as an example and build it from scratch in MakeCode.

Code to Note

Let’s take a look at the code and what to expect.

Code to Note

On [Accelerometer]

The micro:bit has a built in accelerometer that measures the gravitational forces that are acting upon it. With other microcontrollers this is sometimes hard to use and make sense of the data you receive from the sensor. But, MakeCode has simplified that for you in an event block under the Input drawer (you will see it as On Shake) that allows you to pick a number of different force levels, orientations and even patterns (like shaking it). We use this to detect a number of orientations that you can put your robot in. For example the motors will only be ON in the screen up orientation.

If you tap the robot hard enough it will run / drive away from you. If you flip it over the motors will turn off. Same thing holds true if you place it on its tail with the micro:bit pointing up.

Try playing around with the different events that you can trigger with the accelerometer. Can you get it to detect bumping into an object like the wall and have it respond???

What You Should See

Once your code is loaded to your micro:bit and your motor switch is set to "RUN MOTORS." Your robot should not do anything at first. Give it a good tap / smack on top of the micro:bit. If you apply enough force to it your robot should run away. To stop your robot either flip the robot over to its back the motors should stop, or pick it up and set the robot on end with it sitting on its tail with the micro:bit logo pointing up. To run the program again, return the robot to its wheels right side up and smack it again.

Go Further: You can use the idea of detecting a bump in different ways. Try to write a program that giving your robot a nudge or tap starts it to drive forward for a bit, then you need to nudge it again a bit.

Troubleshooting

  • Robot Doesn't Move - Double check to make sure you have uploaded the new code to your board. Also, make sure your motor switch is set to the "RUN MOTORS" position.

  • Moving in Reverse?! - There are two options if you notice the micro:bot moving in the wrong direction. As explained above, you can flip the wiring of your motors or use the set ____ motor invert to ____ block in your on start block to change what is forward vs. reverse.