# micro:bot Kit Experiment Guide

Pages
Contributors: D___Run___, TheDarkSaint

## 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

• 1x micro:bit board (Not Included)
• 1x Micro B USB cable (Not Included)
• 1x moto:bit carrier board
• 2x wheels
• 2x Hobby Gear Motors
• 1x 4xAA Battery Holder
• 4x AA Batteries (Not Included)

Didn’t get the kit? Have no fear! Here are the parts you will need to complete this experiment…

DEV-14208
\$14.95
5

DEV-14213
\$14.95
2

ROB-13302
\$3.95

ROB-13259
\$2.95
2

PRT-09835
\$2.49

ROB-13301
\$12.95
7

SEN-11769
\$2.95
6

PRT-09100
\$0.50

PRT-10368
\$1.50

### 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 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

Like the magnetometer (compass), the accelerometer is on the micro:bit itself.

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.

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

#### 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.

In 2003, CU student Nate Seidle blew a power supply in his dorm room and, in lieu of a way to order easy replacements, decided to start his own company. Since then, SparkFun has been committed to sustainably helping our world achieve electronics literacy from our headquarters in Boulder, Colorado.

No matter your vision, SparkFun's products and resources are designed to make the world of electronics more accessible. In addition to over 2,000 open source components and widgets, SparkFun offers curriculum, training and online tutorials designed to help demystify the wonderful world of embedded electronics. We're here to help you start something.