Having recently acquired a smiley micros kit (Arduino Duemilanov ATMega 328) I'm playing around with the examples. I have a LM35 temperature sensor on analogue A0 and a light sensor on A1. I'm using analogRead in the loop (with 500mS delay) to read the values from A0 and A1 and serial.print to write them back to my computer (baud rate 19200 connected through USB cable).
The light sensor on A1 seems to return a rock solid value but the temperature sensor on A0 seems to fluctuate between a reading of 37 and 41 and yet my volt meter on the LM35 output is rock steady at 205mV.
Interestingly, if I stop calling analogRead(A1) for the light sensor the fluctuations on A0 temp sensor are far less (39/40).
Nothing to do with the fluctuations but I noticed the following: void output_temperature(int pin) contains two ints whole and fraction. As an int (16 bit) can hold only +- 16384 as value. The reading * 49 causes an overflow. Define whole as a long (32 bit) and the math would go right.
You could also define a float for temperature, try this:
robtillaart, thanks but the sensor is sat on my desk so I'm not that worried about integer overflow as the temperature would have to be so hot I would not be here. The LM35 outputs 10mV for 1 degree. 25 degrees is 250mv = 51 ADC units. 51 * 49 = 2891. I' need 330 odd ADC units to overflow and that would be 330 * 4.9mV = 1617 mv = 161 degrees centigrade.
Your point is taken though and I was rather blindly followed the arduino workshop book. I knew about floats but hadn't noticed the arduino supported them.
A second look at your code doesn't show serious error in the SW, so time to scrutinize the HW. Can you make a drawing or photo of how you connected the LM35 and lightsensor to the Arduino?
If the input is too high impedance the input sample and hold capacitor won't have time to charge. Try taking tow readings of the same analogue input in succession and only use the second one. If that helps but does not completely cure the problem try putting a small delay between the first and second reading of the same pin.
I've disconnected everything from the arduino except the LM35 and disconnected everything on my breadboard (see picture) from the +5 and gnd.
At ambient temp in my room I get values from 29 to 34 jumping around all the time (representing 14.21 degrees C to 16.66 degrees C) and also this seems a little low, I'd expect it was around 18/19 degrees C and this represents around 180mV (18 degrees C).
Interestingly if I put my fingers over the LM35 it rises to an adc value of 57 and is consistent then when I remove fingers it steadily drops (no jumping around by more than 1) back to 38 or so then the jumping around starts again.
Problems you may encounter with multiple sensors...
If, when adding more sensors, you find that the temperature is inconsistant, this indicates that the sensors are interfering with each other when switching the analog reading circuit from one pin to the other. You can fix this by doing two delayed readings and tossing out the first one
The link:
A user in the customer forums had a problem with multiplexing analog signals, they seemed to be very noisy and inconsistent. Turns out it was because the sensors are high impedance. This means that if there is capacitance on the ADC line or on any sort of sample&hold capacitor, it may take a while for it to charge up. Read these threads for some great analysis! View here and here?