Skip main navigation

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 temperatureGet 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 hourGet 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 hourGet, 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 dataRemove 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 hourUse 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 arrayAn 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.

This article is from the free online

Build a Physical Computing Prototype

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education