This project demonstrates how to implement an H-Bridge DC motor control system using an STM32F103C6 microcontroller. The system provides bidirectional control of a DC motor through GPIO pins for direction and PWM for speed regulation. By manipulating the H-Bridge configuration, the motor can rotate in both clockwise and counterclockwise directions, while PWM techniques enable precise speed control. Real-time status feedback is provided via UART, and a full Proteus simulation validates the complete motor control system.
The H-Bridge circuit, typically implemented with an IC like the L298N, uses a transistor configuration that forms an "H" pattern, allowing current to flow through the motor in both directions by controlling the saturation states of different transistor pairs.
STM32CubeMX Setup- MCU Selection: STM32F103C6 (72 MHz clock)
- GPIO Configuration:
-
PA4, PA5 configured as Output Push-Pull (Motor direction control pins IN1, IN2)
- TIM2 Configuration:
-
Channel 1 (PA0) configured as PWM Generation
-
Prescaler: 72-1 (1 MHz timer clock)
-
Counter Period: 999 (1 kHz PWM frequency)
-
Pulse: 0 (initial duty cycle)
- UART1 Configuration:
-
Baud Rate: 115200
-
Word Length: 8 bits
-
Parity: None
-
Stop Bits: 1
- Generate initialization code in STM32CubeIDE.
Key Functions
- Button Debouncing & State Handling
-
void HandleStartStop(void);
-
Manages the start/stop button with debouncing, toggles motor state, and sends UART status messages.
-
void HandleDutyCycle(void);
-
Handles duty cycle button press with debouncing and cycles through speed settings (25%, 50%, 75%, 99%).
- PWM Control & Motor Management
-
void UpdatePWM(void);
-
Updates PWM duty cycle based on current setting, calculates angular acceleration, and sends UART updates with motor status.
- UART Communication
-
void SendUartMessage(char* message);
-
Transmits status and debugging messages via UART to a serial terminal.
- Main Loop Logic
while (1)
{
HandleStartStop(); // Check start/stop button
HandleDutyCycle(); // Check speed change button
}
Required Components:
- STM32F103C6 microcontroller
- H-Bridge Motor Driver
- DC Motor
- Virtual Terminal (UART)
Connections:
- Direction Control: PA4 → L298N IN1, PA5 → L298N IN2
- Speed Control: PA0 → L298N ENA (PWM input)
- Motor Connections: L298N outputs → DC Motor terminals
- UART1: PA9 (TX) → Virtual Terminal RX
- Power: L298N VSS → 5V, VS → Motor voltage (e.g., 12V)
Steps:
- Load the.hex file compiled from CubeIDE.
- Run the simulation.
- Observe motor direction changes and speed variations.
- Monitor the real-time status updates on the Virtual Terminal showing direction and PWM duty cycle.
If you have any questions or suggestions don't hesitate to leave a comment below
Comments