# Lesson 07 Temperature Sensor ## Introduction

Temperature sensor is a kind of sensor that can detect temperature and transfer it into output data. Temperature sensor is the core component of temperature gauges and instruments that you see in many devices. It has multiple applications. In this experiment, we are going to learn about the analog temperature sensor–TMP36 and display its data on the screen of micro:bit.

## What you need

• micro:bit board
• Micro USB Cable
• TMP36 Temperature Sensor
• Male to male jumper wires

## TMP36 Temperature Sensor

TMP36 is a kind of analog temperature sensor. Its output voltage and temperature forms a linear relationship. That means higher temperature will have bigger output voltage. When we look at the front side marked “TMP36”, the left pin of the core is VCC, middle is Vout, and the right is GND. Be careful when connected these pins to your circuit otherwise it will get damaged and potentially get very hot. Here’s the curve chart for output voltage of TMP36 changing with temperature:  We can know from the above chart that the temperature formula is:

Temperature（℃）=(Output Voltage（mV）-500)/10

## Hardware Connection After connecting the circuit, you will see the following: ## Programming

Open Microsoft Makecode, write your code in the edit area.

Of course, you can see the whole program in the link below directly. Just click “Edit” on the top right corner of the interface, then click “Download” on the bottom right corner to download code into micro:bit directly.

Link of the whole program: https://makecode.microbit.org/_Csja8Y2koEyi

## Code Explanation

map

Remaps the specified value from one range to another. This function maps the value of from low to the value of to low, the value of from high to the value of to high, and intermediate values to intermediate values.

This function does not constrain values to the ranges, because out-of-range values are sometimes intended and useful. If you need to limit a range, you can use the Math.clamp function before or after calling this function. In our program, we have to use block Map to transfer the read-out analog data into voltage(mV).
If analog read value is 0 to 1023, so from low is 0, from high is 1023.
The basic voltage of Micro:bit is 3.3V, i.e. 3300Mv. So to low is 0, to high is 3300. Temperature（℃）=(Output Voltage（mV）-500)/10
Then according to the formula above to calculate temperature value.

## Result

micro:bit screen displays the present temperature value. ## Taking it further

In this experiment, micro:bit displays the Centigrade temperature. If we want to display the Fahrenheit temperature, then how would you program it?

## micro:bit Starter Kit Lessons

• Lesson 01 – LED
• Lesson 02 – Button
• Lesson 03 – Trimpot
• Lesson 04 – Photocell
• Lesson 05 – RGB LED
• Lesson 06 – Self-lock Switch
• Lesson 07 – Temperature Sensor
• Lesson 08 – Servo
• Lesson 09 – Buzzer
• Lesson 10 – Motor
• Lesson 11 – Rainbow LED
• Lesson 12 – Accelerometer
• Lesson 13 – Compass
• Lesson 14 – Ambient Light

First published at 2:14pm on July 9, 2019
Last updated at 2:14pm on July 9, 2019