### Author Topic: Temperature Sensor Arduino Program with LM335  (Read 9204 times)

0 Members and 1 Guest are viewing this topic.

#### galannthegreat

• Supreme Robot
• Posts: 615
• Blue-Lensed Blue LEDs?! What?! Impossible!!
##### Temperature Sensor Arduino Program with LM335
« on: September 22, 2010, 09:21:41 PM »
I am currently working on a data logging project right now with an Arduino and I have some code working, but I want to make it a bit more accurate. I am wondering if people know of any ways I can do that.

Here is my code:
Code: [Select]
`#define SENSOR 0 // select the input pin for                 // the LM335A temperature sensorfloat val = 0; // variable used to store the value               // coming from the sensorfloat val2 = 0;float celcius = 0;void setup(){  Serial.begin(9600); // open the serial port to send                      // data back to the computer at                      // 9600 baud}void loop(){  val = analogRead(SENSOR); // read value from the sensor  val2 = val/2;  celcius = val2 - 274;    Serial.println(celcius); // print the value to                           // the serial port                         delay(1000); // wait 1 second between each               // each reading}`
Thanks, Kurt.
Kurt

#### rbtying

• Supreme Robot
• Posts: 452
##### Re: Temperature Sensor Arduino Program with LM335
« Reply #1 on: September 22, 2010, 10:35:01 PM »
What sensor are you using?  Also, what do you want to make a bit more accurate?  Please clarify

Sorry, I didn't read the title.

The LM335 has an output of 1mv/K

As such, you need to start by converting the reading back into volts:

Code: [Select]
`val *= 0.0048828125;`
Then, divide the voltage by 100 to get 10mV resolution.  At this point, you should cast to a shorter variable to avoid floating-point operations

Code: [Select]
`int deg = val / 100;`
Lastly, subtract 273.15 degrees to get degrees Celcius from Kelvin:

Code: [Select]
`deg -= 273;`
In summary:

Code: [Select]
` deg = (int)(analogRead(SENSOR)*0.0048828125/100)-273;`
Make sense?
« Last Edit: September 22, 2010, 10:49:08 PM by rbtying »

#### galannthegreat

• Supreme Robot
• Posts: 615
• Blue-Lensed Blue LEDs?! What?! Impossible!!
##### Re: Temperature Sensor Arduino Program with LM335
« Reply #2 on: September 23, 2010, 12:12:24 AM »
Thank you very much for that , it helped me figure out what I wanted to do.

Here is the new code: (I didn't follow exactly what you did but I did the same sort of thing)
Code: [Select]
`#define SENSOR 0 // select the input pin for                 // the LM335A temperature sensorfloat val = 0; // variable used to store the value               // coming from the sensorfloat val2 = 0;float deg = 0;float celcius = 0;void setup(){  Serial.begin(9600); // open the serial port to send                      // data back to the computer at                      // 9600 baud}void loop(){  val = analogRead(SENSOR); // read value from the sensor  val2 = val * 0.00489; // take SENSOR value and multiply it by 4.89mV                        // and store value to val2  deg = val2 * 100; // multiply by 100 to get degrees in K  celcius = deg - 273.15; // subtract absolute zero to get degrees celcius      Serial.println(celcius); // print the value to                           // the serial port                         delay(1000); // wait 1 second between each               // each reading}`
P.S. It also turns out that I should precisely calibrate the sensor to get more accurate results, but it is doing what I want. YAY!
Kurt