Temperature Sensor (Analog Input)
by circuits in Circuits > Arduino
9624 Views, 6 Favorites, 0 Comments
Temperature Sensor (Analog Input)
![temperature-sensor-led-bar-graph.jpg](/proxy/?url=https://content.instructables.com/FI2/BY4X/JD0K9E4T/FI2BY4XJD0K9E4T.jpg&filename=temperature-sensor-led-bar-graph.jpg)
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 temperature sensor, using the built in Analog-to-Digital (ADC) converter, accessed through the analog pins, which you may have learned about in a previous lesson about analog input.
- Explore the sample circuit here in the workplane 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.
- Can you identify all these components in the breadboard circuit pictured?
- Continue to the next step.
Build the LED Circuit
![led circuit.png](/proxy/?url=https://content.instructables.com/FWR/TKV1/JL6UZUZV/FWRTKV1JL6UZUZV.png&filename=led circuit.png)
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. You can change a resistor's value by highlighting it and using the dropdown menu.
- Connect the LED anodes (right legs) to digital pins 4, 3, and 2 on the Arduino. Stuck?
HINT:
The LED anode (+) is the terminal that current flows into. This will connect to the digital output pins on the Arduino. The cathode (-) is the terminal that current flows from. This will connect to the ground rail.
- Continue to the next step.
Add Temperature Sensor
![Screen Shot 2018-01-30 at 10.37.04 AM.png](/proxy/?url=https://content.instructables.com/FTL/GZHA/JD0K8L08/FTLGZHAJD0K8L08.png&filename=Screen Shot 2018-01-30 at 10.37.04 AM.png)
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.
- Continue to the next step.
Analog Input Observation
![FLCVCB9ISCBW3JJ.LARGE.jpg](/proxy/?url=https://content.instructables.com/FXZ/O7BD/JD0K5I0V/FXZO7BDJD0K5I0V.jpg&filename=FLCVCB9ISCBW3JJ.LARGE.jpg)
![Screen Shot 2018-01-29 at 3.01.56 PM.png](/proxy/?url=https://content.instructables.com/F4W/1GCW/JD0K5JRQ/F4W1GCWJD0K5JRQ.png&filename=Screen Shot 2018-01-29 at 3.01.56 PM.png)
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.
- Continue to the next step.
Blocks Code
![Screen Shot 2018-08-24 at 11.15.14 AM.png](/proxy/?url=https://content.instructables.com/FGA/WIJB/JL6URYRC/FGAWIJBJL6URYRC.png&filename=Screen Shot 2018-08-24 at 11.15.14 AM.png)
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.
- Continue to the next step.
Arduino Code Explained
![F2MVK12ISCBW5PR.LARGE.jpg](/proxy/?url=https://content.instructables.com/FR6/ACEJ/JD0K5HE8/FR6ACEJJD0K5HE8.jpg&filename=F2MVK12ISCBW5PR.LARGE.jpg)
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 calledint
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.
- Continue to the next step.
Use It!
![FJFKXUQISCBW5Q6.LARGE.jpg](/proxy/?url=https://content.instructables.com/FX9/HLMN/JD0K8RHU/FX9HLMNJD0K8RHU.jpg&filename=FJFKXUQISCBW5Q6.LARGE.jpg)
![Screen Shot 2018-01-30 at 3.16.55 PM.png](/proxy/?url=https://content.instructables.com/FSO/F5J5/JD0K9CXN/FSOF5J5JD0K9CXN.png&filename=Screen Shot 2018-01-30 at 3.16.55 PM.png)
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.
- Continue to the next step.
Next, Try...
![led-bar-graph-optimized-00.jpg](/proxy/?url=https://content.instructables.com/FJI/0XWH/JIKJZJIB/FJI0XWHJIKJZJIB.jpg&filename=led-bar-graph-optimized-00.jpg)
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! Here are a few ways to expand this project:
- 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?