Sunday, 3 January 2010

Arduino Diecimila Temperature Monitoring

In this entry I will be showing you how to use your Arduino Diecimila to monitor temperature using an LM35 temperature sensor. The Arduino will measure the voltage input from the LM35, convert from the voltage measurement to Celsius and output the result to the serial port.

Here is what we'll use:
  • Arduino Diecimila with usb cable (or a Freeduino, which is 100% Arduino Diecimila compatible).
  • LM35 temperature sensor (TO-92 package - see picture and/or datasheet below)
  • Solderless breadboard
  • Several jump wires
  • 100K Ohm resistor
All components other than the Arduino should be easily purchased at your local hobby electronics store (such as Maplin in the UK).
This assumes the user is able to run the Arduino environment and run a program on the arduino board. For further information please see Getting Started with Arduino.

LM35 Temperature Sensor
The LM35 temperature sensor is a 'Precision Centigrade Temperature Sensor with an accuracy of +/- 1°C, whose output voltage is linear proportional to the Celsius temperature'. With the circuit we will be using, we will have an accurate measurement range of +2˚C to +150˚C, adequate for measuring room temperatures. Although with a slightly more advanced circuit, it can have a range of −55˚ to +150˚C (but the Arduino doesn't support the negative voltage reading). Refer to the LM35 datasheet here for more info.
The LM35 we will be using is a TO-92 plastic package, perfect for our breadboard.
It has three pins:
  • +Vs - Reference voltage that can have a range of (4V to 20V), we will be connecting this to the 5V rail on the Arduino.
  • Vout -This is the voltage output that will be measured by out Arduino's analog to digital converter. It will be connected to Analog pin 5 of the Arduino.
  • GND - Ground for the LM35, this will be connected to our Arduino's GND pin.
As stated previously, the output voltage is linear proportional to the Celsius temperature. As the temperature changes, the voltage will change at a fixed rate of 1 millivolt per 1°C.

In order to calculate the Celsius reading from the analog value, we read from Analog pin 5 of the Arduino and use the following formula to calculate the temperature in Celsius:
  • DegC is the calculate temperature value.
  • adcVal is result of the Arduino's analog to digital conversion on Analog Pin 5.
  • 5.0 is the reference voltage we are using.
  • 1024 is the resolution of the Arduino Analog Pin 5 adc converter.

Here is a schematic and picture of the circuit:
The circuit consists of the LM35 and resistor on the breadboard, with the jump wires used to connect the breadboard and the Arduino.

Source Code
The following is the source code for reading the temperature. Each time the user sends the 's' (for sample) character using the serial monitor, the Arduino will output the current temperature. 

// select the input pin for the LM35
int potPin = 5;

// select the pin for the LED 
int ledPin = 13;

// variable to store the value coming from the sensor 
float temperature = 0;    

void setup()

  // declare the ledPin as an OUTPUT
  pinMode(ledPin, OUTPUT);

  digitalWrite(ledPin, HIGH);

void loop()
  if (Serial.available())
    char ch =;
    if (ch == 's')
      // read the value from the sensor
      int val = analogRead(potPin);   
      temperature = (5.0 * val * 100.0)/1024.0;


The source file can be download from here.

Possible Issues 
Temperature accuracy - The accuracy of the temperature value outputted by the Arduino is dependent on the reference voltage for the LM35 temperature circuit being exactly 5 volts.
I was using an un-powered USB hub to connect to several USB devices, including my Arduino Diecimila. I noticed that the read temperature seemed quite high for the room I was in. First thing I did was double-check the formula in software, which was correct. I then checked the 5volt rail on the Diecimila and found it to be reading 4.2 volts, rather than the expected 5 volts. I plugged the Diecimila's usb cable from the USB hub, to directly into my iMac which fixed the issue (I checked the 5v rail on the Diecimila, which now read 5.05 volts). The current spec for USB ports is about 400mAmps, so it looks like I was over-driving the USB port that was powering the usb hub.
This is also something to consider if you are powering your Diecimila from battery. When the batter starts to weaken, the temperature accuracy will reduce.