Temperature sensor TMP36
Today I connected the temperature sensor Analog Devices TMP36 to the MSP430 Launchpad.
I chose his sensor for my project because it is widely used, cheap and very easy to use
Hardware considerations
TMP36 has only three connections:
- Vcc
- GND
- Output
The TMP36 sensor can measure temperatures between -40 °C and +125 °C and provides an linear output voltage between 0.1 V and 1.7 V. At 0 °C, the sensor outputs 0.5 V. The output changes of 10 mV for each degree of temperature. So the maximum output voltage is
This means that output of the sensor can be connected directly to the input of the MSP430's ADCs. Just to prevent any possible impedance mismatch between the TMP36 output and the ADC input, I added a 741 operational amplifier in buffer configuration
The supply voltage of the sensor comes from the LTC3108 VOUT2. VOUT2 can be configured to output several voltage ranges by properly connecting input pins VS1 and VS2. In this case, LTC3108 is configured to output 5V.
To save energy, all the logic driven by the 5V supply voltage is switched on request. The MSP430 drives the LTC3108's VOUT2_EN pin to switch on and off the 5V output.
The temperature sensor (as well all other sensors on the board) are switched on when required by a ULN2003LV integrated circuit
In the next picture, the 5V supply voltage path is highlighted in red color. The 5V is output by the LTC3108 and is connected to the COM+ pin of the ULN2003LV. When a sensor needs to be switched on, the MSP430 drives high the corresponding output pin (see yellow path). When a ULN2003LV's input pin is high, the corresponding relay closes and this let current flow from 5V to ground thus powering up the sensor. ULN2003LV minimum input voltage is 1.8V, so no extra circuitry is required
It's very easy to connect the sensor to the MSP430 demo board. I placed on a breadboard the TMP36 sensor and the 0.1 µF capacitor. This capacitor must be placed between Vcc and ground and is required in order to improve the precision of readings
Software
To read output from TMP36, I first configured the MSP430's ADC by means of Grace
I'm going to perform a single acquisition of the ADC value, so the clock source and the sample&hold time is not important. I will use a resolution of 10 bits, because this resolution gives me steps of
which is sufficient for this type of application
To read the ADC value, I use the Texas Instruments driverlib library, which provides a convenient way of accessing the MSP430 peripheral. This library basically wraps the access to peripheral registers in human-readable C functions.
For example, to start an ADC acquisition, instead of
ADC10CTL0 |= ADC10ENC + ADC10SC; // Sampling and conversion start
I will write
ADC12_B_startConversion(ADC12_B_BASE,
ADC12_B_MEMORY_0,
ADC12_B_SINGLECHANNEL);
which, in my opinion, is much more clear
To save energy, I will not loop waiting for the conversion being completed, but I will instead put the MCU in sleep
__bis_SR_register(LPM0_bits + GIE); // LPM0, ADC12_ISR will force exit
and will read the ADC value in the ADC12_ISR function
extern void SENSORS_SetADCReading(uint16_t memory, uint16_t value);
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector=ADC12_VECTOR
__interrupt
#elif defined(__GNUC__)
__attribute__((interrupt(ADC12_VECTOR)))
#endif
void ADC12_ISR(void)
{
uint16_t value;
switch(__even_in_range(ADC12IV,12))
{
case 0: break; // Vector 0: No interrupt
case 2: break; // Vector 2: ADC12BMEMx Overflow
case 4: break; // Vector 4: Conversion time overflow
case 6: break; // Vector 6: ADC12BHI
case 8: break; // Vector 8: ADC12BLO
case 10: break; // Vector 10: ADC12BIN
case 12: // Vector 12: ADC12BMEM0 Interrupt
value = ADC12_B_getResults(ADC12_B_BASE, ADC12_B_MEMORY_0);
SENSORS_SENSORS_SetADCReading(ADC12_B_MEMORY_0, value);
__bic_SR_register_on_exit(LPM0_bits); // Exit active CPU
break; // Clear CPUOFF bit from 0(SR)
...
}
}
The SENSORS_SetADCReading function simply set a global variable
uint16_t SENSORS_Adc;
void SENSORS_SetADCReading(uint16_t memory, uint16_t value)
{
SENSORS_Adc = value;
}
This said, the function that reads the ADC value can be written as follow
uint16_t SENSORS_AnalogRead(uint16_t memory)
{
// Base address of ADC12B Module. Start the conversion into memory buffer 0
// Use the single-channel, single-conversion mode
ADC12_B_startConversion(ADC12_B_BASE,
memory,
ADC12_B_SINGLECHANNEL);
__bis_SR_register(LPM0_bits + GIE); // LPM0, ADC12_B_ISR will force exit
return SENSORS_Adc;
}
The sensor output voltage has linear relationship with the temperature
The formula to convert ADC reading into degrees Celsius is
where
- "°C" is the temperature value in degrees Celsius
- "ADC reading" is value returned by the AD converter
- "ADC precision" is the ADC reference voltage (2.0 V) divided by the maximum value returned by the AD converter (1024). This resulting value is 2,0 V / 1024 = 0.00195
- "output @ 0 °C" is the output voltage when the sensor is at 0°C
- "voltage step per °C" is the variation in the voltage output when temperature changes of 1 degree °C. According to TMP36 datasheet, this value is 10 mV / °C
The final function for reading temperature value can be written as
#define SENSORS_ADC_V_MAX 2.0f
#define SENSORS_ADC_STEPS_MAX 1024
#define SENSORS_TMP36_V_0DEG 0.5f
#define SENSORS_TMP36_V_PER_DEG 0.010f
void SENSORS_ReadTemp()
{
float val = SENSORS_AnalogRead(ADC12_B_MEMORY_0);
val = (val * SENSORS_ADC_VMAX) / SENSORS_ADC_STEPS_MAX;
val = (val - SENSORS_TMP36_V_0DEG) / SENSORS_TMP36_V_PER_DEG;
SENSORS_Data.temperature = val;
}