Saturday 1 October 2016

CAN Bus Project part 2 : Thermistors

In Part 1 I introduced my Arduino / Tiva CANBus project.

CAN data can be anything, but in real-world applications (cars, robots etc.) it's often sensor-data. To keep things simple, I'm sending temperature data.

Common temperature sensors are thermocouples, RTDs and thermistors. I'll talk more about the differences another time. Suffice to say, the good ol' thermistor is tried-and-tested and super-duper cheap.

O.K., so how to hook up a thermistor?

The concept is simple:

Thermistors have a resistance (thermistance?) that changes with temperature. Measure this resistance, plug it into the Beta Equation together with some numbers you get from the thermistor data sheet, and voila, out of the equation comes the temperature.


So how to measure thermistor resistance? A quick-and-dirty way is to use a voltage-divider and the Arduino's built-in ADC. The photo shows a voltage-divider I whacked together. 




I used a 10Kohm thermistor (part number 151-237 from RS components) and a 10Kohm bias resistor (actual measured value was 9.947K).

The red wires goes to 5V, green to 0V, and yellow to pin A5 on the Arduino.

My code to do the Beta calculation is (I've also stuck it on my gitHub page)




// Arduino_Thermistor_Reader // Voltage Bias - the Blog.
// Gilbert Waltoon, Oct 2016
//
// Released under a GNU General Public License v3.0
//
// PURPOSE: Reads the voltage of a thermistor voltage divider
// and converts it to temperature.
//
//
// Input pin for thermistor voltage measurement
#define THERMISTORPIN A5
// the value of the bias resistor
#define BIAS_RESISTOR 9926
//beta of thermistor
#define BCOEFFICIENT 3933 //RComponents, type 151-237
// thermistor resistance R0 from datasheet
#define R0 10000
//...at temperature
#define T0 25
//declare the function we'll use
float getThermistorTemperature(void);
//////////////////////////////////////////////////////////////
void setup() {
//start the serial monitor for printing to screen
Serial.begin(9600);
}
////////////////////////////////////////////////////////////
void loop() {
float msgData = getThermistorTemperature();
Serial.print("Temperature="); Serial.println(msgData);
delay(2000);
}
/////////////////////////////////////////////////////////////
// define the function getThermistorTemperature()
float getThermistorTemperature(void) {
float fTemperature;
float fADCReading;
float fThermistance; //thermistor resistance
fADCReading = analogRead(THERMISTORPIN);
// get the thermistor resistance from ADC reading
fThermistance = BIAS_RESISTOR/((1023 / fADCReading) - 1);
// convert resistance to temperature using the Beta equation
// see https://en.wikipedia.org/wiki/Thermistor
fTemperature = fThermistance / R0;
fTemperature = log(fTemperature);
fTemperature /= BCOEFFICIENT; // 1/Beta * ln(R/R0)
fTemperature += 1.0 / (T0 + 273.15); // ...+ (1/To)
fTemperature = 1.0 / fTemperature ; // ...invert
fTemperature -= 273.15; // ...convert to 'C
return fTemperature;
}

It works!





[ As an aside: Electronics newbies may wonder about the DD-5..blah number in photo 1. Old timers will recognize it straightaway as a drawing number. As you'll soon discover in electronics : If you don't write it down, it's gone! Say you spend one month working on a widget, then put it down for six. Sure as apples-is-apples, when you pick it back up, you won't have a clue why you stuck a resistor there, why pin 17 is ground and where-the-flip you saved your source code. What you needed was a record-keeping system! DD-5... is mine.]

O.k., so we have temperature data. Next time I'll talk more about sending it by CAN.

No comments:

Post a Comment