STM32 Based Temperature Controlled Fan Simulation Design
Hello everyone, today I want to share an interesting temperature-controlled fan simulation project. This project simulates a temperature control system in a real working environment, detecting ambient temperature through a temperature sensor and automatically controlling the fan speed. The entire system is simple and practical, making it perfect for STM32 beginners to practice.
Hardware Circuit Design
Main hardware components:
- • STM32F103C8T6 Microcontroller (Main Control)
- • LM35 Temperature Sensor
- • L298N Motor Driver Module
- • DC Motor (Fan)
- • LCD1602 Display
- • Button Module
Key points of circuit design:
- • ADC channel PA0 connected to LM35 for temperature signal acquisition
- • PWM output port PA8 controls motor speed
- • LCD1602 is connected using a 4-bit data line
- • Independent buttons connected to PB1, PB2 for adjusting temperature threshold
STM32 Configuration and Program Design
1. Main Peripheral Configuration
// ADC configuration
void ADC_Config(void)
{
ADC_InitTypeDef ADC_InitStructure;
// ADC1 configured in independent mode
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
// Disable scan mode
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
// Continuous conversion mode
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_Init(ADC1, &ADC_InitStructure);
}
// PWM configuration
void TIM_PWM_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
// Configure time base unit
TIM_TimeBaseStructure.TIM_Period = 999;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
// Configure PWM channel
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
}
2. Temperature Acquisition Processing
float Get_Temperature(void)
{
u16 adcValue;
float temp;
// Start ADC conversion
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
// Wait for conversion to complete
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
adcValue = ADC_GetConversionValue(ADC1);
// LM35 temperature calculation formula: 10mV/℃
temp = (float)adcValue * (3.3 / 4096) * 100;
return temp;
}
3. Fan Control Algorithm
void Fan_Control(float temp)
{
uint16_t pwmValue;
// Temperature range control
if(temp < LOW_TEMP)
{
pwmValue = 0; // Stop
}
else if(temp < MID_TEMP)
{
pwmValue = 500; // 50% speed
}
else
{
pwmValue = 999; // 100% speed
}
TIM_SetCompare1(TIM1, pwmValue);
}
Proteus Simulation Settings
1. Simulation Device List
- • STM32F103C8
- • Virtual Terminal (Displays Temperature)
- • DC Motor
- • LM35
- • Power Module
2. Key Settings
- • LM35 output port connected to PA0 of STM32
- • Motor driver output connected to PA8 of STM32
- • Clock set to 72MHz
- • Add HEX file of STM32 program
Testing Plan
- 1. Temperature Detection Test
- • Use Proteus’s Interactive Temperature Source
- • Verify the accuracy of ADC sampling values
- • Observe oscilloscope waveform
- • Verify duty cycle changes
- • Adjust temperature threshold
- • Observe fan speed changes
Precautions
- • ADC sampling time must be long enough to ensure accurate temperature readings
- • PWM frequency should be set above 20kHz to avoid motor noise
- • Ensure the motor driver circuit power supply is independent
- • Software filtering should be added in practical applications
Troubleshooting Common Issues
- 1. Inaccurate Temperature Display
- • Check ADC reference voltage settings
- • Increase sampling times to take average values
- • Check PWM configuration
- • Verify connections of the driver circuit
Practical Suggestions
Start by building the minimum system for verification:
- 1. Only retain temperature acquisition functionality
- 2. Verify accuracy of ADC data
- 3. Gradually add PWM control
- 4. Finally, add display and button functionalities
The challenges of this project include:
- • Stability of ADC sampling
- • Precision control of PWM output
- • Optimization of temperature control algorithms
Recommended debugging tools:
- • STM32CubeMX: Quickly configure peripherals
- • Virtual Oscilloscope: Observe PWM waveform
- • Serial Debugging Assistant: Print debugging information
Finally, a reminder: The real environment may be more complex than simulations, so it is recommended to add:
- • Temperature sensor calibration function
- • Fault protection mechanism
- • Power-on self-check program