Sunday 8 February 2015

Temperature Measurement using K-type Thermocouple and Arduino

For Industrial temperature Measurement Thermocouples are Best choice because of its robust nature, wide range of temperature measurement and no self heating...
But for Beginners like me Dealing with Thermocouples is a nightmare because of all the hurdles of noise reduction and cold junction compensation.

Please go through the wikipedia page or google to know what is noise involvement in thermocouple and cold junction compensation because I barely know what it is and I dont want to deal with them Now.

Why...?

I learn that a nice IC like Max6675 will take care of both of them (But Expensive... ) and it looks like a best route to go through the thermocouple this time.

From datasheet::
The MAX6675 performs cold-junction compensation and
digitizes the signal from a type-K thermocouple. The data is output in a 12-bit resolution, SPI™-compatible,
read-only format.


so what i wanted to do is to just connect thermocouple wires to the IC and let the IC take care of Temp Measurement and get the reading from Max6675 IC's SPI interface....

Ok.. that's a super easy thing and let's go take a look at the Pin configuration
Max 6675 Pin configuration

there are just 8 pins and 3 of them are GND(pin 1) , VCC(pin 4) and one No Connection Pin(Pin 8)

and two pins to connect our thermocouple. T+(Pin 2) and T-(Pin 3)

and Three Pin's to read the Temp data from Ic i.e SPI connection Pins. SCK (pin 5), CS(pin 6) and SO (pin 7)

as already said we get temp data via SPI.. Ok that's good.. Not enough Information to proceed further.
what we wanted to know is In What Format we are going to receive the data?
In datasheet

the max6675 process the reading from thermocouple and transmit the data through the serial interface
Force CS low and apply clock signal at sck to read the result at SO


ok.. what does it mean by force CS to low ? it means simply sending a logic 0 to the CS pin.(Note that CS is a active low input)

after doing that the result will be available at SO. But In what format?

The first bit, D15, is a dummy sign bit and is always
zero. Bits D14–D3 contain the converted temperature in the order of MSB to LSB. Bit D2 is normally low and
goes high when the thermocouple input is open. D1 is low to provide a device ID for the MAX6675 and bit D0
is three-state.


max6675 Serial configuration

so we need total 16 pulses to read the entire data on SO pin and bits D3 - D14 contain Our information.

the code to do the temp measurement is..

#define cs_pin 10
#define so_pin 12
#define sck_pin 13

void setup(){
Serial.begin(9600);
pinMode(sck_pin ,OUTPUT);
pinMode(cs_pin,OUTPUT);
digitalWrite(cs_pin,HIGH);// cs_pin is a active low pin
pinMode(so_pin,INPUT);
}


void loop(){
Serial.println("Getting the Temperature Value");
Serial.print("Temp in C is -->");
Serial.print(get_value());
Serial.println();
}

//defining the get value function
float get_value(){
digitalWrite(cs_pin,LOW);
delay(2);
digitalWrite(cs_pin,HIGH);
//give some time for conversion to take place
delay(220);//220 milli seconds delay - Refer Data Sheet
// now we have our reading ready..
// we can read the data from so line by sending clock pulses from sck pin
read_dummy_bit();
int temp = read_temp_data();
read_open_TC_bit();
read_remaining_bits();
//disable the device
digitalWrite(cs_pin,HIGH);
// calculate the temp value
float temp_value = temp *0.25; // why 0.25 ?
// the resolution of Max 6675 is 0.25 C
return temp_value;
}//end of get_value() function

//read_dummy_bit() function
void read_dummy_bit(){
digitalWrite(sck_pin,HIGH);
digitalRead(so_pin); // read the value and discard it.
digitalWrite(sck_pin,LOW);
}//end of read_dummy_bit() function

//read_temp_data() function
int read_temp_data(){
int value = 0;
for (int i = 11; i <= 0; i--){
// why are we decrementing and shifting in the descending order ..?
// because we receive the MSB first
digitalWrite(sck_pin,HIGH);
value += digitalRead(so_pin) << i;
digitalWrite(sck_pin,LOW);
}//end of for loop
return value;
}//end of read_temp_data() function

//read_opn_TC_bit() function
void read_open_TC_bit(){
digitalWrite(sck_pin,HIGH);
if(digitalRead(so_pin)){
Serial.println("Please check the connection of thermocouple wires");
}//end of else
digitalWrite(sck_pin,LOW);
}//end of read_open_TC_bit() function

//read_remaining_bits() function
void read_remaining_bits(){
for(int i=0;i<=1;i++){
digitalWrite(sck_pin,HIGH);
delay(1);//just leave the value we dont need them
digitalWrite(sck_pin,LOW);
}//end of for loop
}//end of read_remaining_bits() function

I will explain the code in the next post

No comments:

Post a Comment