Hello, I just started a project with LM35 (temperature sensors) with Arduino, after mounting it on my breadboard I noticed that the values in the serial monitor are inconsistent and vary in a strange way, for example I get 21°, 20°, 22° 24° successively which is impossible I get the bad results with 5 different sensors, even whern i use a heat source the temperature the values vary also in a strange way and I don't know where the problem is can someone help me please and also when approching a heat source the temperature decreases instead of increasing !
Welcome to the forum
Please post your full sketch, using code tags when you do
Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination
In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.
It is also helpful to post error messages in code tags as it makes it easier to scroll through them and copy them for examination
ok sure !
Also look at the raw numbers. If the readings are going in the right direction (up when the temperature increases).
If only the calculated temperature goes in the wrong direction you've got a software problem. The raw counts will have more variation because they have higher precision.
I get the bad results with 5 different sensors,
Did you buy them from a reputable supplier, or AliExpress or eBay?
you means arduino ide ? i'm working on ubuntu , i buy them from a local seller
Given that you are using a breadboard that is, in all likelihood, of poor manufacture, those results are to be expected. Use a double female HDR and plug the sensor on one side and the Dupont wires to the board on the other. This is the next best solution to soldering and is probably 99% as good.
What has that to do with the issue?
The OS will not affect the readings.
i have already tried wiring LM35 directly to arduino but i'm having same result
I was thinking you can get female hdrs that are double and joined but maybe not, so get a double row female and solder a jumper across each of the pins like shown but at the top so it can still sit in a breadboard for convenience.
How, soldered?
Give this program a try, connect LM35 signal to A0 pin.
/*
LM35, TMP36 thermometer, no floats, no delays
*/
const bool t36 = false; // set false for LM35, true for TMP36
const byte numSamples = 8, // number of samples for smoothing
aInPin = A0; // analog input pin
const int calValue = 0, // adjust for calibration, +/-0.1 degree
hAref = 110, // analog ref voltage * 100
// measured with accurate DMM
hnumSamples = numSamples * 100,
tEnd = 3000; // update time in mS
int val,
tempC,
tempF;
uint32_t total, // sum of samples
tStart; // timer start
byte cnt = 10;
const char header[] = "\nRaw Total Temp C Temp F";
void setup()
{
Serial.begin(9600);
analogReference(INTERNAL); // use 1.1V internal ref
analogRead(aInPin);
for(int i = 0;i < numSamples;i++) // for smoothing, fill total
total += analogRead(aInPin); // with numSamples * current
// reading
}
void loop()
{
if(millis() - tStart > tEnd)
{
tStart = millis(); // reset timer
if(++cnt >= 10)
{
Serial.println(header); // every 10 lines
cnt = 0;
}
val = analogRead(aInPin);
total -= (total / numSamples); // make room for new reading
total += val; // add new reading
tempC = total * hAref / hnumSamples + calValue;
if(t36)
tempC -= 500;
tempC = antiDither(tempC);
tempF = tempC * 9 / 5 + 320;
Serial.print(val);
Serial.print("\t");
Serial.print(total); // sum of samples
Serial.print("\t");
prntTemp(tempC);
prntTemp(tempF);
Serial.println();
}
}
void prntTemp(int temp)
{
Serial.print(temp / 10); // whole degrees
Serial.print(".");
Serial.print(temp % 10); // tenths
Serial.print("\t");
}
int antiDither(int newVal) // prevents +1 <-> -1 dither
{
static int val = 0, oldVal = 0;
if(newVal != val && newVal != oldVal)
{
oldVal = val;
val = newVal;
}
return val;
}
did you mean A0 or A7?
A0, I caught it 3ms after hitting reply, TNX.
My code did not produce that.
do you have any idea where the problem is ?
Bad connection somewhere, defective sensor, connected wrong are the only possibilities I can think of.
If reading is backwards as you say when approaching heat source then the sensor is wired wrong.
Just for giggles, experiment with the sensor connections.
You might also have a noisy power supply. But that shouldn't cause BIG problems... As long as the Arduino and LM35 share the same power supply, most of the noise & variations should cancel-out.
If you have a multimeter, measure the LM35 voltage output. If it's jumping around too much that may be clue (there is always SOME noise). Check the 5V too. Of course the meter has its own reference so any nose/variation won't get canceled-out But meters also have some filtering/smoothing so the voltage may look more stable than it is.
i buy them from a local seller
I wonder where he buys them...
You seem to have more problems, but here is something to consider.
If you have a high temperature and then a low temperature there is a design problem where the LM35 may not discharge the ADC sample capacitor sufficiently. This will result is high readings. From the data sheet section 7.1. See last line.
The temperature-sensing element is then buffered by an amplifier and provided to the VOUT pin. The amplifier has a simple class A output stage with typical 0.5-Ω output impedance as shown in the Functional Block Diagram. Therefore the LM35 can only source current and it's sinking capability is limited to 1 μA.