Pages: [1]   Go Down
Author Topic: Temperature sensor and serial port  (Read 202 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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 ?
Logged

Dubuque, Iowa, USA
Offline Offline
Edison Member
*
Karma: 33
Posts: 2280
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
//Legge il valore dalla porta analogica
      val = analogRead(A2);
      val = analogRead(A2);
Logged

Pages: [1]   Go Up
Jump to: