Home Arduino Platform Arduino - Input / Output Basics Arduino Example #4 - Analogue In - Temperature Sensor
Print E-mail
User Rating: / 44
Arduino - Arduino - Input / Output Basics
Written by Dave Auld   
Saturday, 10 October 2009 11:55

Arduino Basic Input Output Circuits

Analogue IN - Reading a analogue input value from a Temperature Sensor

This example shows you read an analogue input from a temperature sensing device and switch an output.

This example also integrates the Digital Out LED example.

This example introduces the Serial communications for monitoring back at the PC, as well as using helper functions to structure the code.


Again my specific hardware is the Arduino Duemilanove platform, but the concept is the same regardless of the exact platform.


Before continuing make sure you have read and understand the warning notice here.


Hardware Requirements;

1x Arduino

1x LED

1x Temperature Sensor (TMP36)

1x Resistor (560Ohm)


In this example, the output (an LED) will be switched on and off in response to a given analogue input from a temperature sensing device.

The temperature sensing device in this example is a TMP36.


The TMP36 will give a fixed voltage rise per temperature sensing division, 10mV per 'C. It has a range of -40'C to +125'C and the voltage is calculated using the formula:

voltage = (temperature in 'C * 10mV) + 500mV


The Arduino has 10bit resolution on the analogue input, so will provide a value in the range 0-1023 relative to the 0 to 5V input capability of the analogue in. With this information it is easy to then construct functions to be used in the code.


-40 'C 100mV
-30 'C 200 mV
-20 'C 300 mV
-10 'C 400 mV
0 'C 500 mV
10 'C 600 mV
20 'C 700 mV
30 'C 800 mV
40 'C 900 mV
50 'C 1000 mV
60 'C 1100 mV
70 'C 1200 mV
80 'C 1300 mV
90 'C 1400 mV
100 'C 1500 mV
110 'C 1600 mV
120 'C 1700 mV
125 'C 1750 mV


The analogue input pin will be analogue In 3

The digital output pin will be digital pin 7


This circuit diagram for this example is shown below;




The following code is used to control the LED in response to the varying temperature seen by the TMP36. The code also include a deadband reset function similiar to that introduced in Example 3b. Load it into the development environment and upload it to the Arduino. Once uploaded and running, open a serial monitor window in the development environment to read back the serial communications.

Basic Analogue Input Control - Temperature Sensor
This example reads an analogue value from analogue in pin 1 and then compares this to a set point.
If the set point is threshold is crossed, the LED on digital pin 7 is turned on/off. A deadband is
provided to prevent high speed toggling at the set point transition.
The logic used below with act on Rising temperature.
The analogue input will come from a TMP36 temperature sensor. 
-40'C to 125'C = 100mV to 1750mv (linear 10mV per 'C)
Author: David M. Auld
Date: 10th October 2009
int tempIn = 3;       // temperature sensor on Analogue Pin 3
int ledOut = 7;        // LED on Digital Pin 7
int aiValue = 0;       // input value (0-1023 = 0 to 5v)
float setPoint = 15;    // Trigger value in 'C
float deadband = 0.5;   // Differential for reset value in 'C
  NOTE: the deadband action can be disabled by setting the deadband to 0
float degC;            // The temperature in Degrees Centigrade
void setup()
  pinMode(ledOut, OUTPUT);    // Configure the Digital Pin Direction for the LED  
void loop() 
  degC = getTemperature(getVolts());      //Get the Volts and the Temperature using Helper functions.
  if (degC > setPoint)
    digitalWrite(ledOut, HIGH);      // Temperature Limit Reached, turn the LED on.
    Serial.println(" | Output: ON.");
      if (degC < (setPoint - deadband))
      digitalWrite(ledOut, LOW);       // Temperature dropped below SP and deadband, turn the LED off.
      Serial.println(" | Output: OFF.");
Return the voltage for the input value; 0-1023 = 0 to 5V
float getVolts()
  int inputValue;
  inputValue = analogRead(tempIn);
  float volts;
  volts = (((float)inputValue / 1024) * 5);
  Serial.print("Input Value: ") ; Serial.print(inputValue);
  Serial.print(" | Voltage: ") ; Serial.print(volts);
  return volts;
Return the temperature in DegreesC for given voltage
float getTemperature(float volts)
  float temp = (volts - 0.5) / 0.01 ;
  Serial.print(" | Temperature: "); Serial.print(temp); Serial.print(" 'C");
  return temp;

Last Updated on Saturday, 06 February 2010 15:38


0 # 2010-12-06 23:25

Thanks Dave - this is almost exactly the example I've been hunting for. Appreciate the time you spent to write it up!
Reply | Reply with quote | Quote
0 # 2013-07-25 00:28
hi dave, can you help me to make a code that have two setpoint between 22c-26c, for ex. if the temp reach the below 22c the heater will on, and if they back to the temp we set it off the heater, and if the reach 26c the cooling equipment will on and if they back to the temp we set it off the cooling equipment. i make a code for this but its not working hope you can help me this project, many thanks pyro
Reply | Reply with quote | Quote
0 # Dave Auld 2013-07-25 05:35
This is fairly easy to do.

If you look at the code above you will see a variable setPoint. What you need to do is define another setPointOff.

In main body you will see to if blocks that check against the set point value, the first one is turning the output on, the second one is turning the output off. Rename the second check to the look at the new variable setPointOff and job done.
Reply | Reply with quote | Quote

Add comment

The site owners has the right to edit/delete or use the comment in any way for which they choose.
The comments may be moderated before going public.

Security code

Copyright © 2015 Dave-Auld.net. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.
Find us on Facebook
We have 31 guests online