Hurry, only 9 days left to get one year of Unlimited learning for £249.99 £174.99. New subscribers only. T&Cs apply

# How can I collect and display sensor data?

You can gather data from a temperature sensor and display it on the micro:bit’s screen. Here is an example of how to do it

You can gather data from a temperature sensor and display it on the micro:bit’s screen. Here is an example of how to do it.

Why do you want to measure temperature? First off: what temperature are we measuring — the outside temperature, inside temperature, or your own temperature?

You could place your device outside and try to measure the outside temperature. You will need a waterproof box for this!

For this example, we’re going to measure the inside temperature of a person’s home office, which is situated in their loft.

The house owner suspects the window in the loft is not sealed properly, and the room temperature is colder than other rooms in the house. To prove this, they are going to measure the temperature over several hours in different rooms of the house.

## 1 Purpose

Straight away, our physical computing challenge has a purpose: to determine if the loft is colder than other rooms. It also has scope: measure the temperature over several hours.

We need to be more exact with our scope but it’s a good start. Equipment wise: the micro:bit has a built-in temperature sensor and can display it on its LEDs. The battery will last for several hours.

## 2 Coding

The first programme to create is to measure the temperature and display it on the LEDs. We will code in blocks for this challenge.

Get and display temperature
(Click to expand)

Now we want to measure the temperature every hour. We’re going to display it, then pause the micro:bit for an hour, which is 3,600,000 milliseconds. Then the loop starts again: display, pause.

Get and display temperature every hour
(Click to expand)

## 3 Variables

Do we want to have to write down the temperature every hour? Let’s store the temperatures in variables. This means when we press the A button, we want the temperatures to show.

Below you can see the code to get four temperatures over three hours:

Get, store and display temperature every hour
(Click to expand)

But wait! Remember, the micro:bit’s temperature sensor runs too hot. We need to edit the data as we store it. We will use a block from the Math menu to subtract 5 from the data.

Remove 5 degrees from the data
(Click to expand)

## 4 Arrays

A more complex way of storing variables is to use an array. This way we can add as many temperature readings as we want without creating lots of variables.

Use an array to get, store and display temperature every hour
(Click to expand)

## 5 Simulation

If you don’t have a micro:bit, when creating the code, you can leave out the hour’s pause and bring it down to a few seconds. You can change the temperature the micro:bit is recording using the simulator.

## Variables and Arrays

In order to store information, we need to create structures in code. These structures are called variables.

A variable has three things:

1. a name
2. a data type
3. a value

In the above code, Hour1 is a variable.

• Its name is Hour1.
• Its data type is a number, or an integer.
• Its value is the temperature it records.

Variables are data. Arrays are also variables. They can store lots of other variables without naming them.

Instead of Hour1, Hour2, Hour3 and so forth, we have an array called AllHours and it stores as many hours as we want.

Here’s a visual representation of the AllHours array after six hours:

An array
(Click to expand)

Each variable in the array has an address. To view that variable, you need to call it by its address. The first hour is in address 0. It has the value 23.