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
MicromousePWMSignal

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.

Duty Cycle(%)=TONTTOTAL×100Duty\ Cycle(\%) = \frac{T_{ON}}{T_{TOTAL}} \times 100

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:

FeatureBenefit
100 MHz Cortex-M4 coreFast control-loop execution
Hardware timersAccurate PWM generation
Multiple PWM channelsDual motor control
Advanced timersComplementary PWM support
Low CPU overheadEfficient multitasking
DMA supportHigh-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:

TimerTypePWM Support
TIM1Advanced-control timerExcellent
TIM232-bit general-purposeExcellent
TIM3General-purposeGood
TIM4General-purposeGood
TIM532-bit timerExcellent
TIM9~11Basic PWM supportLimited

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.

Duty Cycle=CCRARR+1×100Duty\ Cycle=\frac{CCR}{ARR+1}\times100

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:

fPWM=fTIM(PSC+1)(ARR+1)f_{PWM}=\frac{f_{TIM}}{(PSC+1)(ARR+1)}

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:

FrequencyResult
1~5 kHzAudible motor noise
10 kHzStable operation
20 kHzSilent operation
30~40 kHzAdvanced 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.


MicromousePWMConnection
STM32F411 PWM Pin Examples

Popular PWM pins on STM32F411:

TimerChannelPin
TIM3_CH1Channel 1PA6  Left A
TIM3_CH2Channel 2PA7  Left B
TIM3_CH3Channel 3PB0  Right A
TIM3_CH4Channel 4PB1  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:

  1. Read encoder counts
  2. Calculate speed
  3. Compare with target
  4. 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:

u(t)=Kpe(t)+Kdde(t)dtu(t)=K_p e(t)+K_d \frac{de(t)}{dt}

Where:

  • e(t)e(t) = error
  • KpK_p = proportional gain
  • KdK_d = derivative gain
  • u(t)u(t) = 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

Popular posts from this blog

How to Build a Micromouse Robot - Mechanical, Hardware, Software

Micromouse Competitions - Types, Overview, Comparison, Advantages, Worldwide

Complete Guide to Micromouse Maze - Dimensions, Structure and Components, Building