SparkFun Inventor's Kit for micro:bit Experiment Guide

Contributors: D___Run___, bboyho
Favorited Favorite 13

Experiment 7: Reading the Temperature Sensor


A temperature sensor is exactly what it sounds like --- a sensor used to measure ambient temperature. While there is a temperature sensor built into the micro:bit, we will take this opportunity to learn how to wire an external temperature sensor to measure something that is away from the micro:bit. You will learn how to add an external extension 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 v2 Board

micro:bit v2 Board

SparkFun Qwiic micro:bit Breakout (with Headers)

SparkFun Qwiic micro:bit Breakout (with Headers)

Breadboard - Full-Size (Bare)

Breadboard - Full-Size (Bare)

USB Micro-B Cable - 6 Foot

USB Micro-B Cable - 6 Foot

Temperature Sensor - TMP36

Temperature Sensor - TMP36

Jumper Wires Standard 7" M/M - 30 AWG (30 Pack)

Jumper Wires Standard 7" M/M - 30 AWG (30 Pack)


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.


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.

Polarized Components 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 Extension

Heads up! Previously, these libraries were referred to as MakeCode packages. They are now referred to as MakeCode extensions.

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 extension for the TMP36 temperature sensor to cut out a lot of complicated math for you to do.

To add a extension to your MakeCode editor first click on Advanced and then Add Extensions....

MakeCode Extensions

This will bring up a search box. Feel free to search for different extensions to play with, but for this specific example you need to search for a specific extension. From here you can search for "SparkFun" or "gator-temp," and it should show up as a public extension in the list. Go ahead and click on it.

SparkFun Extensions

Heads up! Previously, the temperature extension was labeled as TMP36. The new extension name is the gator-temp. Functionally, they are the same! If you happen to see this a Network request failed error, this is because the extension name was adjusted to the gator-temp. You will need to rebuild any code using the TMP36 extension with the gator-temp extension.

micro:bit MakeCode network request failed error

Once you select the gator-temp extension there should be a temperature 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 Celsius.

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 extension 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 Celsius). 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


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 Reads Random 5-Digit Number

If you see an output with "65525", "65533", "65527", etc. on the LEDs, the issue might be due to a loose (or possibly shorted) connection between the 3.3V pin and the TMP36's voltage input pin. The analog pin will probably start reading random values or there is a slight current draw to the temperature sensor from the micro:bit. Make sure that you check your connections by ensuring that there are no loose connections or pins touching where they should not. If you are using alligator clips, make sure that the connection is secure as well and that there are no shorts (try sliding the sheath around the alligator clips for more insulation).

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.