SparkFun Inventor's Kit for micro:bit Experiment Guide
Experiment 7: Reading the Temperature Sensor
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.
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:
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.
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
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.
Code to Note
Let’s take a look at the code blocks in this experiment.
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….
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.
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!
Once you select the TMP36 package there should be a TMP36 code block drawer to select from in your blocks menu.
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 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.
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.