SparkFun Inventor's Kit for micro:bit Experiment Guide

Pages
Contributors: D___Run___
Favorited Favorite 6

Experiment 7: Reading the Temperature Sensor

Introduction

A temperature sensor is exactly what it sounds like — a sensor used to measure ambient temperature. In this experiment you will add an external package of code to MakeCode that will make using the temperature sensor easier and then put the sensor to good use… measuring the air temperature.

Parts Needed

You will need the following parts:

  • 1x micro:bit
  • 1x Micro B USB Cable
  • 1x micro:bit Breakout (with Headers)
  • 1x Breadboard
  • 5x Jumper Wires
  • 1x TMP36 Temperature Sensor

Didn’t Get the SIK for micro:bit?

If you are conducting this experiment and didn’t get the Inventor’s Kit, we suggest using these parts:

micro:bit Board

DEV-14208
$14.95
5
Breadboard - Full-Size (Bare)

PRT-12615
$5.95
21
SparkFun micro:bit Breakout (with Headers)

BOB-13989
$5.49
1
Jumper Wires Standard 7" M/M - 30 AWG (30 Pack)

PRT-11026
$1.95
20
Temperature Sensor - TMP36

SEN-10988
$1.50
16

Introducing the TMP36 Temperature Sensor

The TMP36 is a low-voltage precision centigrade temperature sensor. It provides a voltage output that is linearly proportional to the Celsius temperature. It also doesn’t require any external calibration to provide typical accuracies of ±1°C at +25°C and ±2°C over the −40°C to +125°C temperature range. The output voltage can easily convert to temperature using the scale factor of 10 mV/°C.

TMP36

If you are looking at the flat face with text on it, the center pin is your signal pin, the left-hand pin is supply voltage (3.3V in this tutorial), and the right-hand pin connects to ground.

Pro Tip: The TMP36 looks a lot like a transistor. Put a dot of fingernail polish on the top of your TMP36 so it’s easy to find.

Hardware Hookup

Ready to start hooking everything up? Check out the wiring diagram below to see how everything is connected.

Pay special attention to the component’s markings indicating how to place it on the breadboard. Polarized components can only be connected to a circuit in one direction.

The temperature sensor can only be connected to a circuit in one direction. See below for the pin outs of the temperature sensor --- TMP36.

Wiring Diagram for the Experiment

Fritzing Diagram: Exp 7

Having a hard time seeing the circuit? Click on the wiring diagram for a closer look.

Run Your Script

Either copy and paste, or re-create the following code into your own MakeCode editor by clicking the open icon in the upper right-hand corner of the editor window. You can also just download this example by clicking the download button in the lower right-hand corner of the code window.

Note: You may need to disable your ad/pop-up blocker to interact with the MakeCode programming environment and simulated circuit!

Code to Note

Let’s take a look at the code blocks in this experiment.

alt text

If you are having a hard time viewing this code, click on the image above to get a better look!

Adding an External Package

In MakeCode you can write external pieces of code that allows you to create and use custom blocks for specific applications and/or components that you could use with the micro:bit. We wrote a simple package for the TMP36 temperature sensor to cut out a lot of complicated math for you to do.

To add a package to your MakeCode editor first click on Advanced and then Add Packages….

add packages

This will bring up a search box. Feel free to search for different packages to play with, but for this specific example you need to search for a specific package. Please copy the link below and paste it into your search box.

language:text
https://github.com/sparkfun/pxt-tmp36

This should bring up the TMP36 package that we wrote. To add the package to your sketch just click on the package once it appears in the search output!

Add Package Dialog

Once you select the TMP36 package there should be a TMP36 code block drawer to select from in your blocks menu.

Get temp screen shot

This drawer has only one block in it and that is a block that returns the temperature from a TMP36 sensor connected to a specific pin in fahrenheit or celcius.

Show Number

The Show Number block accepts a number value and then displays it on the LED array. That’s it! Pretty simple. If the number is longer than a single digit, it will automatically scroll the value for you.

Get Temperature on Pin

The only block in the TMP36 package is the Get Temperature on Pin block. This block does all of the hard math for you to get an accurate temperature from the TMP36 sensor. You select the pin the sensor is connected to and what unit of measure you would prefer (fahrenheit or celcius). If you want, you can also get the raw 10 bit value as well (0-1023).

What You Should See

When your micro:bit turns on, the temperature reading from the TMP36 temperature sensor will be displayed and scrolled across the LED array.

Exp 7 Final Assembly

Troubleshooting

Temperature Value is Unchanging

Try pinching the sensor with your fingers to heat it up or pressing a bag of ice against it to cool it down.

Temperature Sensor is Really Hot!

You have wired it backward! Unplug your micro:bit immediately, let the sensor cool down, and double-check your wiring. If you catch it soon enough, your sensor may not have been damaged and may still work.