Two similar programs read different temperatures??

17 Jun 2011

Hi all, I noticed that the sensor MCP9700A reads a different value of temperatures with the following two programs.

The first program is:

#include "mbed.h"

Serial pc(USBTX, USBRX);
DigitalOut myled(LED1);
AnalogIn temp(p15);


void tempRead(){
    float Ta = 0.0;

    float t = temp.read();           //Read the input voltage
    Ta = ((t * 3.3) - 0.5)/ 0.01;      
    
    pc.printf("The temperature is [%4.4f]: %4.2f deg C\n\r", t, Ta);
 }

int main() {
    while(1) {
    
        myled = 1;
  
        tempRead();         //read temp with DEC
    
        wait(3);
        myled = 0;
        wait(5);
    }
}

The output of this is:

The temperature is [0.2335]: 27.04 deg C
The temperature is [0.2332]: 26.96 deg C
The temperature is [0.2322]: 26.64 deg C
The temperature is [0.2322]: 26.64 deg C
The temperature is [0.2322]: 26.64 deg C



The second program is:

#include "mbed.h"
#include "MSCFileSystem.h"


DigitalOut myled(LED1);
AnalogIn temp(p15);
InterruptIn inb(p21);
MSCFileSystem fs ("fs");
Serial pc(USBTX, USBRX);
Timer tim;


#define DEBOUNCING_INTERVAL  100     //  Debouncing interval (in mili-seconds) 
int var;
int i = 1;

float tempRead(){

    float Ta = 0.0;
    float t = temp.read();           //Read the input voltage
    Ta = ((t * 3.3) - 0.5)/ 0.01;      
    
    return Ta;                      // return the temp
}



void USBWrite(){
     myled = 1;
     if ( tim.read_ms() > DEBOUNCING_INTERVAL ) {       
           var = 1; 
     }
    
     tim.reset();  //  timer reset
     wait(1);
     if(i == 6){
        pc.printf("Error!!\n\r");
     }

}



int main() {
   

   tim.start();             //  timer start
   inb.rise(&USBWrite);     // Call function USBWrite() on rising edge
   
   FILE *fp = fopen("/fs/temp.txt","w");    
   if ( fp == NULL ){
           error("Could not open file for write\n");    // se l'USB non è collegata
   }

   
   while(1){
   
       
      if(var == 1){             
          wait(1); 
          float temp = tempRead();  
          fprintf(fp, "Temp %d: %4.2f deg C\n", i, temp);    //write the value of temp in "temp.txt"
          pc.printf("The temperature is: %4.2f deg C\n\r", temp);
          i++;
          var = 0;
          wait(1);
          myled = 0;
     }
   
      if(i == 6){               
   
         fclose (fp);
         pc.printf("Scrittura Conclusa\n\r");
         myled = 0;
         break;
   }
   
   }
   
   
}

The output is:

The temperature is: 28.41 deg C
The temperature is: 28.41 deg C
The temperature is: 28.41 deg C
The temperature is: 28.33 deg C
The temperature is: 28.49 deg C



The two part for the read of the temperature are similar but there is a difference of 1.5°C.
Someone can help me for resolve this problem??

PS: the room temperature read by another thermometer is 26.1°C.

Thanks

17 Jun 2011

Perhaps the added load on the power supply in the second program (the USB flash drive) is enough to shift the ADC result by the 15mV or so that you are seeing.

The ADC converts inputs ratiometrically with respect to the power supply and ground. The temp sensor provides an absolute voltage output with respect to ground, however. So if the ADC power supply voltage changes even slightly, the temp sensor output will convert to a different number (even though the temperature has not changed).

You could try an experiment: Connect a 1.5V flashlight battery with its negative end to ground and its positive end to another analog input pin (say, p16). This provides an absolute voltage reference. Read this voltage at p16 with your first program, then again with your second program.

If they differ by 15mV or so, you have confirmed the problem. If the readings are the same, you have ruled out this hypothesis...

17 Jun 2011

Thanks Hexley, I tried your test with 1.5V battery and the result confirm your hypothesis.
With the battery instead of the sensor both programs give the same result.