Go Down

Topic: temperature reading not stable: please help! (Read 2 times) previous topic - next topic

robtillaart


You need to "intify" the formulas for the millivolts so they do no premature rounding.

Code: [Select]

  long millivolts = (5000 * value0) / 1024;  // turning the math around prevents premature rounding
  int celsius = millivolts / 10;  // sensor output is 10mV per degree Celsius

  // or in one step
  int celsius = (500 * value0) / 1024;


rH should be a piece of cake now..
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

RIDDICK

i read here before that it might b a good idea to throw away the first reading after an ADMUX change...
the datasheet doesnt say it, but maybe it helps?

what if u do it like this:
Code: [Select]
void loop()
{
  analogRead(inPin0);
  int value0 = analogRead(inPin0);
  float millivolts = (value0 / 1024.0) * 5000;
[...]
-Arne

tuxduino

In a project I did a couple years ago on a PIC18 the compiler online help said a small delay (around 20us IIRC) was required between adc channel selection and adc read; the reason was the adc mux needed some time to stabilize after selection.

dhenry

Quote
maybe it helps?


They definitely help, when the code is incorrectly written.

dc42


i read here before that it might b a good idea to throw away the first reading after an ADMUX change...
the datasheet doesnt say it, but maybe it helps?


The analogRead() code in the Arduino core sets the multiplexer and then starts the ADC conversion in the very next instruction. I think this is plain stupid for a general purpose function. It just about works if the source resistance to the analog inputs is no more than a few kohms, but otherwise it doesn't give the sample capacitor time to charge. Reading the pin twice and throwing away the first reading helps, but doubles the analog read time and still doesn't work very well with high source resistances. I generally either use my own code for reading analog inputs, or patch the Arduino core to insert a delay between setting the multiplexer and starting  the conversion. A value of 1us per 10K of source resistance works well.

Bear in mind that the input resistance of the ADC is quoted as 100Mohms typical, so if you have more than about 100K of source resistance, accuracy will be degraded.
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Go Up