Check Board Version

The original version of the micro:bit has two separate sensors for the accelerometer and the magnetometer. Newer versions have a single sensor. To check which version you have, look on the board in the area shown below.

If you have two sensors then use the following libraries:

  • Magnetometer = MAG3110
  • Accelerometer = MMA8653

If you have a single sensor, then use the following library:

  • Magno/Accelo = LSM303AGR

Magnetometer (MAG3110)

Lets start with the magnetometer chip, the MAG3110

We can talk to the chip using an Arduino library

You can download Sparkfun's library by clicking the button below!

And read our guide on how to install libraries

Restart the IDE. Now you can upload some examples. I suggest starting with the Basic example which is replicated below

/*  ********************************************* 
 *  SparkFun_MAG3110_Basic
 *  Triple Axis Magnetometer Breakout - MAG3110 
 *  Hook Up Guide Example 
 *  Utilizing Sparkfun's MAG3110 Library
 *  A basic sketch that reads x y and z readings
 *  from the MAG3110 sensor
 *  George B. on behalf of SparkFun Electronics
 *  Created: Sep 22, 2016
 *  Updated: n/a
 *  Development Environment Specifics:
 *  Arduino 1.6.7
 *  Hardware Specifications:
 *  SparkFun MAG3110
 *  Bi-directional Logic Level Converter
 *  Arduino Micro
 *  This code is beerware; if you see me (or any other SparkFun employee) at the
 *  local, and you've found our code helpful, please buy us a round!
 *  Distributed as-is; no warranty is given.
 *  *********************************************/

#include <SparkFun_MAG3110.h>

MAG3110 mag = MAG3110(); //Instantiate MAG3110

void setup() {

  mag.initialize(); //Initializes the mag sensor
  mag.start();      //Puts the sensor in active mode

void loop() {

  int x, y, z;
  //Only read data when it's ready
  if(mag.dataReady()) {
    //Read the data
    mag.readMag(&x, &y, &z);
    Serial.print("X: ");
    Serial.print(", Y: ");
    Serial.print(", Z: ");

Upload this to the microbit to see the following raw data:

Note that the magnetometer is not calibrated, so you'll get different numbers on XYZ but when you twist and rotate the mirobit the numbers should move up and down a bit! (This is why magnetometers must be calibrated)

Accelerometer (MMA8653)

The microbit has an onboard 3-axis accelerometer as well!

You can use this akafugu MMA8653 to communicate with it:

Install like other libraries!

Next up, run this example code:

 * MMA845XQ test code
 * (C) 2012 Akafugu Corporation
 * This program is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE.  See the GNU General Public License for more details.

#include "Wire.h"
#include "MMA8653.h"

MMA8653 accel;

void setup() {

  Serial.println("microbit accel test");
  accel.begin(false, 2); // 8-bit mode, 2g range

void loop() {
  Serial.print(accel.getX());    Serial.print(" , ");
  Serial.print(accel.getY());    Serial.print(", ");


And open the serial monitor to see the X Y and Z acceleration data points!

This library is pretty old and incomplete so at this time you can only use it in 8-bit mode. If you want to get the data in g's use this for the loop:

void loop() {
  Serial.print((float)accel.getX() * 0.0156);    Serial.print(" , ");
  Serial.print((float)accel.getY() * 0.0156);    Serial.print(", ");
  Serial.println((float)accel.getZ() * 0.0156);


Combined Accelerometer / Magnetometer (LSM303AGR)

Newer versions of the micro:bit have a single LSM303AGR sensor which has both an accelerometer and a magnetometer:

To use this version, you can use the LSM303AGR library from here:

You can install it via the Library Manager. Just search for LSM303AGR and pick the one from STM32duino:

The library comes with one example. Running it should give you the following output: 

This guide was first published on Sep 27, 2017. It was last updated on Mar 08, 2024.

This page (Accelerometer and Magnetometer) was last updated on Mar 08, 2024.

Text editor powered by tinymce.