Skip to content

uBit.thermometer

Overview

uBit.thermometer provides access to the surface temperature of the micro:bit. This is provided on-core by the chipset used on the micro:bit: the Nordic NRF52.

Using the thermometer

As with most measuring components on the micro:bit, you can subscribe to events posted by the thermometer; in this case, there is only one event, which is fired when a temperature change has been detected.

Note

See Message Bus for more information about events and handlers.

Getting ambient temperature

The temperature reading by default is not representative of the ambient temperature, as the chip used on the board has not been calibrated; one chip's idea of a temperature may by X degrees, and other something different. (They will still accurately measure temperature difference though, so that a change in 2 degrees will be shown as a difference of 2 degrees regardless of original reference.)

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.

Here is a snippet to show ambient temperature by calibrating the thermometer to the current board temperature and then printing it out on the display:

Example

1
2
3
4
5
6
7
8
9
int temp;

uBit.thermometer.setCalibration(uBit.thermometer.getTemperature());

while(true) {
    temp = uBit.thermometer.getTemperature();  // This is now calibrated!
    uBit.display.print(temp);
    uBit.sleep(1000);
}

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 for some reason there is no scheduler running, you can drive thermometer updates manually with requestUpdate().

Message Bus Info

Message Bus IDs

Constant Value
DEVICE_ID_THERMOMETER 8

Message Bus Events

Constant Value
MICROBIT_THERMOMETER_EVT_UPDATE 1

API

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.


setCalibration

int
setCalibration
(
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


getCalibration

int
getCalibration
()

Description

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

Returns

the current offset.


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.


Component Constructor

Advanced users only

Do not use this unless you really know what you're doing. It's usually best to use uBit.

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 DEVICE_ID_THERMOMETER.

Example
 MicroBitThermometer thermometer;