Go Down

Topic: Temperature sensor and serial port (Read 301 times) previous topic - next topic

teamteck01

I'm working with arduino due and TMP36 on  A2 pin. The program reads the temperature when external python script sends 'A' character on serial pins.
This is my simple loop code:

Code: [Select]

void loop()
{

 //analogRead(A3);

 if (Serial.available()){  
 
   char c = Serial.read();  
   
   if (c == 'A'){
   
     //Legge il valore dalla porta analogica
     val = analogRead(A2);
     
     // converte il valore letto in tensione
     float voltage = (val * 3.289)/4096;
   
     //converte la tensione in temperatura
     float celsius = (voltage - 0.5) * 100 ;
   
     // Trasmette sulla seriale il valore in gradi
     Serial.println(celsius,2);  
     
   }  
   
 }    
   
}


The problem is that the values read are wrong ( 50,00... -49,99... 8.45...ecc-).
If I uncomment analogRead(A3), then values are correct (22,99... 23.01... ecc.)
Also the first analogRead() command that seems to fix the problem is not related to  the sensor pin (A2, not A3)

I do not understand ... some help ?

Chagrin

This is a typical problem with analogRead(); readings can carry over between pins.

http://arduino.cc/en/Tutorial/AnalogInputPins
The Atmega datasheet also cautions against switching analog pins in close temporal proximity to making A/D readings (analogRead) on other analog pins. This can cause electrical noise and introduce jitter in the analog system. It may be desirable, after manipulating analog pins (in digital mode), to add a short delay before using analogRead() to read other analog pins.

The usual solution is to just execute the analogRead() twice.

Code: [Select]
//Legge il valore dalla porta analogica
      val = analogRead(A2);
      val = analogRead(A2);


Go Up