In this blog post, we explore how to control dual brushed DC motors using an STM32F103C8 microcontroller paired with an L298 H-bridge driver. We'll cover the circuit design, practical applications in robotics and automation, and a step-by-step explanation of how the system works, making it accessible for both hobbyists and students learning embedded motor control.
1. What the Circuit is For
This circuit is designed to control the direction and state of two independent brushed DC motors using an 32-bit ARM Cortex-M3 microcontroller (STM32F103C8) interfaced to a high-current dual H-bridge motor driver IC (L298). It provides precise micro-controller logic driving high-current inductive loads (motors).
2. Where It Can Be Used
- Differential Drive Mobile Robots: Automated Guided Vehicles (AGVs), small rover platforms, and wheeled robotics requiring forward, reverse, and turning maneuvers.
- Automated Conveyors & Actuation: Dual-axis motorized positioning, linear actuator driving, and conveyor belt control in automation test rigs.
- RC & Educational Robotics: Demonstrations of embedded C programming, GPIO interfacing, power switching, and motor control principles.
3. How It Works
A. Power & Logic Connections
- Microcontroller System (U1): Powered by a +3.3V supply (VCC/VDD). Filtering capacitors (C1, C2, C3, C5, C6) decouple power supply noise. A pull-up resistor (R1), capacitor (C4), and push button (BUTTON) form the hardware reset line on pin NRST.
- Motor Driver IC (U2): Powered by dual supply inputs:
- Logic supply voltage (+5V) on pin 9 (VCC).
- Motor drive voltage (+9V) on pin 4 (VS) to drive the motors.
- Status Indicator: An onboard status LED (D1) connected to pin PC13 via current-limiting resistor R3 toggles to indicate operational state changes.
B. Control Signals
- Motor Channel A Control (MOTOR A):
- Enable Pin: PB15 drives ENA (Pin 6).
- Direction Inputs: PA11 drives IN1 (Pin 5) and PA10 drives IN2 (Pin 7).
- Outputs OUT1 (Pin 2) and OUT2 (Pin 3) connect across Motor A.
- Motor Channel B Control (MOTOR B):
- Enable Pin: PB14 drives ENB (Pin 11).
- Direction Inputs: PA9 drives IN3 (Pin 10) and PA8 drives IN4 (Pin 12).
- Outputs OUT3 (Pin 13) and OUT4 (Pin 14) connect across Motor B.
C. Firmware Execution Sequence (main.c)
Code
#include
// Pin Definitions based on Schematic Wiring
// Motor A (Motor 1)
#define ENA_PIN 15 // PB15 -> ENA
#define IN1_PIN 11 // PA11 -> IN1
#define IN2_PIN 10 // PA10 -> IN2
// Motor B (Motor 2)
#define ENB_PIN 14 // PB14 -> ENB
#define IN3_PIN 9 // PA9 -> IN3
#define IN4_PIN 8 // PA8 -> IN4
// Status LED
#define LED_PIN 13 // PC13 -> LED
void delay_ms(volatile uint32_t count) {
for (count *= 2000; count > 0; count--);
}
void GPIO_Init(void) {
// 1. Enable GPIO Clocks (GPIOA, GPIOB, GPIOC)
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN | RCC_APB2ENR_IOPCEN;
// 2. Configure PA8, PA9, PA10, PA11 as Outputs (Push-Pull, 2MHz)
GPIOA->CRH &= ~((0xF << 0) | (0xF << 4) | (0xF << 8) | (0xF << 12)); // Clear PA8, PA9, PA10, PA11
GPIOA->CRH |= ((0x2 << 0) | (0x2 << 4) | (0x2 << 8) | (0x2 << 12)); // Set to Output 2MHz Push-Pull
// 3. Configure PB14, PB15 as Outputs (Push-Pull, 2MHz)
GPIOB->CRH &= ~((0xF << 24) | (0xF << 28)); // Clear PB14, PB15
GPIOB->CRH |= ((0x2 << 24) | (0x2 << 28)); // Set to Output 2MHz Push-Pull
// 4. Configure PC13 (LED) as Output
GPIOC->CRH &= ~(0xF << 20);
GPIOC->CRH |= (0x2 << 20);
}
int main(void) {
GPIO_Init();
// Enable Motor Drivers (ENA & ENB High)
GPIOB->BSRR = (1 << ENA_PIN) | (1 << ENB_PIN);
while (1) {
// --- 1. Move Forward ---
// Motor A: IN1 HIGH, IN2 LOW
// Motor B: IN3 HIGH, IN4 LOW
GPIOA->BSRR = (1 << IN1_PIN) | (1 << IN3_PIN); // Set HIGH
GPIOA->BRR = (1 << IN2_PIN) | (1 << IN4_PIN); // Set LOW
GPIOC->ODR ^= (1 << LED_PIN); // Toggle LED
delay_ms(2000);
// --- 2. Move Reverse ---
// Motor A: IN1 LOW, IN2 HIGH
// Motor B: IN3 LOW, IN4 HIGH
GPIOA->BRR = (1 << IN1_PIN) | (1 << IN3_PIN); // Set LOW
GPIOA->BSRR = (1 << IN2_PIN) | (1 << IN4_PIN); // Set HIGH
GPIOC->ODR ^= (1 << LED_PIN); // Toggle LED
delay_ms(2000);
// --- 3. Turn Right (Motor A Reverse, Motor B Forward) ---
GPIOA->BRR = (1 << IN1_PIN) | (1 << IN4_PIN);
GPIOA->BSRR = (1 << IN2_PIN) | (1 << IN3_PIN);
GPIOC->ODR ^= (1 << LED_PIN);
delay_ms(1000);
// --- 4. Stop Both Motors ---
GPIOA->BRR = (1 << IN1_PIN) | (1 << IN2_PIN) | (1 << IN3_PIN) | (1 << IN4_PIN);
GPIOC->ODR ^= (1 << LED_PIN);
delay_ms(1000);
}
}
- Initialization: Configures GPIO pins for outputs (2MHz Push-Pull mode) and sets enable pins ENA and ENB HIGH.
- Forward State (2 sec): IN1=HIGH / IN2=LOW and IN3=HIGH / IN4=LOW. Both motors rotate forward.
- Reverse State (2 sec): IN1=LOW / IN2=HIGH and IN3=LOW / IN4=HIGH. Both motors reverse rotation.
- Right Turn State (1 sec): IN1=LOW / IN2=HIGH and IN3=HIGH / IN4=LOW. Motor A reverses while Motor B moves forward to turn the platform.
- Stop State (1 sec): All input lines (IN1..IN4) are set LOW to stop motor motion.
- Status Update: The MCU toggles LED D1 on pin PC13 at each phase transition.
Video Demonstration
See also:
