Clear Filters
Clear Filters

Help read temperature data from MAX6675 sensor connected to Arduino

19 views (last 30 days)
We have a k-type thermocouple that is connected to the MAX6675 board that is then connected to our arduino mega 2560 which is connected to matlab. The SO pin is in D42, the SCK is in D52, the CS pin is in D50, the VCC is connected to the 5Volts and the GND is connected to the ground. The code we were using so far only is outputting 0 no matter what we try. We know the sensor is working after testing it on the arduino ide software, so there is something wrong with our code and how we are sending/recieving data from the arduino. Any help is apperciated. This code was from another post here on matlab, we think the issue is with datOut, it only has been 0 the whole time we have been testing with it.
% KTYPE.m This example is based on the reference manual of the MAX6675 amplifier
% LED Versuch
clear all, close all
%ComPort = getAvailableComPort
a = arduino('COM3','Uno','Libraries','SPI');
dev = device(a,'SPIChipSelectPin','D8');
MAX6675 = device(a,'SPIChipSelectPin','D10');
tmax= 30;
fs = 1;
dt = 1/fs;
t = [dt:dt:tmax];
il = length(t);
T = zeros(il,1);
dummy = [uint8(0),uint8(0)];
for i=1:il
datOut = writeRead(MAX6675,dummy); % Read MAX6675
pause(0.5)
BINR = [dec2bin(datOut(1),8), dec2bin(datOut(2),8)]; % Show the bits of the read
DECR = bin2dec(BINR);
DECT = bitshift(DECR,-3); % Shift by 3 bits to the right
BINT = dec2bin(DECT); % Just to show the change
MP = 255/1023.75; % Reading 0 means 0°C,
% Reading 255 its hot as 1023.75°C
T(i,1) = MP*DECT; % Factor to get °C
disp(T(i,:))
end

Answers (1)

Muskan
Muskan on 17 Jun 2024
Hello Andrew,
After going through the code, here are a few pointers:
1. Correct the SPI Configuration
The first issue is with the SPI configuration, specifically the "SPIChipSelectPin" and the board type. You mentioned using an Arduino Mega 2560, but the code initializes the Arduino object for an Uno. The pin assignments also need to be corrected according to your wiring.
Modify the Arduino object initialization to match the Mega 2560 and correct the according to your setup. Make sure the "SPIChipSelectPin" ('D50') matches the pin to which the CS pin of the MAX6675 is connected.
2. Verify the SPI Pins.
3. Given the initialization correction, the rest of your code seems logically sound for reading the temperature. However, ensure that the "dummy" array you're sending with "writeRead" matches the expectation of the MAX6675 for initiating a read.

Categories

Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!