March 11, 2011

Build a Thermometer Using the LM335 Sensor and Arduino

Today we are building a digital thermometer. Parts we need are: 
  • Arduino Uno
  • 1 x 2ΚΩ resistor
  • LM335 temperature sensor
Now let's start with the basics. What is the sensor? You can find the full data-sheet here, but with a few words it's a diode. The voltage on the edge of the diode is proportional to the temperature. For every Kelvin degree increase in temperature the voltage increases by 10 mV. As I said in my previous post the read accuracy of the input pin is ~5mV, so the temperature we read will be half a degree accurate.

Now let's see the circuit we need to build.

 
It's pretty simple, you have:
5V->2KΩ resistor->middle pin of the LM335-> right pin of LM335->GND
                    AND->wire to input A0 (pin 0)

So build this circuit on your breadboard, and now it's time to code! :D

/*
 * temperature_sensor.pde
 * -----------------
 * Takes the sensor (LM335) input and converts it
 * to Kelvin degrees, then celsius.
 * Subtract 2.5 celsius degrees that seem
 * to be the sensor error perhaps due to the heat
 * generating by the current running through it.
 * 2.5 degrees were calculated by using a mercury
 * thermometer next to the sensor.
 *
 * http://spacetinkerer.blogspot.com
 */

float temp_in_celsius = 0, temp_in_kelvin=0, temp_in_fahrenheit=0;

void setup()
{
  Serial.begin(9600);
}

void loop()
{

  //Reads the input and converts it to Kelvin degrees
  temp_in_kelvin = analogRead(0) * 0.004882812 * 100;
 
  //Converts Kelvin to Celsius minus 2.5 degrees error
  temp_in_celsius = temp_in_kelvin - 2.5 - 273.15;
 
  temp_in_fahrenheit = ((temp_in_kelvin - 2.5) * 9 / 5) - 459.67;

  //Print the temperature in Celsius to the serial port
  Serial.print("Celsius: ");
  Serial.println(temp_in_celsius);                 

  //Print the temperature in Fahrenheit to the serial port
  Serial.print("Fahrenheit: ");
  Serial.println(temp_in_fahrenheit);
  Serial.println();


  delay(1000);
}

The code is also simple. This command 

temp_in_kelvin = analogRead(0) * 0.004882812 * 100;

takes the input (0-1023) and converts it to Kelvin degrees. As I said 1 Kelvin degree temperature raise, raises the voltage by 10 mV or 0.01V so it's 100 degrees in every volt, if I may express it like that. So the input is converted in volts (multiplied by 0.004882812V, which is 5V / 1024) and then to Kelvin (multiplied again by 100).

Then we take the Kelvin temperature and convert it to Celsius and Fahrenheit. You can read more about Kelvin degrees and the conversion on wikipedia.

Also you may wonder why I subtract 2.5 from the degrees when I convert them. I noticed that the sensor measures 2.5 degrees more, it must be an error of some sort, possibly due to the current that's warming it the sensor. I measured the temperature difference using another mercurial thermometer and that to be the difference.

The rest of the code should be familiar to you, if not you can read my older posts.

Now you can watch a video of the program running:


You can download the code.

Check out my latest article on how to make your own breadboard jumper wires.

If you like my posts then subscribe via rss, or via e-mail to stay updated.

Happy Tinkering!

If you liked this article then take a second to bookmark it with...


6 comments:

  1. Hi

    How did you make the image which shows the circuit. When ever I want to make a circuit the results are abysmal.

    ReplyDelete
  2. Hello Samir,

    I use this program called fritzing (http://fritzing.org/). It is used for exactly this purpose, i.e. making circuits. It's open source and free and there is Windows, Linux and Mac Version. It's quite easy to learn.

    Kostis

    ReplyDelete
  3. great stuff! I live in Australia. What exactly is Arduino and where do you guys buy it from ?

    Also, did you download the LM335 part externally? I can't seem to find it in Fitzing

    ReplyDelete
  4. Wonderful. Got me up and running with my arduino-fridge-replacement-thermostat in minutes.

    ReplyDelete
  5. You can avoid the 2.5 degrees subtraction by connecting a 10K pot to the middle pin to calibrate the sensor. Set it to 2.982V at 25C as explained in the datasheet.

    Hope this helps.

    ReplyDelete
  6. Thanks for the rundown!

    I'm wondering how you chose the resistor value. The datasheet just labels it as R1, but doesn't (AFAICT) give it a value.

    I'm guessing the value seems to be calibrated for the 5v supply - if I try to run the LM335AZ off my 3.3v supply line, I get totally whacky results. I'm guessing this is because we're basically making a voltage divider here, but I'm a little unclear on how to calibrate it.

    thanks again!

    Spencer

    ReplyDelete