I'm using an LM335Z temperature sensor with an Arduino Mega.
I have the 5V rail going through a 2K resistor and have managed to calibrated it.
At the moment the Arduino is connected to my PC with USB.
I find the reading is quite stable UNTIL I connect my I2C 7-Segment display, and then the readings start to vary jumping from 23 degrees to 31!
I understand that the sensor needs a solid analogue reference voltage to give accurate readings, so plugging the Arduino into a 9V power supply instead of the USB to computer will probably be better.
Why does the display cause these fluctuations? Is it a voltage issue or interference? What can I do to address this issue? Can I use a capacitor is some way to stabilise the voltage???
Arduino uses the potentially unstable 5volt rail as reference for the A/D.
If you're happy with a temp range of ~0-100C, you could use the internal 1.1volt Aref.
Here is an example sketch I wrote.
Change (INTERNAL) to (INTERNAL1V1) for your Mega.
Leo..
// LM35 temp sensor connected to A1
// ~2 to ~102 degrees C
//
float Aref = 1.063; // calibration, change this to the actual Aref voltage of ---YOUR--- Arduino
unsigned long total; // A/D output
float tempC; // Celcius
float tempF; // Fahrenheit
//
void setup() {
analogReference(INTERNAL); // use the internal ~1.1volt reference, change (INTERNAL) to (INTERNAL1V1) for a Mega
Serial.begin(115200); // ---set serial monitor to this value---
}
//
void loop() {
analogRead(A1); // one unused reading to clear old sh#t
for (int x = 0; x < 100; x++) { // 100 readings for averaging
total = total + analogRead(A1); // add each value
}
// convert value to temp
tempC = total * Aref / 1024; // value to temp conversion
tempF = tempC * 1.8 + 32; // Celcius to Fahrenheit conversion
total = 0; // reset value
// print to serial monitor
Serial.print("The temperature is ");
Serial.print(tempC, 1); // one decimal place
Serial.print(" Celcius ");
Serial.print(tempF, 0); // no decimal places
Serial.println(" Fahrenheit");
delay(1000); // slows readings
}
void DisplayTemp()
{
unsigned long total;
float tempC;
analogRead(TEMPERATURE);
for (int x = 0; x < 100; x++)
{
total = total + analogRead(TEMPERATURE);
}
tempC = total * Aref / 1024;
Serial.print("The temperature is ");
Serial.print(tempC, 1); // one decimal place
Serial.println(" Celcius");
}
The raw value from analogRead(TEMPERATURE); is 1023.00!
No it's not the wiring. I changed back to my original code without Aref and the values are ok, but they fluctuate a lot (see above) from about 21 to 33 degrees
I get reasonably good values ie. not varying too much when I have just the temp sensor and Chronodot connected, but they start varying a lot once I add the 7-segment display.
This is very frustrating, I think I might have to try a different temp sensor, maybe the Dallas version.
This is essentially my original wiring which is correct according to the LM335 datasheet and the examples I found on the Net.
I can get reasonably god results UNTIL I connect my 7-Seg LED display. Then I start to get fluctuations in the readings +- 10 degrees!
I thought the display might be causing interference or voltage fluctuations, so maybe I need to add a capacitor to smooth out the voltage, but I'm not sure how to do this properly.
But that still doesn't explain why I'm getting the 1023 readings when using Aref.
I am really at a loss as to why this is happening.
Yes, I never mentioned any TMPxxx, my post is titled "LM335Z" which is what's written on the sensor.
Okay, then I won't use Aref.
At this stage I don't know what to do about the varying readings, except to try connecting the Mega with a power supply as opposed to via the USB from the PC and see if that makes any difference.
Perhaps I should try another sensor such as the Dallas (aka. Maxim) DS18B20, but that means I will have to use the 1-Wire protocol/library.
The LM335Z doesn't like me, and I've lost patience with it. I will get a TMP36 and DS18B20 and give them a try. At least the TMP36 will work with Aref which should iron out the fluctuations somewhat.
The LM335Z is indeed not easy to use with an Arduino.
Because you have to use default Aref, and that is potentially unstable.
TMP36 will work with 1.1volt Aref only to ~50 degrees C.
But with a Mega, and 2V56 Aref, the full scale can be used.
The code for a TMP36 is slightly different because of the 500mV offset.
Here is some (untested) code to get you started.
Leo..
// TMP36 temp sensor connected to Analogue-in A1, +5volt and ground
// ~ -50 to +55 degrees C
//
unsigned int total;
float tempC;
float tempF;
//
void setup() {
analogReference(INTERNAL2V56); // ---uncomment this line for a MEGA---
Serial.begin(115200); // set serial monitor to this value
}
//
void loop() {
analogRead(A1); // one unused reading
for (int x = 0; x < 20; x++) { // 20 readings for averaging
total = total + analogRead(A1); // add each value to a total
}
Serial.print("Raw average = "); // debugging
Serial.print(total * 0.05, 1); // 1/20 of 20 readings, one decimal place
// leave ONE of these three lines uncommented
//tempC = total * 3.3 * 5 / 1024 - 50; // ---this line is for 3.3volt Arduinos---
//tempC = total * 5.0 * 5 / 1024 - 50; // ---this line is for 5volt Arduinos---
tempC = total * 2.56 * 5 / 1024 - 50; // ---this line is for a MEGA with 2.56volt Aref enabled---
// calibration: change reference voltages to actual voltage
tempF = tempC * 1.8 + 32; // Fahrenheit conversion
Serial.print(" The temperature is ");
Serial.print(tempC, 1); // one decimal place
Serial.print(" Celcius ");
Serial.print(tempF, 0); // no decimal places
Serial.println(" Fahrenheit");
delay(1000); // slows readings
total = 0; // reset value
}