uBit.thermometer

Overview

MicroBitThermometer provides access to the surface temperature of the nrf51822. The temperature reading therefore is not representative of the ambient temperature, but rather the temperature relative to the surface temperature of the chip.

However, we can make it representative of the ambient temperature in software through "calibrating" the thermometer.

Calibration is very simple, and is calculated by giving the current temperature to the setCalibration() member function. From the temperature, an offset is calculated, and is subsequently used to offset future temperature readings.

Real time updates

When using the standard uBit presentation, the thermometer is continuously updated in the background using an idle thread (after it is first used), which is executed whenever the micro:bit has no other tasks to perform.

If there is no scheduler running, the values are synchronously read on getTemperature() calls. Additionally, if you would like to drive thermometer updates manually updateSample() can be used.

Message Bus ID

Constant Value
MICROBIT_ID_THERMOMETER 28

Message Bus Events

Constant Value
MICROBIT_THERMOMETER_EVT_UPDATE 1

API

Constructor


MicroBitThermometer(
MicroBitStorage &
_storage)

Description

Constructor. Create new MicroBitThermometer that gives an indication of the current temperature.

Parameters

MicroBitStorage &
_storage - an instance of MicroBitStorage used to persist temperature offset data

Example
 MicroBitStorage storage; 
 MicroBitThermometer thermometer(storage); 


MicroBitThermometer(
MicroBitStorage &
_storage,
uint16_t
id)

Description

Constructor. Create new MicroBitThermometer that gives an indication of the current temperature.

Parameters

MicroBitStorage &
_storage - an instance of MicroBitStorage used to persist temperature offset data

uint16_t
id - the unique EventModel id of this component. Defaults to MICROBIT_ID_THERMOMETER.

Example
 MicroBitStorage storage; 
 MicroBitThermometer thermometer(storage); 


MicroBitThermometer()

Description

Constructor. Create new MicroBitThermometer that gives an indication of the current temperature.

Example
 MicroBitThermometer thermometer; 


MicroBitThermometer(
uint16_t
id)

Description

Constructor. Create new MicroBitThermometer that gives an indication of the current temperature.

Parameters

uint16_t
id - the unique EventModel id of this component. Defaults to MICROBIT_ID_THERMOMETER.

Example
 MicroBitThermometer thermometer; 

setPeriod


void
setPeriod
(
int
period)

Description

Set the sample rate at which the temperatureis read (in ms).

The default sample period is 1 second.

Parameters

int
period - the requested time between samples, in milliseconds.

Note

the temperature is always read in the background, and is only updated when the processor is idle, or when the temperature is explicitly read.

getPeriod


int
getPeriod
()

Description

Reads the currently configured sample rate of the thermometer.

Returns

The time between samples, in milliseconds.

setOffset


int
setOffset
(
int
offset)

Description

Set the value that is used to offset the raw silicon temperature.

Parameters

int
offset - the offset for the silicon temperature

Returns

MICROBIT_OK on success

getOffset


int
getOffset
()

Description

Retreive the value that is used to offset the raw silicon temperature.

Returns

the current offset.

setCalibration


int
setCalibration
(
int
calibrationTemp)

Description

This member function fetches the raw silicon temperature, and calculates the value used to offset the raw silicon temperature based on a given temperature.

Parameters

int
calibrationTemp - the temperature used to calculate the raw silicon temperature offset.

Returns

MICROBIT_OK on success

getTemperature


int
getTemperature
()

Description

Gets the current temperature of the microbit.

Returns

the current temperature, in degrees celsius.

Example
 thermometer.getTemperature(); 

updateSample


int
updateSample
()

Description

Updates the temperature sample of this instance of MicroBitThermometer only if isSampleNeeded() indicates that an update is required.

This call also will add the thermometer to fiber components to receive periodic callbacks.

Returns

MICROBIT_OK on success.