Engineers have found that 80% of PT100 temperature measurement projects fail due to wiring details! When your instrument’s temperature suddenly fluctuates by ±10℃, and the three-wire connection makes the PCB layout a mess… Today, this comprehensive guide to avoiding pitfalls from CubeMX configuration to code implementation will help you completely solve the RTD temperature measurement challenges!
1. Industrial Grade Temperature Measurement Solution Selection: Why Choose MAX31865?
In industrial control, medical devices, and other scenarios, PT100 is the preferred temperature sensor due to its excellent stability and accuracy. However, traditional solutions face three major pain points:
-
Signal Attenuation: Long-distance transmission leads to measurement errors
-
Lead Resistance: Three-wire connections are complex and prone to errors
-
Calibration Difficulties: High threshold for implementing software compensation algorithms
Three Major Advantages of the MAX31865 Chip:
-
Built-in RTD driver circuit, supports 2/3/4 wire configurations
-
Automatically compensates for lead resistance errors (critical!)
-
15-bit ADC resolution (0.03125℃/bit)
-
Supports SPI interface, perfectly compatible with STM32

2. CubeMX Configuration in Four Steps: Precisely Building the Hardware Bridge
2.1 Clock Tree Configuration (Taking STM32F4 as an Example)
// Set in the Clock Configuration tab: - HSE: 8MHz (adjust according to actual crystal) - SYSCLK: 168MHz - APB1: 42MHz - APB2: 84MHz
Avoidance Tip: For SPI clocks exceeding 50MHz, enable prescaling!
2.2 SPI Peripheral Configuration
// Connectivity → SPI1 Mode: Full-Duplex Master Hardware NSS: Disable // Use GPIO to simulate chip select! Prescaler: 256 (resulting in 656.25KHz clock) CPOL: Low, CPHA: 1 Edge
2.3 Key GPIO Configuration
| Pin | Function | Configuration Mode |
|---|---|---|
| PA4 | SPI_CS (Chip Select) | Output Push-Pull |
| PA3 | DRDY (Data Ready) | EXTI Falling |
| PA5 | SPI_SCK | AF_PP |
| PA6 | SPI_MISO | AF_PP |
| PA7 | SPI_MOSI | AF_PP |

2.4 NVIC Interrupt Configuration
// System Core → NVIC EXTI line3 interrupt: Enable Preemption Priority: 1 Sub Priority: 1
Core Technique: Triggering interrupts on the falling edge allows for zero-wait data reading!
3. Hardware Wiring Pitfalls: Critical Details of Three-Wire Connections
3.1 Three-Wire vs. Four-Wire (Measured Comparison)
| Type | Accuracy | Anti-Interference | Applicable Scenarios |
|---|---|---|---|
| Two-Wire | ±5℃ | Poor | Short Distance (<1m) |
| Three-Wire | ±0.5℃ | Strong | Industrial Site (Critical!) |
| Four-Wire | ±0.1℃ | Very Strong | Laboratory Calibration |
3.2 MAX31865 Three-Wire Golden Connection
PT100 Pins → MAX31865-------------------------Red Wire (RTD) → RTD+ Black Wire (RTD) → RTD- & REF- Yellow Wire (Compensation) → REF+
Hard Lessons Learned: The length of the compensation wire must be equal to that of the RTD wire! Control the error within ±5cm
4. Code Practice: From Register Operations to Temperature Conversion Algorithms
4.1 Driver Function Encapsulation (Directly Copy and Use)
// PT100.h Header File Key Definitions #define MAX31685_CS_HIGH() HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_SET) #define MAX31685_CS_LOW() HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_RESET) // Single Byte Read (Core SPI Timing) uint8_t MAX31865_SB_Read(uint8_t addr) { uint8_t read; MAX31685_CS_LOW(); HAL_SPI_Transmit(&hspi1, &addr, 1, 60); HAL_SPI_Receive(&hspi1, &read, 1, 60); // 60ms timeout MAX31685_CS_HIGH(); return read;}
4.2 Detailed Explanation of Temperature Conversion Algorithm
float Get_tempture(void) { unsigned int data = (MAX31865_SB_Read(0x01)<<8) | MAX31865_SB_Read(0x02); data >>= 1; // Clear Fault Flag // Core Formula: Rt = (ADC Value / 32768) * Rref float Rt = (float)data / 32768.0f * RREF; // RREF=430Ω /* PT100 Temperature Characteristic Equation: T = (-A + sqrt(A² - 4B(1 - Rt/R0))) / (2B) A=3.9083e-3, B=-5.775e-7 */ if(Rt > 100.0f) { // Positive Temperature Range return (-a + sqrt(a*a - 4*b*(1 - Rt/Rt0))) / (2*b); } else { // Negative Temperature Fitting Polynomial (Maxim Official Algorithm) return -242.02 + 2.2228*Rt + 2.5859e-3*Rt*Rt - 4.8260e-6*pow(Rt,3) - 2.8183e-8*pow(Rt,4); }}
Accuracy Improvement Tips: Use a 0.1% precision resistor for RREF, and perform regular automatic calibration!
4.3 Interrupt-Driven Data Acquisition
// Interrupt Callback Function (Triggers Temperature Conversion) void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { if(GPIO_Pin == GPIO_PIN_3) { s_ucTemperFlag = 1; // Set Temperature Conversion Flag }} // Main Loop Processing while(1) { if(s_ucTemperFlag) { s_fNowTemper = Get_tempture(); OLED_ShowString(2,1,"T:%.3f C",s_fNowTemper); s_ucTemperFlag = 0; }}
5. Debugging Pitfall Checklist
-
Reading Fluctuation Issues
-
Check board-level filtering capacitors: VDD pin in parallel with 10μF + 0.1μF
-
SPI clock below 1MHz (high interference environment)
-245℃ Error Code
-
Three-wire connection error (focus on checking REF pin)
-
PT100 probe open circuit (measure resistance with a multimeter)
Accuracy Not Up to Standard
// Add configuration in the initialization function (critical for three-wire mode!) void MAX31865_Init() { MAX31865_SB_Write(0x80, 0xD1); // 0xD1=Three-Wire Mode + 50Hz Filtering}
4. Abnormal Power Consumption
-
Configure automatic shutdown mode: write to 0x80 register 0xXX3 (lowest two bits = 11)
6. Engineering Resources and Upgrade Guide
Upgrade Plan Preview:
-
Multi-node PT100 Networking (RS485 + Modbus)
-
Dynamic Temperature Compensation Algorithm (eliminating cold junction errors)
-
Self-Diagnostic System (real-time detection of disconnection faults)
Discussion: What pitfalls have you encountered in your PT100 projects? Have you faced bizarre temperature fluctuation issues?Feel free to share your experiences in the comments! If this article has solved your temperature measurement challenges that have troubled you for weeks, please like 👍→ view 🌟→ bookmark 📌 all three actions!
Original Declaration: This article is originally published by Embedded Xiao Jin, and sharing is welcome. For reprints, please contact for authorization.
Author: Xiao Jin
Previous Recommendations:
-
5 Steps to Master STM32 Hardware IIC! CubeMX + HAL Library Driver SHT40 Temperature and Humidity Sensor Avoidance Guide
-
STM32 Hardware SPI Driver 2.42-inch OLED (SSD1309) Practical Guide – Lighting Up 12864 High-Definition Display