micro:bit and MCP9808 digital temperature sensor example

MCP9808 digital temperature sensor example using the Arduino IDE

The MCP9808 digital temperature sensor converts temperatures between -20°C and +100°C to a digital word with ±0.5°C (max.) accuracy. The MCP9808 comes with user-programmable registers that provide flexibility for temperature sensing applications. The registers allow user-selectable settings such as Shutdown or low-power modes and the specification of temperature Event and Critical output boundaries.

When the temperature changes beyond the specified boundary limits, the MCP9808 outputs an Event signal. The user has the option of setting the event output signal polarity as an active-low or active-high comparator output for thermostat operation, or as temperature event interrupt output for microprocessor-based systems. The event output can also be configured as a Critical temperature output. This sensor has an industry standard 2-wire, SMBus and Standard I2C™Compatible compatible (100kHz/400kHz bus clock) serial interface, allowing up to eight sensors to be controlled in a single serial bus.


±0.25°C (typical) from -40°C to +125°C
±0.5°C (maximum) from -20°C to +100°C

User Selectable Measurement Resolution:
0.5°C, 0.25°C, 0.125°C, 0.0625°C

User Programmable Temperature Limits:
Temperature Window Limit
Critical Temperature Limit

User Programmable Temperature Alert Output
Operating Voltage Range: 2.7V to 5.5V

More details about the sensor at http://www.microchip.com/wwwproducts/en/MCP9808

This typically comes in a breakout such as the one in the breakout below

mcp9808 breakout

mcp9808 breakout


Connect Vin to Microbit 3v3
Connect GND to Microbit Gnd
Connect SCL to Microbit P19
Connect SDA to Microbit P20


This is an arduino example

You will need the adafruit MCP9808 library which is available at https://github.com/adafruit/Adafruit_MCP9808_Library/archive/master.zip

