I'm trying to get consistent values from the LM335Z temperature sensor. But it behaves very strange.
I'm using an arduino nano and it's powered via USB from a Raspberry Pi. The circuit also has two little reed relays (SIP-1A05), a button and a potentiometer.
It's connected like this:
Additionally I'm using analogReference(EXTERNAL)
in an attempt to get consistent values, so I've connected the Arduinos 5V output to the reference voltage pin.
Here's the code for reading the thermometer:
int getTemperature() {
int rawvoltage = analogRead(0);
float millivolts = (rawvoltage / 1024.0) * 5000;
float kelvin = (millivolts / 10);
float celsius = kelvin - 273.15;
return round(celsius);
}
Then it just waits for me to say "get" in the serial monitor and it replies with:
void sendData(){
String data = (String)getTemperature();
data += ',';
data += (String)map(getTablePos(), 0, 1023, 0, 100); //Potentiometer
data += ',';
data += (String)getTableActive(); //1 or 0
Serial.println(data);
}
I also read somewhere that the first couple of readings are unreliable, and that you should read a couple of times in startup, which I do:
for(int i = 0; i < 10; i++){
analogRead(0);
delay(1);
}
Yesterday the termometer reported 37 degrees. Today it started by reporting 49 degrees. After asking for the temperature a couple of times it said 56. Then now, about an hour later it says 52. And if one of they relays is active it says 77 degrees. My two other (non Arduino) thermometers have ben on a steady 25 all this time.
What should I do to get reliable values from the LM335z?
Edit
Added dots to the intersections of the circuit diagram for better readability.
I've also found a way to get the thermometer to work, kind of. I removed the external analog reference and only have one 1kOhm resistor for the LM335Z.
Then I have this code instead:
const float cal = 4.62; //Voltage between 5V and GND, measured with multimeter
float getTemperature() {
float input = (float) analogRead(0);
input = (input*cal)/1024.0;
return (input/0.01)-273.15;
}
It worked perfectly standalone on an Arduino Uno powered via USB3.0. When I moved it over into my project with the Nano it gave a few degrees off, but that can be combated by just subtracting the difference I guess.
Then I tried switching to a USB2.0-port on my keyboard, and the values started jumping all over the place.
Here's a comparison using Arduinos rather spartan plotter:
Left: USB2.0 via keyboard hub. Right: Front panel USB3.0.
Also, the temperature dubbels when a relay is on.
How do I tackle this? I'd like keep powering it from my Raspberry pi 3, if possible.