Pages: [1]   Go Down
Author Topic: temperature reading not stable: please help!  (Read 1632 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hy,
i'm not an expert and i ask your help to understand where i'm wrong.
that's the code i wrote, reading a humidity sensor and a temp sensor (lm35).

const int inPin0 = 0; // analog pin
const int inPin1 = 1; // analog pin
void setup()
{
  Serial.begin(9600);
}
void loop()
{
  int value0 = analogRead(inPin0);
  float millivolts = (value0 / 1024.0) * 5000;
  float celsius = millivolts / 10;  // sensor output is 10mV per degree Celsius
  Serial.print(celsius);
  Serial.print(" degrees Celsius, ");
  int value1 = analogRead(inPin1);
  float millivolts1 = (value1 / 1024.0) * 2310; // conversion to %hr
  float rh = millivolts1 / 30;  // sensor output is 30mV per °RH%
  Serial.print(rh); Serial.println("rh");
  delay(1000); // wait for one second
}

While the humidity signal is stable, the temp signal jump without logic. Just the first value is correct, then the others go to from 10 to 40°C.
Many thanks in advance for everybody can help me.
Regards.
Andrea.
Logged

0
Offline Offline
Shannon Member
****
Karma: 162
Posts: 10496
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Firstly learn to use the
Code:
// code tags for inline code snippets - the # button when composing.

Need to see the circuit - in particular exactly how the power and LM35 are wired up - a photo is good.  Often such issues are down to not understanding grounding.
Logged

[ I won't respond to messages, use the forum please ]

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 27
Posts: 1186
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I got the same thing until I stopped powering the system from the USB port and provided a stable external power supply that was rock solid on 5V. The sensor is very sensitive to voltage variations as you are calculating assuming you have exactly 5 volts powering the sensor. if you have 4.5 V you can be wildly out in the temperature reading.

The humidity sensor is just based on a proprtion of full scale and is therefore much more tolerant of supply voltage.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Many many thanks for your answers. For markT i'll post a picture this evening and thanksin adavance when you'll have a look! For Marco_c i'm running with usb power. This evening I'll connect the board with an external switching power supply.
But my doubt is that there's something wrong in the sketch. I'm starting now to program and probably i did a mistake. Please guys try again to get a look at it and tell me if it's correct.
thanks again for your help.
Andrea.
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Meanwhile have a look at blink without delay, so you can have your sketch do other things in that second you're wasting now (just a suggestion for future development) smiley
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 27
Posts: 1186
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code look ok to me. For future postings, use the #button to post your code as it is more readable in a separate scrollable window.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 27
Posts: 1186
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's something else to try. This code uses the 1.1V reference in the Arduino to calibrate what the actual value of the nominal 5V supply is. If you want to use the USB power supply, call readVcc and use the value returned instaed of the 5000 as the divisor in the temperature calculation.

Code:
int readVcc()
// Calculate current Vcc in mV from the 1.1V reference voltage
{
  long result;

  // Read 1.1V reference against AVcc
  ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  delay(2); // Wait for Vref to settle
  ADCSRA |= _BV(ADSC); // Convert
  while (bit_is_set(ADCSRA,ADSC));
  result = ADCL;
  result |= ADCH<<8;
  result = 1126400L / result; // Back-calculate AVcc in mV
  
  return(result);
}

void setup() {
  Serial.begin(57600);
}

void loop() {
  Serial.println( readVcc(), DEC );
  delay(1000);
}
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  float millivolts = (value0 / 1024.0) * 5000;
  float celsius = millivolts / 10;  // sensor output is 10mV per degree Celsius
  ...
  float millivolts1 = (value1 / 1024.0) * 2310; // conversion to %hr
  float rh = millivolts1 / 30;  // sensor output is 30mV per °RH%

If I were you, I would think seriously if I need floating point math here.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hy,
many many thanks evreybody gave a suggestion to solve the problem. I,ve tried to take out the float but no way, i tried to take out the delay but no way. For Marco_c thanks for your code, i tried but again nothing. As i wrote if temp runs alone is perfect, with the rh sensor start to jump. Strange is that the first reading is perfectly correct, then from the second data wrong numbers. Some time the correct value appear. I definitely think (and hope) an external power is needed.
Thanks again.
Andrea
Logged

Leeds, England
Offline Offline
Sr. Member
****
Karma: 8
Posts: 447
Quick, chuck it in the bin before the boss finds out...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How long are the leads between the sensor and the Arduino? And is the signal lead screened?
Logged

Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12483
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Code:
  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..
Logged

Rob Tillaart

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

Brunsbüttel, SH, F.Rep.GERM
Offline Offline
God Member
*****
Karma: 4
Posts: 596
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
void loop()
{
  analogRead(inPin0);
  int value0 = analogRead(inPin0);
  float millivolts = (value0 / 1024.0) * 5000;
[...]
Logged

-Arne

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
maybe it helps?

They definitely help, when the code is incorrectly written.
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 220
Posts: 6587
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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.

Pages: [1]   Go Up
Jump to: