# micro:bit Lesson 1. Using the Built-in Sensors

## Overview

The BBC micro:bit has a number of built-in sensors that make it really easy to get started with a few simple projects.&nbsp;

In this lesson you will learn how to use the micro:bit's accelerometer to make a simple movement activated alarm. You will also learn how to use the micro:bit's magnetometer chip to detect the presence of a magnet.

![](https://cdn-learn.adafruit.com/assets/assets/000/050/174/medium800/sensors_F01_01b.jpg?1516029530)

If you flip your micro:bit over, you can see both the accelerometer and magnetometer (compass) marked on the bottom left of the photograph above.

# micro:bit Lesson 1. Using the Built-in Sensors

## Magnetometer

The micro:bit's built-in magnetometer chip is intended for use as compass to detect magnetic north. Like the compass app on your phone, this requires calibration and in my experience doesn't work _very_ reliably. However, it is great for detecting the presence of a magnet!

# Magnet Detector

The following project will make the micro:bit's display show a cross when a magnet comes near. This works best with a strong 'neodymium' magnet like the one in the side panel.

### High-strength 'rare earth' magnet

[High-strength 'rare earth' magnet](https://www.adafruit.com/product/9)
Yow! These things are super powerful. .47" diameter and .18" thick discs. Great for use with your SpokePOV Kit. If you have an aluminium frame, use tape/foam sticky tape to attach it.

In Stock
[Buy Now](https://www.adafruit.com/product/9)
[Related Guides to the Product](https://learn.adafruit.com/products/9/guides)
![Angled shot of a High-strength 'rare earth' magnet.](https://cdn-shop.adafruit.com/640x480/9-01.jpg)

![](https://cdn-learn.adafruit.com/assets/assets/000/050/451/medium800thumb/sensors_magent_detector.jpg?1516709383)

When you are looking at the top of the micro:bit, the compass chip is more or less behind button B.&nbsp;

Note that I have had problems with this project using battery power from the battery connector. However, its fine powered from a USB lead.

# JavaScript Block Code

When the program starts the micro:bit will automatically take you through a calibration process for the compass chip. This involves tilting the micro:bit around in a circular motion in order to light up a ring of LEDs on the display.&nbsp;

The JavaScript Blocks Code editor is embedded directly on this page below. From the editor, you can click on Download button (bottom right) and then copy the downloaded file onto your micro:bit. Alternatively, you can&nbsp;[Click here](https://makecode.com/_VYkAcm53Y7Y4)&nbsp;to open the editor in a separate browser tab.

https://makecode.com/_VYkAcm53Y7Y4

In this program the block _magnetic force_ is used to measure the intensity of the magnetic field near the compass chip on your micro:bit. This is used in the `on start` block to establish a `baseline` reading of magnetic strength with no magnet near the micro:bit. This happens when the micro:bit is first powered up or the reset button is pressed.

The `forever` loop repeatedly takes readings and checks to see if the absolute value of the difference between the new reading and the baseline reading is above 100 and if it is it displays the cross symbol on the display. The `absolute` value is just the value with the sign removed. This is necessary because, depending on which pole of the magnet is pointing towards the compass chip, the `magnetic force` reading may decrease rather than increase.

You may need to increase the threshold of 100 if the cross appears even without a magnet being near.

# MicroPython

To run the MicroPython version of the code, open up the&nbsp;[online Python editor here](http://python.microbit.org/)&nbsp;and paste the following code into the editor window.

```
from microbit import *

baseline = compass.get_field_strength() # Take a baseline reading of magnetic strength

while True:
  if abs(compass.get_field_strength() - baseline) &gt; 10000:
      # Magnetic field strength increased by 10000
      display.show(Image.NO)    # Show a cross symbol
  else:
      display.clear()
```

The program first `import`s the&nbsp;`microbit`&nbsp;library that contains the `compass` and `display` objects needed in the program.

The&nbsp;`while`&nbsp;loop works just like its JavaScript Blocks sibling. It repeatedly takes readings and checks to see if the absolute value of the difference between the new reading and the `baseline` reading is above 10000 and if it is it displays the cross symbol on the display. The&nbsp;`abs`&nbsp;value is just the value with the sign removed. This is necessary because, depending on which pole of the magnet is pointing towards the compass chip, the&nbsp;_magnetic force&nbsp;_reading may decrease rather than increase.

You may need to increase the threshold of 10000 if the cross appears even without a magnet being near.

# Arduino

Make sure that you have your Arduino environment set up for micro:bit by following&nbsp;[this guide](../../../../use-micro-bit-with-arduino/overview).

Now start a new Sketch by clicking on the&nbsp;_File_&nbsp;menu and&nbsp;_New_. Then paste the following code into the editor window.

```
#include "Wire.h"
#include &lt;SparkFun_MAG3110.h&gt;
#include &lt;Adafruit_Microbit.h&gt;

Adafruit_Microbit_Matrix microbit;
MAG3110 compass = MAG3110();  // The compass chip
int baseline = 0;             

void setup() {
  microbit.begin();
  compass.initialize();       // Initializes the compass chip
  compass.start();            // Puts the sensor in active mode
  baseline = readStrength();  // Take a baseline reading of magnetic strength
  delay(500);

}

void loop() {
  if (abs(readStrength() - baseline) &gt; 15000) {
    // Magnetic field strength increased by 15000
    microbit.show(microbit.NO);
  }
  else {
    microbit.clear();
  }
}

int readStrength() {
  int x, y, z;
  while(!compass.dataReady()) {};   // Wait for data to become available to read
  compass.readMag(&amp;x, &amp;y, &amp;z);      // Read data into variables
  // calculate the RMS power combining x, y and z readings
  int power = sqrt((sq(float(x)) + sq(float(y)) + sq(float(z))) / 3);
  return power;
}

```

Info: 

The Arduino version of the compass library does not include an equivalent to the _magnetic force_ block of the Blocks editor or the `get_field_strength` of MicroPython. This means that we have to write our own, called `readStrength`. This takes the x, y and z readings from the compass chip and combines them using a mathematical technique called [RMS](https://en.wikipedia.org/wiki/Root_mean_square) (root mean square). That takes a kind of average of the readings.

You may need to increase the threshold of 15000 if the cross appears even without a magnet being near.

# micro:bit Lesson 1. Using the Built-in Sensors

## Accelerometer 

An accelerometer chip measures the forces acting on a tiny weight in each of three dimensions. If you imagine the micro:bit sitting flat on a desk, the x dimension is left to right; the y dimension front to back and the z dimension into and out of the desk.

![](https://cdn-learn.adafruit.com/assets/assets/000/050/175/medium800/sensors_F09_02.png?1516029972)

If the micro:bit is perfectly flat, the forces on the x and y dimensions will be zero. The force acting on the z axis will have a value (when read in your JavaScript Blocks or Python code) of about -1000 due to the pull of gravity. If you then tilt the back of the micro:bit up then the force in the y dimension will increase a little.

Its these changes that allow you to use an accelerometer to detect the orientation of the micro:bit, since gravity will always be acting in the same downwards direction.

# Movement Alarm Example

As an example of using the accelerometer lets make a sensitive movement alarm from out micro:bit.

The project works like this:&nbsp;

**When you press button A, the display changes top show a triangle, indicating that the alarm will arm itself after two seconds. Once armed, the display will be blank.**

**If you now try and move the micro:bit then the LEDs will all light indicating that the alarm has been triggered. Put the micro:bit down, so that it's still and then press button A to arm it again.**

![](https://cdn-learn.adafruit.com/assets/assets/000/050/454/medium800thumb/sensors_movement_alarm.jpg?1516723626)

# JavaScript Block Code

Here is the JavaScript Block version of the alarm program.&nbsp;The JavaScript Blocks Code editor is embedded directly on this page below. From the editor, you can click on Download button (bottom right) and then copy the downloaded file onto your micro:bit. Alternatively, you can&nbsp;[Click here](https://makecode.com/_XoedDaJvhbMD)&nbsp;to open the editor in a separate browser tab.

https://makecode.com/_XoedDaJvhbMD

The code works like this:

The _on start_ block will be run the first time the program runs after the micro:bit has been powered-up or reset. This sets a variable `z` (the acceleration in the z axis) to zero. It also sets a Boolean (true or false) variable (`alarm triggered`) to false. The variable&nbsp;`alarm triggered` will be used to indicate to other parts of the program that the alarm has been triggered.

The `forever` loop will run over and over again and is in three parts, each part being an `if`/`then` block.&nbsp;

The first `if`/`then` block checks for a press of button A, and if button A is pressed, it displays a triangle symbol, waits two seconds (2000 milliseconds) then reads the acceleration in the z axis into the variable `z`. Finally it clears the screen.

The second if/then in the `forever` block checks the current acceleration in the z axis against the value stored in the variable `z` and if it is less than the stored reading by a margin of 30, it sets the variable `alarm triggered`&nbsp; to be true.

The final part of the code determines what happens when the `alarm triggered` variable is true. If the alarm is triggered, then the _cross_ symbol will be shown on the display otherwise the display will be cleared. The `alarm triggered` variable will remain true until button A is pressed again.

# MicroPython

Here is the MicroPython version of the code for the movement alarm.

```
from microbit import *

z = 0
alarm_triggered = False

while True:
    if button_a.was_pressed():
        # Wait for 2 seconds while the alarm is armed
        display.show(Image.TRIANGLE)
        sleep(2000)
        z = accelerometer.get_z() # take a reading while still
        alarm_triggered = False   # the alarm is now armed
    # Check to see if acceleration on z axis changed by 30
    if accelerometer.get_z() &lt; z - 30:
        alarm_triggered = True
    if alarm_triggered:
        display.show(Image.NO)    # display a cross
    else:
        display.clear()
```

The code is almost a line by line copy of the JavaScript Blocks code, but translated into Python.

# Arduino

The Arduino version of the program is listed below and contains a few differences from the other two versions of the code.

```
#include "Wire.h"
#include "MMA8653.h"
#include &lt;Adafruit_Microbit.h&gt;

const int buttonA = 5;     // the number of the pushbutton pin

const uint8_t triangle[] = { 
  B00000,
  B00100,
  B01010,
  B11111,
  B00000 
};

Adafruit_Microbit_Matrix microbit;
MMA8653 accelerometer;
int z = 0;
boolean alarmTriggered = false;

void setup() {
  pinMode(buttonA, INPUT);  
  microbit.begin();              // start the display
  accelerometer.begin(false, 2); // 8-bit mode, 2g range
}

void loop() {
  accelerometer.update();
  if (digitalRead(buttonA) == LOW) {
    // Wait for 2 seconds while the alarm is armed
    microbit.clear();
    microbit.show(triangle);
    delay(2000);
    z = accelerometer.getZ();         // take a reading while still
    alarmTriggered = false;           // the alarm is now armed
  }
  // Check to see if acceleration on z axis changed by 5
  if (accelerometer.getZ() &lt; z - 5) {
    alarmTriggered = true;
  }
  if (alarmTriggered) {
    microbit.clear();
    microbit.show(microbit.NO);  // display a cross
  }
  else {
    microbit.clear();
  }
}
```

The main thing about the Arduino version of the code is that there are no predefined triangle symbol to use with the display and so you have to define this yourself as the `triangle` byte array.

Each byte array is an array of 5 bytes each of eight bits. Each bit can be either on or off (1 or 0). If the bit is 1 then the LED in that position will be lit otherwise it won't. If you look carefully at the byte arrays triangle and cross, you can just make out the bit pattern.

The `setup` function (like the on start block in JavaScript Blocks) is only run when the micro:bit starts or is reset. In this case the button A pin has to be defined as an input explicitly. The `microbit.begin `method starts the refreshing of the micro:bit's screen.&nbsp;

The `loop` function will be run repeatedly and does actually work just like the JavaScript Blocks and MicroPython code.&nbsp;

# micro:bit Lesson 1. Using the Built-in Sensors

## Other Things to Do

You might like to experiment with the code for the projects in this lesson. You could for example make both the magnet detector and accelerometer project more sensitive. You could change the magnet detector project so that it counted the number of times a magnet was brought close to it. This technique of a magnet and magnetic sensor can be used to count the rotations of a wheel.

# About the Author

If you have found this lesson useful, and want to learn Python, then you might like my book Programming the BBC micro:bit: Getting Started with MicroPython.&nbsp;

![](https://cdn-learn.adafruit.com/assets/assets/000/050/453/medium800/sensors_cover.jpg?1516722940)


## Featured Products

### BBC micro:bit

[BBC micro:bit](https://www.adafruit.com/product/3530)
The British Invasion is here! No, not music...microcontrollers! New to the USA is the newest and _easiest_ way to learn programming and electronics - the **BBC micro:bit**.

Designed specifically for kids and beginners, the&nbsp; **micro:bit** &nbsp;is a...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/3530)
[Related Guides to the Product](https://learn.adafruit.com/products/3530/guides)
### BBC micro:bit Go Bundle

[BBC micro:bit Go Bundle](https://www.adafruit.com/product/3362)
Discontinued - [**you can grab** micro:bit v2 Go Bundle - Batteries and USB Cable Included **instead!&nbsp;**](https://www.adafruit.com/product/4834)

The British Invasion is here! No, not music...microcontrollers! New to the USA is the newest and _easiest_ way to...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/3362)
[Related Guides to the Product](https://learn.adafruit.com/products/3362/guides)
### High-strength 'rare earth' magnet

[High-strength 'rare earth' magnet](https://www.adafruit.com/product/9)
Yow! These things are super powerful. .47" diameter and .18" thick discs. Great for use with your SpokePOV Kit. If you have an aluminium frame, use tape/foam sticky tape to attach it.

In Stock
[Buy Now](https://www.adafruit.com/product/9)
[Related Guides to the Product](https://learn.adafruit.com/products/9/guides)

## Related Guides

- [Circuit Playground's Motion Sensor](https://learn.adafruit.com/circuit-playgrounds-motion-sensor.md)
- [No-Code WipperSnapper Water Detection](https://learn.adafruit.com/water-detection-with-wippersnapper.md)
- [Big Key Switches Macro Pad](https://learn.adafruit.com/big-key-switch.md)
- [Adafruit Swirly Aluminum Mounting Grid for 0.1" Spaced PCBs](https://learn.adafruit.com/swirly-grid.md)
- [Facial Detection and Recognition with MEMENTO](https://learn.adafruit.com/facial-detection-and-recognition-with-memento.md)
- [Adafruit LPS33/LPS35 Water Resistant Pressure Sensor](https://learn.adafruit.com/lps35hw-water-resistant-pressure-sensor.md)
- [Skill Badge Requirements: Solar](https://learn.adafruit.com/skill-badge-requirements-solar.md)
- [Adafruit IO Basics: Digital Input](https://learn.adafruit.com/adafruit-io-basics-digital-input.md)
- [How to Scan and Detect I2C Addresses](https://learn.adafruit.com/scanning-i2c-addresses.md)
- [Adafruit SensorLab - Magnetometer Calibration](https://learn.adafruit.com/adafruit-sensorlab-magnetometer-calibration.md)
- [sino:bit MicroPython](https://learn.adafruit.com/sino-bit-micropython.md)
- [Bluetooth LE MIDI Controller](https://learn.adafruit.com/bluetooth-le-midi-controller.md)
- [Sensors in MakeCode](https://learn.adafruit.com/sensors-in-makecode.md)
- [Motion-Activated Solder Fume Extractor With Lamp](https://learn.adafruit.com/motion-activated-solder-fume-extractor-with-lamp.md)
- [Adafruit ENS160 MOX Gas Sensor](https://learn.adafruit.com/adafruit-ens160-mox-gas-sensor.md)
