Key Takeaway: Build a complete closed-loop PID temperature controller using an STM32F103 Blue Pill, DS18B20 temperature sensor, and an IRF520 MOSFET driving a heater element — with adjustable setpoint, real-time LCD display, and auto-tuned PID gains for ±0.5°C accuracy.
Table of Contents
1. Project Overview
Precision temperature control is a fundamental requirement in countless industrial and hobbyist applications — from reflow soldering ovens and 3D printer hotends to incubators, chemical reactors, and environmental chambers. While basic on-off (bang-bang) controllers are simple to implement, they suffer from overshoot, oscillation, and poor steady-state accuracy. A proper STM32 PID temperature controller solves all of these problems.
This guide walks through the complete build of a digital PID temperature controller using the STM32F103C8T6 (Blue Pill) microcontroller. We cover hardware selection, circuit design, PID implementation in C using STM32 HAL libraries, and practical tuning methods to achieve professional-grade thermal regulation.
If you are new to STM32 development, read our STM32 Beginner’s Guide 2026 first for STM32CubeIDE setup and basic project creation steps.
2. Required Components
| Component | Specification | Quantity |
|---|---|---|
| STM32F103C8T6 (Blue Pill) | 72MHz ARM Cortex-M3 | 1 |
| DS18B20 Temperature Sensor | -55°C to +125°C, ±0.5°C accuracy | 1 |
| IRF520 MOSFET Module | Logic level, 9.6A max | 1 |
| 1602 LCD with I2C Module | 16×2 characters, PCF8574 | 1 |
| 10kΩ Potentiometer | Setpoint adjustment | 1 |
| 4.7kΩ Resistor | OneWire pull-up | 1 |
| Heater Element | 12V / 24V / 48V (match your supply) | 1 |
| 12V/2A Power Supply | For heater + MOSFET | 1 |
| Breadboard + Jumper Wires | For prototyping | 1 set |
Buy STM32 Blue Pill starter kit on Amazon | Order from Mouser India
3. Circuit Diagram and Wiring
The wiring for this STM32 PID temperature controller project is straightforward. Connect the components as follows:
DS18B20 (OneWire protocol):
– VDD to STM32 3.3V
– GND to GND
– DATA to PB0 on STM32
– Pull PB0 to 3.3V through 4.7kΩ resistor
1602 LCD I2C:
– VCC to STM32 5V
– GND to GND
– SDA to PB7 (I2C1 SDA)
– SCL to PB6 (I2C1 SCL)
Setpoint Potentiometer:
– Wiper to PA0 (ADC1 channel 0)
– Other pins to 3.3V and GND
Heater via MOSFET:
– IRF520 gate (SIG) to PA8 (TIM1 PWM)
– IRF520 V+ to heater positive
– IRF520 V- to GND
– Heater negative to 12V return path
4. PID Theory and Tuning
The PID algorithm is the heart of this controller. The control output is calculated as:
u(t) = Kp • e(t) + Ki • ∫e(t)dt + Kd • de(t)/dt
Where:
– Kp (Proportional): Reacts to current error. A high Kp causes aggressive response but can overshoot.
– Ki (Integral): Eliminates steady-state error by accumulating past errors. Too high causes oscillation.
– Kd (Derivative): Predicts future error from the rate of change. Dampens overshoot but amplifies noise.
For a thermal system with a 100ms control loop, starting gains of Kp=2.5, Ki=0.08, Kd=1.2 work well for most small heaters (25-100W range). Our implementation uses the standard positional PID form with integral windup protection and output clamping.
Auto-tuning method: Implement a simple relay-based auto-tune that cycles the heater on/off around the setpoint, measures the oscillation period and amplitude, then applies Ziegler-Nichols rules to calculate PID gains automatically.
5. STM32CubeIDE Code Implementation
Here is the core PID control loop implementation in C using the STM32 HAL library:
typedef struct {
float Kp, Ki, Kd;
float integral, prev_error;
float out_min, out_max;
float dt; // 100ms = 0.1f
} PID_Controller;
void PID_Init(PID_Controller *pid, float Kp, float Ki, float Kd) {
pid->Kp = Kp; pid->Ki = Ki; pid->Kd = Kd;
pid->integral = 0; pid->prev_error = 0;
pid->out_min = 0; pid->out_max = 100;
pid->dt = 0.1f;
}
float PID_Compute(PID_Controller *pid, float setpoint, float input) {
float error = setpoint - input;
pid->integral += error * pid->dt;
// Anti-windup: clamp integral
if (pid->integral > pid->out_max / pid->Ki)
pid->integral = pid->out_max / pid->Ki;
if (pid->integral < pid->out_min / pid->Ki)
pid->integral = pid->out_min / pid->Ki;
float derivative = (error - pid->prev_error) / pid->dt;
float output = pid->Kp * error + pid->Ki * pid->integral + pid->Kd * derivative;
// Clamp output
if (output > pid->out_max) output = pid->out_max;
if (output < pid->out_min) output = pid->out_min;
pid->prev_error = error;
return output;
}
// Called in TIM3 interrupt every 100ms
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
if (htim->Instance == TIM3) {
float temp = DS18B20_ReadTemperature();
uint16_t adc = HAL_ADC_GetValue(&hadc1);
float setpoint = (adc / 4095.0f) * 100.0f; // 0-100°C
float output = PID_Compute(&pid, setpoint, temp);
// PWM duty cycle = output%
uint16_t pulse = (uint16_t)(output / 100.0f * 999);
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, pulse);
LCD_Display(setpoint, temp, output);
}
}
This PID controller code runs in the TIM3 interrupt at 10 Hz (100ms period). The DS18B20 temperature data is read via the OneWire protocol on PB0, the setpoint is sampled from the ADC on PA0, and the computed output drives the PWM duty cycle on PA8 to control the MOSFET-driven heater.
For I2C communication with the LCD display, refer to our I2C Protocol Guide 2026 for detailed wiring and configuration steps.
6. Testing and Calibration
After uploading the firmware, follow these steps to test your STM32 PID temperature controller:
Step 1: Power on the system. The LCD should display the current temperature, setpoint, and output power percentage.
Step 2: Turn the potentiometer to set a target temperature of approximately 40°C. The heater should begin warming up and the PWM percentage should increase.
Step 3: Observe the temperature approaching the setpoint. A well-tuned PID controller should settle within ±0.5°C within 30-60 seconds without significant overshoot.
Step 4: If the system oscillates (temperature keeps swinging past the setpoint), reduce Kp by 30% and increase Kd slightly. If response is too slow, increase Kp or Ki.
Step 5: For advanced tuning, implement the relay auto-tune routine. The system will output a square wave and measure the ultimate gain and period for Ziegler-Nichols tuning.
| Symptom | Root Cause | Fix |
|---|---|---|
| Sustained oscillation | Kp too high | Reduce Kp by 40-50% |
| Slow to reach setpoint | Kp too low or heater underpowered | Increase Kp by 30% |
| Steady-state offset | Ki too low | Increase Ki by 0.02 increments |
| Rapid output changes | Kd amplifying sensor noise | Apply moving average filter to input |
Frequently Asked Questions
What is the maximum temperature this STM32 PID temperature controller can handle?
The DS18B20 sensor is rated for -55°C to +125°C. For higher temperatures, replace it with a thermocouple amplifier (MAX6675 or MAX31855) and use a solid-state relay instead of the MOSFET.
Can I use this controller for a reflow soldering oven?
Yes, with modifications. You will need a thermocouple (K-type), a MAX6675 module, and an SSR rated for the oven’s AC heater current. The PID code remains largely the same — just change the temperature sensor read function.
What is the difference between positional and velocity PID?
Positional PID computes the absolute output directly, while velocity PID computes the change in output each cycle. This implementation uses positional PID, which is simpler for thermal applications. Velocity PID is preferred for systems where the output must change smoothly without bumps.
How do I add Wi-Fi or Bluetooth connectivity?
Connect an ESP8266 or HC-05 Bluetooth module via UART to the STM32. The current temperature, setpoint, and PID status can be streamed over serial at 115200 baud. For a complete IoT implementation, see our ESP32 MQTT Smart Home System Guide for the software architecture.
Sources
- STM32F103 Product Page — STMicroelectronics
- PID Control Loop Tuning Guide — Analog Devices
- DS18B20 Programmable Resolution 1-Wire Digital Thermometer — Maxim Integrated
- PWM Motor Control with MOSFET Drivers — Texas Instruments
Disclosure: This post contains affiliate links. We may earn a small commission if you purchase through these links at no additional cost to you.

