This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Measuring temperature using LM335, MSP430g2553

Other Parts Discussed in Thread: LM335

Hello,

I am using LM335 to measure the temperature of the hand

so I am getting quite high values > 700 degree Celsius 

I am not sure why that is happening, I have the sensor connected to a ground pin through the negative terminal, and I used a 1 Kohm connected to Vcc and then to the positive terminal which is also connected to A1

I am sure of my code but I will show it just in case

#include <msp430g2553.h>

#ifndef TIMER0_A1_VECTOR
#define TIMER0_A1_VECTOR    TIMERA1_VECTOR
#define TIMER0_A0_VECTOR    TIMERA0_VECTOR
#endif

void FaultRoutine(void);
void ConfigWDT(void);
void ConfigClocks(void);
void ConfigLED(void);
void ConfigADC10(void);
void ConfigTimerA2(void);

//volatile int lightRead=0;
volatile float TRead=0;

int main(void) {
	volatile unsigned int i;

	ConfigWDT();
	ConfigClocks();
	ConfigLED();
	ConfigADC10();
	ConfigTimerA2();
	
	_BIS_SR(GIE);			//Enable global interrupts
	while(1);
}

void ConfigWDT(void)
 {
 WDTCTL = WDTPW + WDTHOLD;                 // Stop watchdog timer
 }

void ConfigClocks(void)
 {
 if (CALBC1_1MHZ ==0xFF || CALDCO_1MHZ == 0xFF)
   FaultRoutine();		                    // If calibration data is erased
 				                            // run FaultRoutine()
  BCSCTL1 = CALBC1_1MHZ; 					// Set range
  DCOCTL = CALDCO_1MHZ;  					// Set DCO step + modulation
  BCSCTL3 |= LFXT1S_2;                      // LFXT1 = VLO
  IFG1 &= ~OFIFG;                           // Clear OSCFault flag
  BCSCTL2 |= SELM_0 + DIVM_3 + DIVS_3;      // MCLK = DCO/8, SMCLK = DCO/8
  BCSCTL1 |= DIVA_3;						// ACLK -> 1.5 KHz
 }

void FaultRoutine(void)
 {
   P1OUT = BIT0;                            // P1.0 on (red LED)
   while(1); 			                    // TRAP
 }

void ConfigLED(void){
  P1DIR = BIT0+BIT6;                             // P1.0 (red) output
  P1OUT = 0;                                // LED off
}

void ConfigADC10(void){
	ADC10CTL1 = INCH_1 + ADC10DIV_1;		//Choose input channel and choose clock frequency
	ADC10CTL0 |= SREF_0 + ADC10SHT_2 + REFON + ADC10ON + ADC10IE;    //Vcc to Vss as reference and turn on
	_delay_cycles(30);                         						// Wait for ADC Ref to settle
	ADC10AE0 = BIT1;    							//Enable ADC input through PIN 1.3

}

void ConfigTimerA2(void){
	CCTL0 = CCIE;							// Allow periodic interrupt
	TACTL = TASSEL_1 + MC_1+ ID_3;
	CCR0 = 187;							// Enable interrupts every 1 second


}

#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A_ISR (void){

	while (!(ADC10IFG & ADC10CTL0)){ 							 // Check if the conversion is completed
		ConfigADC10();
		ADC10CTL0 |= ENC + ADC10SC;								// Sampling and conversion start
	}
	TRead = ADC10MEM;
	TRead = TRead - 273.15;
	ADC10CTL0 &= ~ADC10IFG;


	ADC10CTL0 &= ~ENC;				   								// Disable ADC conversion
	ADC10CTL0 &= ~(REFON + ADC10ON);        						// Reference and ADC10 off

	CCR0 += 187;
}

  • Hello again,

    First, keep in mind that the supply voltage to the LM335 has to be 5 V.  I assume from your description that you've set it up as a basic temperature sensor, meaning that at room temperature the output voltage should be near 2.982 V at room temperature (298.15 degrees Kelvin or 25 degrees Celsium).  Before doing anything else, use a multimeter to make sure this is right.

    Now then, what is the supply voltage to your MCU?  This is very important, because as you have your references set up for Vr+ = Vcc and Vr- = Vss.  This means that a range of 0 to Vcc correlates to an ADC value of 0 to 1023.  So if we for example have Vcc at 3.3 V, an input of 2.982 V (temp sensor output at room temperature) will produce an ADC reading of 1023*(Vin/Vcc) = 1023(2.982/3.3) = ~924.  That's obviously not degrees Kelvin, so what do we do with it?  We can multiply it by our Vcc and by 100 (LM335 outputs at 10 mV/K) to get degrees Kelvin.  Then we can subtract 273.15 to finally get out answer in degrees Celcius.  A simple solution, and probably not the most effective, but it works so long as you have a constant and dependable Vcc.

    Have fun,

    Ryan

  • Good response, Ryan. Except one thing:

    After multiplying with VCC and 100, you need to divide by 1024.

    (924*3.3*100)=304920. /1024 = 297.7 -273.14 = 24.56°C

    For the /1024, one can do >>10. And one can subtract the offset before shifting:

    T=(ADC10MEM*3300L-304845L)>>10

    Due to the shift and the hardware multiplication, this calculation doesn’t take long.

  • Well dang I knew that while writing the post but somehow I forgot to add the extra divide/shift at the end!  Thanks for catching that.


    Ryan

  • Thank you guys for the helpful responses :)

**Attention** This is a public forum