How to read my RTD-sensor data using MAX31865 and NUCLEO-F767ZI?

Hi everyone,

I’m struggling here to make an SPI-code work :smiling_face_with_tear: . My circuit contains the following hardware:

Since the MAX31865 requires SPI protocol I wrote this code to read my RTD-data:

#include “mbed.h”

SPI spi(PB_5, PB_4, PB_3); // mosi, miso, sclk
DigitalOut cs(PA_4);

int main()
{
// Chip must be deselected
cs = 1;

// Setup the spi for 8 bit data, high steady state clock,
// second edge capture, with a 1MHz clock rate
spi.format(8, 1);
spi.frequency(1000000);

// Select the device by seting chip select low
cs = 0;

// Send a dummy byte to receive the contents of the register
int x = spi.write(0x00);
printf("register = 0x%X\n", x);

// Deselect the device
cs = 1;

}

Does someone know why I’m not getting any data?

best regard,
Ezzow

First of all, Mbed 6 supports using any GPIO pin as a chip select without doing it manually. If anyone tells you different they’re living in the past :slight_smile: . So, you can do:

SPI spi(PB_5, PB_4, PB_3, PA_4, use_gpio_ssel);

and then do:

spi.select();
spi.write(...);
spi.deselect();

instead of using a DigitalOut for CS.

Second, looking at the datasheet, reading the any data from that chip requires at least two bytes (even though they call it a “single byte read”): one address byte and one or more data bytes. I think that to read the RTD resistance, you probably want to do something like this:

spi.select();
spi.write(0x01); // read register 1, RTD resistance
uint8_t resistanceHigh = spi.write(0x00); // value written is a don't care
uint8_t resistanceLow = spi.write(0x00); // value written is a don't care
spi.deselect();

You could also do this with the transaction API:

char dataIn[1] = {0x01}; 
char dataOut[3];
spi.write(dataIn, 1, dataOut, 3); // note: will send/receive three bytes, the last two bytes sent will be the SPI fill char since the in array is only one byte long/

Hi @MultipleMonomials,

I really appreciate your help!

I added your parts to my code but it didn’t really help. Could you please suggest any kind of tutorials to know what to write to be able to read my data? I’m from another field and would like to understand how this works.

Thank you!

Hi @MultipleMonomials,

After several tries I wrote the following code:

<>
#include “mbed.h”
#include
#include
#include

SPI spi(PB_5, PB_4, PB_3,PA_4,use_gpio_ssel);

double resistance;
uint8_t reg1, reg2; // reg1 holds MSB, reg2 holds LSB for RTD
uint16_t fullreg; // fullreg holds the combined reg1 and reg2
double temperature;

int main() {
while (true) {

/
spi.format(8, 1);
spi.frequency(1000000);

cs = 0;
spi.write(0x80); // 80h = 128 - config register
spi.write(0xB0); // B0h = 10100000bias ON, 1-shot, start 1-shot, rest are 0
cs = 1;

cs = 0;
spi.write(1);           // we want to read the first register
reg1 = spi.write(0xFF); // dummy data:  read part 1 of the first register
reg2 = spi.write(0xFF); // dummy data:  read part 2 of the first register
cs = 1;

// combinate reg1 and reg2
fullreg = reg1;       // read MSB
fullreg <<= 8;        // shift to the MSB part
fullreg |= reg2;      // read LSB and combine it with MSB
fullreg >>= 1;        // shift D0 out
resistance = fullreg; // pass the value to the resistance

cs = 0;
spi.write(0x80); // 80h = 128
spi.write(144);  // 144 = 10010000


cs = 1;

printf("RTD resistance=%f\n", resistance);

}
}
<>

At first I only want to read the RTD resistance (ADC CODE) measured by MAX31865. However, I’m getting a value of 27300, where it should be around 8900 based on the following table (source: MAX31865 datasheet):

do you have any explanation?

thank you!

best regards,

ezzow

problem solved! thanks :smiley: