Introduction: TMP36 Temperature Sensor With Arduino in Tinkercad
In this project, you will turn the Arduino into a thermometer! Use a temperature sensor to measure your skin temperature, and register the output with three LEDs. Even though the Arduino is a digital tool, it can interpret signals from an analog input, like the TMP36 temperature sensor, using the built in Analog-to-Digital (ADC) converter, accessed through the analog pins A0-A5, which you may have learned about in a previous lesson about analog input.
Explore the sample circuit embedded here by starting the simulation, clicking on the sensor, then dragging its temperature slider to adjust the simulated input and observe the resulting LED patterns.
In this lesson, you'll build this simulated circuit yourself along side the sample. To optionally build the physical circuit, gather up your Arduino Uno board, USB cable, solderless breadboard, three LEDs, three alike resistors (any value from 100-1K, 220 ohms preferred), a TMP36 temperature sensor, and breadboard wires.
You can follow along virtually using Tinkercad Circuits. You can even view this lesson from within Tinkercad (free login required)! Explore the sample circuit and build your own right next to it. Tinkercad Circuits is a free browser-based program that lets you build and simulate circuits. It's perfect for learning, teaching, and prototyping.
Step 1: Build the LED Circuit
Just as you’ve learned from the introductory lessons, start by wiring up your Arduino and breadboard with power and ground next to the example circuit, then add the the three red LEDs to the breadboard, as shown. These will be the indicator or "bar graph" lights for the project.
Drag an Arduino Uno and breadboard from the components panel to the workplane, next to the existing circuit.
Connect the 5 volt and ground pins on the Arduino to the power (+) and ground (-) rails on the breadboard with wires. You can change the wire colors if you want to! Either use the inspector dropdown or the number keys on your keyboard.
Drag three LEDs on the breadboard in row E, spaced 2 breadboard sockets apart. You can change the LED color using the inspector that pops up when you click on each one.
Use a 220 Ohm resistor to connect each LED's cathode (left leg) to the ground rail (black) of the breadboard. In Tinkercad Circutis, you can change a resistor's value by highlighting it and using the dropdown menu in the inspector.
Connect the LED anodes (right, longer legs) to digital pins 4, 3, and 2 on the Arduino. The LED anode (+) is the terminal that current flows into.
The cathode (-) is the terminal that current flows from. This connects to the ground rail.
Step 2: Add Temperature Sensor
A temperature sensor creates a changing voltage signal depending on the temperature it senses. It has three pins: one that connects to ground, another that connects to 5 volts, and a third that outputs a variable voltage to your Arduino, similar to the analog signal from a potentiometer.
There are several different models of temperature sensor. This model, the TMP36, is convenient because its output voltage is directly proportional to temperature in degrees Celsius.
In the circuits editor, find the temperature sensor in the components drawer.
Place the temperature sensor (TMP36) on the breadboard with the rounded part facing away from the Arduino, as shown in the figure (this is the default orientation).
Place the temperature sensor on the breadboard in row E, as shown.
Wire up the temperature sensor so the left pin connects to the 5V voltage rail, the center pin connects to A0 on the Arduino, and the right pin connects to the GND rail.
Step 3: Analog Input Observation
In the circuit schematic, you can see that the temperature sensor is connected to power (5 volts) and ground (0 volts) and the analog pin A0. As temperature rises, the pin connected to A0 increases its voltage. You can also see that three LEDs are each connected to their own digital pin.
Even though the Arduino is a digital tool, it’s possible for it to get information from analog sensors to measure things like temperature or light. To do this, you’ll take advantage of the Arduino’s built-in Analog-to-Digital Converter (ADC).
Analog-in pins A0 to A5 can interpret voltages between 0 and 5V, and translate that voltage to a value between 0 and 1023 for the Arduino sketch to use. The analog pins are primarily used to read information from sensors (but can also be used as digital outputs 14-19, unrelatedly).
Click "Start Simulation."
Open the Code Editor and find the "Serial Monitor" button to watch the sensor values pour in.
Step 4: Blocks Code
Let's use the code blocks editor to listen to the state of the sensor, then make decisions about which LEDs to light up based on the sensor's value.
Click the "Code" button to open the code editor. The grey Notation blocks are comments for making note of what you intend for your code to do, but this text isn't required or executed as part of the program.
Click on the Variables category in the code editor. Create a new variable called baselineTemp and use a "set" block to set it to 40 (degrees C).
To store the sensor value, create a variable named "celsius".
Drag out a "set" block and adjust the dropdown to our new variable celsius.
In the Math category, drag out a "map" block, and nest two arithmetic blocks ("1 + 1") within its first field.
Adjust the range from -40 to 125.
Click on the Input category and drag out an "analog read pin" block, and place it into the first arithmetic field inside the "map" block.
Adjust the arithmetic blocks to "(read analog pin A0 - 20) x 3.04".
Optionally create a new variable for converting the temperature to Fahrenheit with a set block and some arithmetic blocks to read "set fahrenheit to (celsius x 9)/5 + 32".
Add some serial monitoring blocks to print out the temperature in one or both C or F.
Click the Control category and drag out an if then block, then navigate to Math and drag a comparator block onto the if block.
In the Variables category, grab the celsius variable and the baselineTemp variable and drag them into the comparator block, adjusting the dropdown so it reads "if celsius < baselineTemp then".
Add three digital output blocks inside the if statement to set pins 2, 3, and 4 LOW.
Duplicate this if statement four times and add arithmetic blocks and and/or blocks to create five total state detection if statements. The first state is "the temperature is below our target baseline" so no LEDs light up. When the temperature is greater than or equal to the baselineTemp and less than baselineTemp+10, light up only pin 2's LED. When the temperature is between baselineTemp+10 and baselineTemp+20, light up two LEDs. And so on to account for all the desired states.
Step 5: Arduino Code Explained
When the code editor is open, you can click the dropdown menu on the left and select "Blocks + Text" to reveal the Arduino code generated by the code blocks. Follow along as we explore the code in more detail.
int baselineTemp = 0; int celsius = 0; int fahrenheit = 0;
Before the setup()
, we create variables to store the target baseline temperature, as well as the sensor value. They're called int
because they are integers, or any whole number.
void setup() { pinMode(A0, INPUT); Serial.begin(9600); pinMode(2, OUTPUT); pinMode(3, OUTPUT); pinMode(4, OUTPUT); }
Inside the setup, pins are configured using the pinMode()
function. Pin A0 is configured as an input, so we can "listen" to the electrical state of the temperature sensor. Pins 2, 3, and 4 are configured as outputs to control the LEDs.
void loop() { // set threshold temperature to activate LEDs baselineTemp = 40; // measure temperature in Celsius celsius = map(((analogRead(A0) - 20) * 3.04), 0, 1023, -40, 125);
Anything after a set of slashes //
is a comment, just for us humans to read, and is not included in the program when the Arduino runs it. In the main loop, baselineTemp is set to its target 40 degrees C.
// convert to Fahrenheit fahrenheit = ((celsius * 9) / 5 + 32); Serial.print(celsius); Serial.print(" C, "); Serial.print(fahrenheit); Serial.println(" F");
The formula for converting between celsius and Fahrenheit is F = (C * 9) / 5 + 32. Printing to the serial monitor helps you observe the temperature change more granularly than the LED states show alone.
if (celsius < baselineTemp) { digitalWrite(2, LOW); digitalWrite(3, LOW); digitalWrite(4, LOW); } if (celsius >= baselineTemp && celsius < baselineTemp + 10) { digitalWrite(2, HIGH); digitalWrite(3, LOW); digitalWrite(4, LOW); } if (celsius >= baselineTemp + 10 && celsius < baselineTemp + 20) { digitalWrite(2, HIGH); digitalWrite(3, HIGH); digitalWrite(4, LOW); } if (celsius >= baselineTemp + 20 && celsius < baselineTemp + 30) { digitalWrite(2, HIGH); digitalWrite(3, HIGH); digitalWrite(4, HIGH); } if (celsius >= baselineTemp + 30) { digitalWrite(2, HIGH); digitalWrite(3, HIGH); digitalWrite(4, HIGH); } delay(1000); // Wait for 1000 millisecond(s) }
The loop's six if statements evaluate for different segments of a certain temperature range between 40 and 46 degrees C, lighting up more LEDs the warmer the temperature.
If you want to see a more obvious change in bar graph lights, you can change the baseline temperature variable and/or the range that you are looking at by changing the arguments in the if() statements. This is called calibration.
Step 6: Use It!
If you built a physical version of this circuit, you can try it out with the Arduino software's serial monitor (magnifying glass button in the upper right of the sketch window), activating the sensor with your fingers. The project might not do what you want it to if the room temperature is really cold or really warm, or if your fingers are cold!
If using a physical board, observe the room temperature using the serial monitor, and set baselineTemp to that value.
Adjust your different temperature threshold "buckets" to a smaller range (2, 4, 6, instead of 10, 20, 30).
Upload your code again, and try holding the sensor in your fingers. As the temperature rises, you should see the LEDs turn on one by one.
Step 7: Next, Try...
You have used analogRead() and the serial monitor to track changes inside your Arduino and create a simple temperature display with LEDs. You will use this technique with other types of sensors in future projects! One way to expand this project is to create a way for two people to compare finger temperature. Would you need two sensors, or could they take turns? How would you build code to function as well as communicate the way to use it?
You can also learn more electronics skills with the free Instructables classes on Arduino, Basic Electronics, LEDs & Lighting, 3D Printing, and more.
16 Comments
Question 1 year ago on Step 5
So I am using the code given in this link and the map function is giving correct value in degree celcius if I comment out if else statements, but with if else statement.. map function is giving constant value of -40. Can you please help me understand it If you know the reason?
1 year ago
hi how to link this TMP36 to thingspeak and what is the code ? please....
1 year ago
i made same circuit and and same code but lights aren`t glowing where should i change the values to glow the light
Question 2 years ago
I have a problem with the code. What does the (if) do? When I'm trying to verify it I get this message:
expected unqualified-id before 'if'.
What can i do to fix this?
Question 2 years ago on Introduction
When using Tinkercad as a prototype modeller. How do you set the TMP36 to a particular temperature to emulate the real temperature that would be registered by a real circuit?
4 years ago
i understand how the code works and all i was just wondering why in the map function you times the analogRead(a0-20) with 3.04. could youb please explain as i am missing someting there
Best Answer 4 years ago
It's a formula for converting the ADC reading from the sensor into the actual temperature value. See https://learn.adafruit.com/tmp36-temperature-sensor/using-a-temp-sensor for an alternative way of calculating the temperature from the ADC reading. The datasheet explains more: https://www.analog.com/media/en/technical-documentation/data-sheets/TMP35_36_37.pdf
Reply 2 years ago
Obviously it's a formula for converting the sensor value into a temperature value. The question is why this arithmetic? Before I came to these comments, I scoured through the data sheet and the adafruit article, which are the first hits to pop up in a google search about using a TMP36, but no where do I find why this formula would be used. This is the most frustrating part for me about learning to use Arduino. Almost every example sketch I see makes 99% total sense, then there will be this one line of code that seems like it's some kind of magic handed down from the gods and I would never be able to come up with it on my own if I needed to.
I think I understand that mapping is taking a value and sort of moving it from one scale to another, so the part of the mapping line that is (...,0,1023,-40,125) means that you're taking a value from a 0 to 1023 scale and translating it to a -40 to 125 scale. But I don't understand why we're processing the input from the ADC by subtracting 20 and multiplying the sum by 3.04. Where are those numbers coming from and why wouldn't the 0 to 1023 value from the ADC be what gets mapped to -40 to 125?
It makes sense to me that the input from the ADC needs to be converted to a voltage. If the total range of voltage is 5V and there are 1024 units from the ADC, then we can find the voltage by taking the reading from A0 and dividing by 1024, then multiplying that quotient by 5. According to the data sheet, the scale factor is 10 mV per degree C, so thats 0.01 V per degree C, so that's 100 degrees C per volt. The voltage at 0 degrees C is 0.5 V, so it makes sense to subtract 0.5 V from the actual voltage so we can orient ourselves on the temperature scale. So something like
celsius=((analogRead(A0)/1024)*5-.5)*100;
would seem to give you a temperature value in celsius.
Reply 2 years ago
The way it's done in code blocks for Tinkercad Circuits is a bit of a workaround for the fact that there is no float-type variable support at this time, so all the decimal math has to happen to integers in the arithmetic. Your math is sound and will work for a real Arduino or for the text coding option in Tinkercad Circuits, just not blocks code at this time.
Reply 4 years ago
thanks you it all makes sense now
Question 4 years ago
Hello all, can anyone tell me why, I followed the exact instructions on tinkercad, then when that didn't work, I went and looked at the tinkercad model that they provided, (higher up) but my circuit just isn't working.....?
I have just started learning arduino, (I have done bekathwia's Arduino course, Thanks Bekathwia, it was so helpful :D and "A Beginner's Guide to Arduino" by tttapa.) I intend to make a simple weather station that will show temp and humidity and perhaps the soil moisture in my garden. Could anybody tell me why this project isn't working and if they know some other arduino courses/instructables that would be helpful?
Thanks...
Answer 4 years ago
Hi, could you attach a photo of your circuit? I can try to help you look for errors. What does your serial monitor output look like when the program is running?
Reply 4 years ago
Hello,
So I put two screen videos of both the circuit running and the blocks code ('cos I have too low resolution to take 1 screenshot), and i have uploaded the text code, I was wondering if it mighta' been something to do with the 2nd command (in the blocks editor) where it says:
"set celsius to (map ((read analog pin A0) -20 * 3.04) to range -40 to 125"
(brackets are showing the starts and ends of blocks), because in my version, the text is the same but the blocks are in a kind-of different order...(as you can see in the video).
I don't reckon that that could be the problem, because it looks like the text code is the same as the example...
thanks....
Reply 4 years ago
ok so i feel pretty stupid now, 'cos i just figured out that yes that was the problem and yes i wasn't looking quite carefully enough at the text code 'cos yes the different order of the blocks do actually mean different order of brackets..... :D
I'm pretty stupid, sorry for wasting your time :/
Reply 4 years ago
Don't be too hard on yourself, learning to code means accepting that there will always be typos. Glad you got it sorted!
Question 4 years ago
how to make calculator?