STM32F411 PWM for Micromouse Robot - Signal Generation, Frequency Formula, Recommended PWM Frequency, STM32 HAL PWM, PD Motor Control, Practical PWM Design
STM32F411 PWM for Micromouse Robot - Signal Generation, Frequency Formula, Recommended PWM Frequency, STM32 HAL PWM, PD Motor Control, Practical PWM Design
The STM32F411CEU6 is one of the most popular microcontrollers used in Micromouse robots because of its high processing speed, advanced timers, and efficient PWM generation capabilities. With a clock speed up to 100 MHz and multiple hardware timers, the STM32F411 can generate highly accurate PWM signals for precise motor control. PWM allows to control motor speed, steering response, and power efficiency by rapidly switching the motor driver ON and OFF.
In Micromouse applications, PWM generated by the STM32F411 is mainly used for:
- DC motor speed control
- Steering correction
- PD or PID control
- Motion profiling
- Acceleration and braking control
What is PWM?
PWM is a digital signal that alternates between HIGH and LOW states at a fixed frequency. The amount of time the signal remains HIGH during one cycle is called the duty cycle.
- 0% duty cycle → motor OFF
- 50% duty cycle → medium motor speed
- 100% duty cycle → maximum motor speed
The STM32 timer peripherals generate PWM signals directly in hardware, reducing CPU load and providing highly accurate timing.
In Micromouse robots, PWM signals are typically connected to motor drivers such as:
- STMicroelectronics motor drivers
- DRV8833
- TB6612FNG
- MX1508
These drivers amplify the low-current STM32 PWM signal and power the DC motors.
Why STM32F411 is Good for PWM
The STM32F411 includes several powerful timer peripherals optimized for motor-control applications.
Key advantages:
| Feature | Benefit |
|---|---|
| 100 MHz Cortex-M4 core | Fast control-loop execution |
| Hardware timers | Accurate PWM generation |
| Multiple PWM channels | Dual motor control |
| Advanced timers | Complementary PWM support |
| Low CPU overhead | Efficient multitasking |
| DMA support | High-speed waveform updates |
| Floating-point unit (FPU) | Faster control calculations |
These features make the STM32F411 ideal for fast and stable Micromouse motion control.
STM32F411 Timer Architecture
The STM32F411 contains multiple timers:
| Timer | Type | PWM Support |
|---|---|---|
| TIM1 | Advanced-control timer | Excellent |
| TIM2 | 32-bit general-purpose | Excellent |
| TIM3 | General-purpose | Good |
| TIM4 | General-purpose | Good |
| TIM5 | 32-bit timer | Excellent |
| TIM9~11 | Basic PWM support | Limited |
For Micromouse robots:
- TIM1 is commonly used for motor PWM
- TIM2/TIM5 are often used for encoder reading
- TIM3/TIM4 may handle sensors or auxiliary outputs
PWM Signal Generation
PWM is generated by comparing a timer counter with a compare register value. Motor speed is adjusted by changing the compare register value (CCR).
The STM32F411 timer continuously counts upward:
- Counter < CCR → output HIGH
- Counter > CCR → output LOW
The duty cycle determines average motor voltage.
Where:
- CCR = Compare register
- ARR = Auto-reload register
STM32F411 PWM Frequency Formula
The PWM frequency depends on:
- APB timer clock
- Prescaler (PSC)
- Auto-reload register (ARR)
Formula:
Example:
- Timer clock = 100 MHz
- PSC = 99
- ARR = 99
Result:
- PWM frequency = 10 kHz
This frequency is commonly used in Micromouse robots.
Recommended PWM Frequency for Micromouse
Typical PWM frequencies:
| Frequency | Result |
|---|---|
| 1~5 kHz | Audible motor noise |
| 10 kHz | Stable operation |
| 20 kHz | Silent operation |
| 30~40 kHz | Advanced high-speed systems |
Most STM32F411 Micromouse designs use:
- 20 kHz PWM
- 10-bit to 16-bit resolution
Higher frequencies reduce motor vibration and improve control smoothness.
STM32F411 PWM Pin Examples
Popular PWM pins on STM32F411:
| Timer | Channel | Pin |
|---|---|---|
| TIM3_CH1 | Channel 1 | PA6 Left A |
| TIM3_CH2 | Channel 2 | PA7 Left B |
| TIM3_CH3 | Channel 3 | PB0 Right A |
| TIM3_CH4 | Channel 4 | PB1 Right B |
STM32CubeMX PWM Configuration
PWM configuration using STM32CubeMX is straightforward.
Step 1: Enable Timer
- Select TIM1
- Enable PWM Generation CH1 and CH2
Step 2: Configure GPIO
CubeMX automatically configures alternate-function pins.
Step 3: Set Prescaler and Period
Example:
Prescaler = 99
Period = 99
Produces:
10 kHz PWM
Step 4: Generate Code
CubeMX generates initialization code automatically.
STM32 HAL PWM Code Example
Start PWM
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);
or
/* USER CODE BEGIN TIM3_Init 2 */
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_3);
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_4);
/* USER CODE END TIM3_Init 2 */
Set Motor Speed
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, left_pwm);
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, right_pwm);
Example
left_pwm = 50;
right_pwm = 50;
or
Set Motor Speed and direction
void CDriveMotors::setPWMLeft(uint16_t LpwmA, uint16_t LpwmB){
*MotorCfgParam[LEFT].PWM_TIM_CCRa = LpwmA; // backward
*MotorCfgParam[LEFT].PWM_TIM_CCRb = LpwmB; // foreward
}
void CDriveMotors::setPWMRight(uint16_t LpwmA, uint16_t LpwmB){
*MotorCfgParam[RIGHT].PWM_TIM_CCRa = LpwmA; // backward
*MotorCfgParam[RIGHT].PWM_TIM_CCRb = LpwmB; // foreward
}
LpwmA = This produces approximately 50% duty cycle.
Encoder Feedback with PWM
PWM alone cannot guarantee accurate movement because motor speed changes depending on:
- Battery voltage
- Surface friction
- Load
- Motor variation
Therefore Micromouse robots use encoder feedback.
Typical control loop:
- Read encoder counts
- Calculate speed
- Compare with target
- Adjust PWM duty cycle
This creates closed-loop motor control.
PWM in PD Motor Control
PWM is heavily used in PD-controlled Micromouse systems.
The controller continuously adjusts PWM duty cycle based on encoder error and wall sensor feedback.
Basic PD equation:
Where:
- = error
- = proportional gain
- = derivative gain
- = PWM output correction
The calculated control output modifies motor PWM values in real time.
Practical PWM Design Tips
Use High PWM Frequency
20 kHz is recommended to eliminate audible noise.
Keep Timer Resolution High
Higher ARR values improve speed precision.
Avoid Sudden PWM Changes
Implement acceleration ramps.
Use Separate Timers
Avoid sharing timers between motors and sensors.
Use Encoder-Based Correction
Open-loop PWM is not accurate enough for competition robots.
Conclusion
The STM32F411 is an excellent microcontroller for Micromouse PWM motor control. Its advanced timers, fast processing speed, and efficient hardware PWM generation make it ideal for precise robot movement.
By combining STM32F411 PWM with encoder feedback and PD control algorithms, a Micromouse robot can achieve:
- Stable speed control
- Accurate turning
- Smooth acceleration
- High-speed maze solving
- Reliable competition performance
Efficient PWM design is one of the key foundations of a successful STM32-based Micromouse system.


Comments
Post a Comment