Search This Blog

Friday 22 January 2016

Thermistor to Temperature conversion in ARM-mbed

This is a short article about how to quickly convert a Thermistor Value to Temperature on the Freescale (Now NXP) FRDM-K64F board.  Honestly, this code will work for any of the FRDM boards, as there is nothing specific to the K64F.

The FRDM-K64F developer board from NXP  (formerly Freescale) is a 32bit ARM® Cortex™-M4 core with DSP instructions and Floating Point Unit (FPU)running at 120 Mhz. It has 1M flash memory and 256k RAM.   It is compatible with most Arduino shields.

Add to this:
  •  FlexBus, 
  • 16 channel DMA, 
  • 2 16bit ADC, 
  • 2 16bit DAC, 
  • 3 analog comparators, 
  • CANbus, 
  • 3 SPI ports, 
  • 3 I2C ports, 
  • 6 UARTs, 
  • 1 Secure Digital Host Controller, 
  • Ethernet, 
  • Micro-SD card
  • Hardware CRC and random-number generator module, 
  • Hardware encryption supporting DES, 3DES, AES, MD5, SHA-1 and SHA-256 algorithms
  • 6-axis combo Sensor Accelerometer and Magnetometer

NXP/Freescale do have their own free IDE available, the Kinetis Design Studio that I've discussed here in the past. It is quite a rich and capable development platform, however with that richness comes complexity.  

For those less familiar with configuring hardware clocks and startup code for embedded processors, I suggest you look at the ARM-mbed platform.



ARM-mbed is a fairly good online IDE for working with various ARM based developer boards.







I was rummaging through my code today, and found this piece that I had struggled with last year when I was working on my 3D printer firmware.  I wanted to be able to read two thermistors, one for the extruder nozzle, and one for the heated bed.  These thermistors are used to regulate the temperature for accurate printing.

Several of the standard 3D printer firmware options (Marlin, Teacup, etc...)  that were developed for the 8bit Arduino platform used conversion tables to reduce the processing overhead. Because I was using a 32bit ARM processor at 120Mhz, I didn't feel that this was the right method to follow.  
Looking around, I quickly found out about the  Stenhart-Hart equation for converting Thermistor resistance values to Temperature values. 

It looked rather daunting at first, but then Adafruit came to the rescue with this awesome tutorial:


Anyway, with no further ado... here is my demo code for displaying Temperature values from a 16bit analog read of a 100k thermistor on the FRDM-K64F development board.


/*         FRDM-Thermistor-Demo
*  This is a Thermistor to Temerature conversion demo
*  for the @NXP (@freescale) FRDM-K64F demo board
*  Much thanks to @Adafruit for this tutorial:
*  https://learn.adafruit.com/thermistor/using-a-thermistor
*
*  The 100K Thermistor is configured with a 4.7k series resistor
*  tied to vcc (3.3v)  like this:
*
*   +3.3v
*     |
*      \
*      /  4.7k series resistor
*      \
*      /
*      |
*      .-----------O To Anlog pin on FRDM board
*      |
*      \
*      /
*  Thermistor  100k Nominal
*      \
*      /
*      |
*     ---
*   GND
*          
*
*********************************************************************/


#include "mbed.h"

#define THERMISTORNOMINAL 100000      // 100k
// temp. for nominal resistance (almost always 25 C)
#define TEMPERATURENOMINAL 25  
// The beta coefficient of the thermistor (usually 3000-4000)
#define BCOEFFICIENT 3950
// the value of the 'other' resistor
#define SERIESRESISTOR 4700   
 
AnalogIn Thermistor(A3);

Serial pc(USBTX, USBRX);


// This is the workhorse routine that calculates the temperature
// using the Steinhart-Hart equation for thermistors
// https://en.wikipedia.org/wiki/Steinhart%E2%80%93Hart_equation
float get_temperature()
{
    float temperature, resistance;
    float steinhart;
    int a;
   
    a = Thermistor.read_u16();       // Read 16bit Analog value
    pc.printf("Raw Analog Value for Thermistor = %d\r\n",a);
 
    /* Calculate the resistance of the thermistor from analog votage read. */
    resistance = (float) SERIESRESISTOR / ((65536.0 / a) - 1);
    pc.printf("Resistance for Thermistor = %f\r\n",resistance);
  
    steinhart = resistance / THERMISTORNOMINAL  // (R/Ro)
    steinhart = log(steinhart);                 // ln(R/Ro)
    steinhart /= BCOEFFICIENT;                  // 1/B * ln(R/Ro)
    steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15);   // + (1/To)
    steinhart = 1.0 / steinhart;                // Invert
    temperature = steinhart - 273.15;           // convert to C

    return temperature;
}
  

int main()
{
    pc.baud(115200);   
    pc.printf("\r\nThermistor Test - Build " __DATE__ " " __TIME__ "\r\n");
 
    while(1) { 
       pc.printf("Temperature %f *C\r\n",get_temperature());

       wait(.5);
    }
}


    

References:
NXP.COM: FRDM-K64F Developer board
ARM-mbed: FRDM-K64F 
Adafruit: Using a thermistor to tell temperature 

No comments:

Post a Comment