LM335 Sensor question

Hi all,

This is one of my first little projects and i'm having a bit of an issue. I've got the LM335 hooked as follows:

A0 on pin 2
5v -> 2k2 resister to pin 2
GND on pin 3
pin 1 not used

Serial results work as expected and I'm seeing the correct temperature display over and over in the loop which this morning was 12-13 degrees give or take. The issue I have is after adding the IF statements to turn on some LEDs depending on the temperature the first reading was correct at 12-13 degrees, the second then hit 19 degrees and the third was 27 degrees! and all subsequent readings are around 27.. I think the issue has something to do with my IF statements but I have no idea what i've stuffed up!!

Any help would be appreciated :slight_smile:

CODE below:
float kelvin; // setup float for LM335 sensor reading
float Celsius; // setup float for Celsius calculation

void setup()
{
Serial.begin(9600);
Serial.println("Starting up.....");
pinMode(12, OUTPUT); //green led
pinMode(13, OUTPUT); //red led
pinMode(11, OUTPUT); //yellow led
pinMode(0, INPUT); // analog pin 0 for LM335 reading
}
void loop ()
{
kelvin = analogRead(0); // read LM335 sensor
Celsius = ((((kelvin / 1023) * 5) * 100) - 279.15); // calculate Celsius from kelvin reading
Serial.print("Currently it is ");
Serial.print(Celsius);
Serial.println(" degrees Celsius");
if (Celsius < 15) // if temp is less then 15 degrees turn on RED led
{
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
}
else if ((Celsius > 15) && (Celsius < 20)) // if temp is greater then 15 and less then 20 degrees degrees turn on YELLOW led
{
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
}
else // if temp is over 20 degrees turn on GREEN led
{
digitalWrite(11, LOW);
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
}
delay (3000);
}

How close is your sensor to the LEDs?

Also, what happens in your code if Celsius = 15?

The sensor is actually right near the LEDs (didn't think of that!).. i'll move it and also adjust the "> 15" to ">=15" :slight_smile: and see how it goes..

Moved the sensor and adjusted the 15 degree issue and got these results, which shows the same spike as before :frowning:

Starting up.....
Currently it is 14.59 degrees Celsius
Currently it is 20.46 degrees Celsius
Currently it is 20.95 degrees Celsius
Currently it is 20.95 degrees Celsius
Currently it is 21.44 degrees Celsius
Currently it is 20.95 degrees Celsius
Currently it is 21.44 degrees Celsius
Currently it is 21.44 degrees Celsius

Removed IF statements gives the correct temp within range:

Starting up.....
Currently it is 14.59 degrees Celsius
Currently it is 14.59 degrees Celsius
Currently it is 14.11 degrees Celsius
Currently it is 14.59 degrees Celsius
Currently it is 14.59 degrees Celsius
Currently it is 14.11 degrees Celsius
Currently it is 14.11 degrees Celsius
Currently it is 14.11 degrees Celsius
Currently it is 15.08 degrees Celsius
Currently it is 14.59 degrees Celsius
Currently it is 15.08 degrees Celsius
Currently it is 14.59 degrees Celsius
Currently it is 15.08 degrees Celsius
Currently it is 15.08 degrees Celsius
Currently it is 15.08 degrees Celsius
Currently it is 15.08 degrees Celsius
Currently it is 15.08 degrees Celsius

Silly things first....this is not correct:

pinMode(0, INPUT); // analog pin 0 for LM335 reading

"0" is not the same as A0, or analog pin 0 (also digital pin D14 on an Uno/Duemilanove). So this should be"

pinMode(A0, INPUT); // analog pin 0 for LM335 reading

Second silly thing....are you sure you have pin 1 and pin 3 right on the LM335? The National datasheet shows these from a bottom view while many other devices are shown from a top view of the part.

--
The Ruggeduino: compatible with Arduino UNO, 24V operation, all I/O's fused and protected

Hi RuggedCircuits,

Thanks for pointing out the Analog pin i've adjusted that. As for the pin on the LM335 i've double checked the spec and it's in the correct spot.

Is this repeatable, or was it a one-time thing? In other words, do you get the spike every time you run with LED's and DON'T get the spike every time you DON'T run with LED's?

--
The Gadget Shield: accelerometer, RGB LED, IR transmit/receive, speaker, microphone, light sensor, potentiometer, pushbuttons

Yes every time it's run with LEDs you get the spike and without LEDs no spike... could be the current going to the LM335 is reduced once the LED switches on??

If the current is reduced the voltage (hence temperature) would do down :astonished:

Two things, probably both incorrect, come to mind:

  • Self-heating: the sensor is getting warmer just by current through it. Does not at all explain why this doesn't happen when LED's aren't on.
  • Good old noise: try putting a small capacitor (1uF or less) across pin A0 and GND.

--
Beat707: MIDI drum machine / sequencer / groove-box for Arduino

Can you post of photo or drawing of how you have this wired?

So this should be"

Code:
pinMode(A0, INPUT); // analog pin 0 for LM335 reading

...it really shouldn't be there at all.