#include <Wire.h>
#include "Adafruit_MCP9808.h"
// Create the MCP9808 temperature sensor object
Adafruit_MCP9808 tempsensor = Adafruit_MCP9808();
void setup() {
  if (!tempsensor.begin()) 
    Serial.println("Couldn't find MCP9808!");
    while (1);
void loop() {
  // Read and print out the temperature, then convert to *F
  float c = tempsensor.readTempC();
  float f = c * 9.0 / 5.0 + 32;
  Serial.print("Temp: "); 
  Serial.print(" C\t"); 
  Serial.println(" F");



Open the serial monitor and you should see something like this

Temp: 19.62 C 67.32 F
Temp: 19.56 C 67.21 F
Temp: 19.50 C 67.10 F
Temp: 21.12 C 70.03 F
Temp: 22.81 C 73.06 F
Temp: 23.87 C 74.97 F
Temp: 23.81 C 74.86 F
Temp: 23.50 C 74.30 F
Temp: 23.25 C 73.85 F
Temp: 23.00 C 73.40 F



Adafruit MCP9808 High Accuracy I2C Temperature Sensor Breakout Board [ADA1782]

Arduino and the micro:bit onboard 3-axis accelerometer

The MMA8653FC is an intelligent, low-power, three-axis, capacitive micromachined accelerometer with 10 bits of resolution. This accelerometer is packed with embedded functions with flexible user-programmable options, configurable to two interrupt pins. Embedded interrupt functions enable overall power savings, by relieving the host processor from continuously polling data. There is access to
either low-pass or high-pass filtered data, which minimizes the data analysis required for jolt detection and faster transitions. The device can be configured to generate inertial wake-up interrupt signals from any combination of the configurable embedded functions, enabling the MMA8653FC to monitor inertial events while remaining in a low-power mode during periods of inactivity. The MMA8653FC is available in a small 10-pin DFN package (2 mm x 2 mm x 1 mm).

• 1.95 V to 3.6 V supply voltage
• 1.62 V to 3.6 V digital interface voltage
• ±2 g, ±4 g, and ±8 g dynamically selectable full-scale ranges
• Output Data Rates (ODR) from 1.56 Hz to 800 Hz
• 10-bit digital output
• I2C digital output interface with programmable interrupts
• One embedded channel of configurable motion detection (Freefall)
• Orientation (Portrait/Landscape) detection with fixed hysteresis of 15°.
• Configurable automatic ODR change triggered by the Auto-Wake/Sleep state change
• Self-Test

Typical applications
• Tilt compensation in e-compass applications
• Static orientation detection (Portrait/Landscape, Up/Down, Left/Right, Back/ Front position identification)
• Notebook, tablet, e-reader, and laptop tumble and freefall detection
• Real-time orientation detection (virtual reality and gaming 3D user orientation feedback)
• Real-time activity analysis (pedometer step counting, freefall drop detection for HDD, dead-reckoning GPS backup)
• Motion detection for portable product power saving (Auto-SLEEP and Auto-WAKE for cell phone, PDA, GPS, gaming)
• Shock and vibration monitoring (mechatronic compensation, shipping and warranty usage logging)
• User interface (tilt menu scrolling)


You need to the https://github.com/hidnseek/hidnseek/tree/master/arduino/libraries/MMA8653

#include "Wire.h"
#include "MMA8653.h"
MMA8653 accel;
void setup() 
  accel.begin(false, 2); // 8-bit mode, 2g range
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);



Open the serial monitor and move the micro:bit – the values are in g’s

0.66 , -0.06, 0.69
0.22 , -0.19, 1.25
0.09 , 0.50, -0.50
-0.36 , 0.67, -0.20
0.86 , -0.17, 0.37
0.17 , 0.12, -1.03
0.23 , -0.31, -1.37
0.64 , 1.51, 1.03
0.94 , -0.39, -1.64
0.31 , 0.33, -0.27
-0.42 , 0.56, 0.28
-0.94 , 0.25, -0.45
-0.19 , 0.05, -0.83
-1.12 , -0.12, -0.14
-0.48 , 0.19, -0.59
-0.45 , 0.28, 0.23

Use the micro:bit magnetometer with the Arduino IDE

Freescale’s MAG3110 is a small, low-power, digital 3-axis magnetometer. The device can be used in conjunction with a 3-axis accelerometer to realize an orientation independent electronic compass that can provide accurate heading information. It features a standard I2C serial interface output and smart embedded functions.

The MAG3110 is capable of measuring magnetic fields with an output data rate (ODR) up to 80 Hz; these output data rates correspond to sample intervals from 12.5 ms to several seconds. The MAG3110 is available in a plastic DFN package and it is guaranteed to operate over the extended temperature range of -40°C to +85°C.

You have a micro:bit so you already have one on the board



You need to import the Sparkfun library for the MAG3110 to run this example – https://github.com/sparkfun/SparkFun_MAG3110_Breakout_Board_Arduino_Library/archive/master.zip


#include <SparkFun_MAG3110.h>
MAG3110 mag = 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
    //Read the data
    mag.readMag(&x, &y, &z);
    Serial.print("X: ");
    Serial.print(", Y: ");
    Serial.print(", Z: ");



Open the serial monitor and move your micro:bit through the various axis, you should see something like this


X: 218, Y: 65109, Z: 65274
X: 71, Y: 64965, Z: 49
X: 598, Y: 65036, Z: 248
X: 423, Y: 65338, Z: 564
X: 348, Y: 61, Z: 565
X: 413, Y: 65532, Z: 576
X: 502, Y: 65024, Z: 65352

Program your micro:bit with the Arduino IDE

You can now use the Arduino IDE to develop and program your Micro:Bit, in my view thats a good thing to add this support as its a popular development tool that is used for many other boards

Arduino assumes there’s a ‘softdevice’ radio already installed.

Flashing a SoftDevice

This is the instructions from https://github.com/sandeepmistry/arduino-nRF5

cd <SKETCHBOOK>, where <SKETCHBOOK> is your Arduino Sketch folder:
OS X: ~/Documents/Arduino
Linux: ~/Arduino
Windows: ~/Documents/Arduino
Create the following directories: tools/nRF5FlashSoftDevice/tool/
Download nRF5FlashSoftDevice.jar to <SKETCHBOOK>/tools/nRF5FlashSoftDevice/tool/
Restart the Arduino IDE
Select your nRF board from the Tools -> Board menu
Select a SoftDevice from the Tools -> “SoftDevice: ” menu
Select a Programmer (J-Link, ST-Link V2, or CMSIS-DAP) from the Tools -> “Programmer: ” menu
Select Tools -> nRF5 Flash SoftDevice
Read license agreement
Click “Accept” to accept license and continue, or “Decline” to decline and abort
If accepted, SoftDevice binary will be flashed to the board


Download this zip file, extract it and drag it into your MICROBIT drive – microbit-adv

Add Arduino Support

In the Arduino IDE, go to Preferences and add https://sandeepmistry.github.io/arduino-nRF5/package_nRF5_boards_index.json into the Additional Board Manager URL text box.

Open Tools>Board>Boards Manager from the menu bar, search for nRF5 and install “Nordic Semiconductor nRF5 Boards” by Sandeep Mistry


Select BBC micro:bit from the Boards menu
Set SoftDevice to S110
And set the Port to the microbit com port


Lets play with the push buttons

You may have to increasethe delay(150), I used this to try and get rid of simple multiple presses and switch bounce

const int buttonA = 5; 
const int buttonB = 11;
void setup() 
  pinMode(buttonA, INPUT);  
  pinMode(buttonB, INPUT);   
void loop()
  if (! digitalRead(buttonA)) 
    Serial.println("Button A pressed");
  if (! digitalRead(buttonB)) 
    Serial.println("Button B pressed");



Open the serial monitor and press the buttons

Button A pressed
Button B pressed
Button A pressed
Button A pressed
Button B pressed
Button B pressed
Button B pressed
Button A pressed
Button A pressed
Button B pressed
Button B pressed
Button A pressed
Button A pressed