Thermocouple Interfacing usng MAX31855 | Raspberry Pi

Thermocouple Interfacing usng MAX31855 | JAVA & Pi4J| Raspberry Pi

thermocouple MAX31855Reading thermocouple data on Raspberry Pi using JAVA and Pi4J with the help of MAX31855 14 bit thermocouple to digital converter. The MAX31855 performs cold-junction compensation and digitizes the signal from a K-, J-, N-, T-, S-, R-, or E-type thermocouple. The data is output in a signed 14-bit, SPI-compatible, read-only format. This converter resolves temperatures to 0.25°C, allows readings as high as +1800°C and as low as -270°C, and exhibits thermocouple accuracy of ±2°C for temperatures ranging from -200°C to +700°C for K-type thermocouples.

Source : MAXIM Integrated(bit.ly/2i3GvZ4)

 

 


Links :
Running JAVA application on Pi: bit.ly/2hO2f84

Java Code: bit.ly/2h4GtOO

import java.io.IOException;
import java.nio.ByteBuffer;

import com.pi4j.io.spi.SpiChannel;
import com.pi4j.io.spi.SpiDevice;
import com.pi4j.io.spi.SpiFactory;

public class MAX31855_Thermocouple {
    public static SpiDevice spi = null;
    public static void main(String args[]) throws InterruptedException, IOException {
    	
    	System.out.println("Starting Thermocouple Application.");
        spi = SpiFactory.getInstance(SpiChannel.CS0,SpiDevice.DEFAULT_SPI_SPEED,SpiDevice.DEFAULT_SPI_MODE);
        while(true){
        	System.out.println(getConversionValue()); //Print out red ADC Sample Counts
            Thread.sleep(2000);
        }
        
    }
    public static double getConversionValue() throws IOException {

        byte data[] = new byte[] {0,0, 0, 0};// Dummy payloads. It's not responsible for anything.
       
        byte[] result = spi.write(data); //Request data from MAX31855 via SPI with dummy pay-load
        
        if((result[0] & 128)==0 && (result[1] & 1)==1 ) {//Sign bit is 0 and D16 is high corresponds to Thermocouple not connected.
            System.out.println("Thermocouple is not connected");
            return 0;
        }
        String stringResult=String.format("%32s",Integer.toString(ByteBuffer.wrap(result).getInt(), 2)).replace(' ', '0');
        double valInt=0.0;
        
        if(stringResult.charAt(0)=='1' ){  //Checking for signed bit. If need to convert to 2's Complement.
         	StringBuilder onesComplementBuilder = new StringBuilder();
         	
        	for(char bit : stringResult.substring(0, 12).toCharArray()) {
        	    onesComplementBuilder.append((bit == '0') ? 1 : 0);  // if bit is '0', append a 1. if bit is '1', append a 0.
        	}
        	String onesComplement = onesComplementBuilder.toString();
        	valInt = -1*( Integer.valueOf(onesComplement, 2) + 1); // two's complement = one's complement + 1. This is the positive value of our original binary string, so make it negative again.
        	
        }else{
        	valInt=Integer.parseInt(stringResult.substring(0, 12),2); //+ve no convert to double value
        }
        
        if(stringResult.charAt(12)=='1') //Check for D18 and D19 for fractional values
        	valInt+=0.5;	
        if(stringResult.charAt(13)=='1')
        	valInt+=0.25;
        
        return valInt;
    }
}

Github Repo: bit.ly/2i9yXzT
MAX31855 Datasheet: bit.ly/2h60KVR

Buy MAX31855: amzn.to/2hP5VZB
—————————————————————————————————
Subscribe YouTube: bit.ly/2gsIZ2g

Guys Subscribe to my channel for latest contents into your inbox.
Support me to keep going.
___________________________________________

Website : www.weargenius.in
Twitter : twitter.com/geekybikash
YouTube : www.youtube.com/weargenius
Instagram : www.instagram.com/weargenius/
GIT : github.com/oksbwn

Bikash Panda
Catch Me On

Bikash Panda

Blogger / Embedded System Developer at WeArGenius
A techie, tinkerer and tech lover, who loves to blog and feels everyone can learn tech provided they have the right attitude towards learning and passion. By profession, I am an IOT developer working in Smart Home/ Smart Grid domain.
Bikash Panda
Catch Me On

Related posts