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.

Features

Accuracy:
±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

Wiring

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

Code

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

[codesyntax lang=”cpp”]

#include <Wire.h>
#include "Adafruit_MCP9808.h"

// Create the MCP9808 temperature sensor object
Adafruit_MCP9808 tempsensor = Adafruit_MCP9808();

void setup() {
  Serial.begin(9600);
  
  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); 
  Serial.print(" C\t"); 
  Serial.print(f); 
  Serial.println(" F");
  delay(250);
  
  tempsensor.shutdown_wake(1);
  delay(2000);
  tempsensor.shutdown_wake(0);
  
   
}

[/codesyntax]

 

Results

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

 

Links

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

Leave a Comment

This div height required for enabling the sticky sidebar
Ad Clicks : Ad Views : Ad Clicks : Ad Views :