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: Sketch of circuit

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

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++){

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?


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: enter image description here
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.

  • You may want to use a more stable reference. You're checking the 5V of the Arduino by taking it's 5V output to the analog-reference input? You could also just use the default in that case. You could use the internal "bandgap reference" which should always be 1.1V, even when the input voltage varies (which might be the case, I don't think it's a steady 5V?). – Paul Mar 30 '16 at 8:01
  • @Paul doesn't the thermometer and the potentiometer I have connected need to output between 0v and 1.1v if I use internal? – Blargmode Mar 30 '16 at 19:54
  • I believe not, you can check it againsts the 1.1V read this article by our user Majenko for more information (hackingmajenkoblog.wordpress.com/2016/02/01/…) – Paul Mar 30 '16 at 19:58

According to your schematic, one of the LM335Z's leads is floating. The pins are meant to be Vcc, Gnd, and output; the output should go to an Analog input.

You wrote that Aref was tied to +5 but you've drawn it as going to one end of the pot with the other end of the pot grounded. The pot's wiper goes to A1 (assuming the pin hasn't been set to some other function).

In short, check your circuit. Try wiring up the sensor to +5, Gnd, and your DVM. Then replace the DVM with A0. At that point you'll have confirmed the sensor circuit, leaving only the code and the external analog circuitry. While I only skimmed the code I don't see anything that would give flaky results. I think you're seeing analog noise, probably due to one or more of the floating sensor pin, an inconsistent or noisy Aref, and pickup in the analog input from the sensor.

Update: I based my connections description on this diagram

enter image description here

, from this datasheet. In all of those diagrams, the Resistor-Zener connection is shown as the output and no pins are shown floating. But I did see that pin labeled as adj, for what reason I don't know. That's really why I recommended getting the sensor working by itself first.

I see your +5V to Aref connection now. The usual convention for indicating a connection at such a crossing is to put a dot on the intersection, just as the output pin's connection is shown in the above diagram.

  • All the tutorials have shown that you only use two pins, and the datasheet says that it has adj + -. And adj is for offsetting if the value if off. The pot is connected to the same 5V line as Aref, I don't now exactly how you show that lines are connected but I didn't draw a little loop in that intersection. – Blargmode Mar 28 '16 at 18:25
  • Thanks for expanding on your reply. I tried that without success. However, I found old code from when I first fiddled with the sensor, and that worked to some extent. I've updated the main post with details. – Blargmode Mar 29 '16 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.