MAX 31856 and Matlab
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

MAX 31856 and Matlab

by Carton on Thu Feb 20, 2020 10:40 am

Hello all,

I'm working on a project including the measure of temperature and simultaneous control of current sources. For that I pilot an Arduino Uno board via matlab's Arduino Support Package. Most of the measures are made by K type thermocouples and collected via analog pins and no problem arose. One specific measure requires a T type thermocouple, for that I got a MAX 31856 breakout board. The measurements are totally fine with the Arduino code given in the examples but I can't seem to make the switch to matlab.

By looking around the forum I found a matlab code for MAX 31855:
Code: Select all | TOGGLE FULL SIZE
%Thermocouple Data
clear all
therm = device(a,'SPIChipSelectPin','D10'); %Chip select pin D10, MISO is ICSP-1, SCK is ICSP-3, MOSI(not used)
for i=1:1000
dataIn = [1 1]; %write 2 pieces of info in order to receive 2 pieces of info
dataOut = writeRead(therm,dataIn,'uint16'); %uint16 is the largest integer you can use with this function therefore you need 2
x= [ x ; dataOut];
bytepack=uint32(dataOut(1)); %since there are two separate pieces you need to combine them to a 32-bit integer
bytepack=bitshift(bytepack,16); %data(1) is the 1st half and is converted to uint32 and the data is shifted to the correct side.
z = bitor(bytepack,uint32(dataOut(2))); %data(2) is the 2nd half and bitor() is used to place this into the correct side of uint32
z = dec2bin(z,32); %create a 32-bit binary number
centigrade=bin2dec(z(2:14))*0.25; %from the MAX31855 datasheet, bits 18-31 is the temperature in celcius. These number are referenced backwards in matlab.
% I left out the 1st bit because it was the signed integer, which I didn't need.
% Multiplied by 0.25 since the datasheet says the LSB is 0.25°C.
%Check the adafruit Max31855 library for an in-depth look at processing these bits.
temp = [temp, centigrade];
time = [time, t];

The resulting plot only gives a 0 value for "temp". By looking around the different variables I found that it is the first variable "dataOut" that gives a [0 0] vector (found with the x= [x ; dataOut] vector). So the problem apparently comes from either the writeRead function or the device function in itself.

If anybody have experience in this matter It'll be a huge help.

Posts: 1
Joined: Thu Feb 20, 2020 10:22 am

Re: MAX 31856 and Matlab

by jfindleyderegt on Tue May 11, 2021 5:34 pm

Hey, did you ever figure out what was going on with this?

Posts: 2
Joined: Mon May 10, 2021 1:57 pm

Re: MAX 31856 and Matlab

by jfindleyderegt on Wed May 12, 2021 2:31 pm

I've gotten the above code block to work. First, I had to install the "Instrument Control Toolbox". Then I amended the "writeRead" statement. It refers to "therm", which was never defined in the context. This page:
https://www.mathworks.com/help/supportp ... eread.html

Implies that writeRead is looking for the device definition in the position where it calls "therm", so I changed it to "dev", which is what the SPI device was defined as.

Posts: 2
Joined: Mon May 10, 2021 1:57 pm

Please be positive and constructive with your questions and comments.