Building an STM32 Motor Controller with L298N and LCD Display

In this project, we develop a full motor control system using the STM32F103C8 "Blue Pill" microcontroller alongside the popular L298N motor driver. It’s designed for beginners and experienced makers alike, covering key aspects of robotics such as microcontroller design, motor management, and display integration.


Building an STM32 Motor Controller with L298N and LCD Display



1. Introduction: The Project

  • What it is: A motor control system driven by an STM32 microcontroller.
  • Use Cases: Robotics (e.g., a small rover or differential drive robot), automated conveyor systems, or any project requiring precise motor speed and direction control with status feedback via an LCD.

2. The Brain: STM32F103C8 (Cortex-M3)

At the heart of our system is the STM32F103C8, a powerful ARM Cortex-M3 microcontroller. Running at 72 MHz, it provides plenty of speed to handle motor control logic while simultaneously managing communication with our display. Its rich set of GPIO pins allows us to drive the L298N directly, and its flexibility lets us implement custom communication protocols like the software I2C we’re using here.

  • Core: ARM Cortex-M3, which provides a great balance of performance and power efficiency.
  • Key Specs:
    • 72 MHz clock speed.
    • Rich set of peripherals (Timers for PWM, I2C for the LCD, GPIOs for the motor driver).
    • Flash/RAM capacity suitable for complex control algorithms.
  • Why it's great for this: It's the "industry standard" for learning embedded C and ARM architecture.

3. The Muscle: L298N Motor Driver

To bridge the gap between our 3.3V microcontroller and our 12V motors, we use the L298N. This dual H-Bridge driver is a classic in the hobbyist world. It allows the STM32 to control not just the direction of our motors—by toggling logic pins—but also their speed, using PWM signals sent to the Enable pins.
  • Function: Acts as an interface between the low-power MCU and high-power motors.
  • Capabilities: Dual H-Bridge configuration allows it to control two DC motors independently, providing both speed (via PWM) and direction (via logic pins) control.
  • Integration: You'll explain how the STM32's PWM output pins connect to the L298N's ENA/ENB pins for speed control, and standard GPIOs to the IN1-IN4 pins for direction.

4. The Interface: I2C LCD

Finally, we have the user interface: a 16x2 LCD. While traditional LCDs require a massive bundle of wires, we’ve opted for an I2C version. By using just two wires—SDA and SCL—we keep our wiring clean and leave plenty of pins free for other sensors or expansion.
  • Why I2C: It dramatically simplifies wiring. Instead of using 6–10 pins (as with parallel mode), you only need two pins (SDA and SCL) plus power.
  • Function: It provides a human-readable interface to display real-time data like motor speed, status messages, or any other telemetry you choose to implement.

5. Programming


#include 
#include 

// ---------------- Pin definitions ----------------
// Motor A
#define ENA_PIN    15  // PB15 -> ENA
#define IN1_PIN    11  // PA11 -> IN1
#define IN2_PIN    10  // PA10 -> IN2
// Motor B
#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

// LCD (JHD-2X16-I2C in Proteus): 7-bit address 0x3E -> 0x7C as 8-bit write address
#define LCD_ADDR   0x7C

// Software I2C on PB6 (SCL) and PB7 (SDA), open-drain, needs external pull-ups
#define SCL_H()   (GPIOB->BSRR = (1U << 6))
#define SCL_L()   (GPIOB->BRR  = (1U << 6))
#define SDA_H()   (GPIOB->BSRR = (1U << 7))
#define SDA_L()   (GPIOB->BRR  = (1U << 7))
#define SDA_IN()  (GPIOB->IDR & (1U << 7))

volatile uint32_t lcd_nack_count = 0;   // watch this in the debugger: >0 means LCD did not ACK

// ---------------- Delay ----------------
void delay_ms(volatile uint32_t count) {
    for (count *= 200; count > 0; count--);
}

static void i2c_dly(void) {
    for (volatile int i = 0; i < 30; i++);
}

// ---------------- Software I2C ----------------
void I2C_Init(void) {
    RCC->APB2ENR |= RCC_APB2ENR_IOPBEN;
    GPIOB->CRL &= ~((0xFU << 24) | (0xFU << 28));
    GPIOB->CRL |=  ((0x6U << 24) | (0x6U << 28));  // general-purpose open-drain, 2 MHz
    SDA_H();
    SCL_H();
}

static void i2c_start(void) {
    SDA_H(); SCL_H(); i2c_dly();
    SDA_L(); i2c_dly();
    SCL_L();
}

static void i2c_stop(void) {
    SDA_L(); i2c_dly();
    SCL_H(); i2c_dly();
    SDA_H(); i2c_dly();
}

// returns 1 if the slave ACKed
static uint8_t i2c_write(uint8_t b) {
    for (int i = 0; i < 8; i++) {
        if (b & 0x80) SDA_H(); else SDA_L();
        i2c_dly();
        SCL_H(); i2c_dly();
        SCL_L();
        b <<= 1;
    }
    SDA_H();            // release SDA for ACK
    i2c_dly();
    SCL_H(); i2c_dly();
    uint8_t ack = !SDA_IN();
    SCL_L();
    return ack;
}

void I2C_WriteBytes(uint8_t addr, uint8_t *data, uint8_t len) {
    i2c_start();
    if (i2c_write(addr)) {
        for (uint8_t i = 0; i < len; i++) i2c_write(data[i]);
    } else {
        lcd_nack_count++;
    }
    i2c_stop();
}

// ---------------- LCD ----------------
void LCD_SendCommand(uint8_t cmd) {
    uint8_t buf[2] = {0x80, cmd};   // control byte: command
    I2C_WriteBytes(LCD_ADDR, buf, 2);
    delay_ms(2);
}

void LCD_SendData(uint8_t data) {
    uint8_t buf[2] = {0x40, data};  // control byte: data
    I2C_WriteBytes(LCD_ADDR, buf, 2);
    delay_ms(2);
}

void LCD_Init(void) {
    delay_ms(50);
    LCD_SendCommand(0x28); delay_ms(5);   // function set (same as DFRobot_LCD library)
    LCD_SendCommand(0x28); delay_ms(5);
    LCD_SendCommand(0x28);
    LCD_SendCommand(0x0C);                // display on, cursor off
    LCD_SendCommand(0x01); delay_ms(3);   // clear
    LCD_SendCommand(0x06);                // entry mode: left to right
}

void LCD_SetCursor(uint8_t col, uint8_t row) {
    uint8_t address = (row == 0) ? 0x00 : 0x40;
    LCD_SendCommand(0x80 | (address + col));
}

void LCD_Print(const char *str) {
    while (*str) LCD_SendData((uint8_t)*str++);
}

// ---------------- GPIO ----------------
void GPIO_Init(void) {
    RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN | RCC_APB2ENR_IOPCEN;

    // PA8..PA11 push-pull outputs, 2 MHz
    GPIOA->CRH &= ~((0xFU << 0) | (0xFU << 4) | (0xFU << 8) | (0xFU << 12));
    GPIOA->CRH |=  ((0x2U << 0) | (0x2U << 4) | (0x2U << 8) | (0x2U << 12));

    // PB14, PB15 push-pull outputs, 2 MHz
    GPIOB->CRH &= ~((0xFU << 24) | (0xFU << 28));
    GPIOB->CRH |=  ((0x2U << 24) | (0x2U << 28));

    // PC13 push-pull output, 2 MHz
    GPIOC->CRH &= ~(0xFU << 20);
    GPIOC->CRH |=  (0x2U << 20);
}

// ---------------- Main ----------------
int main(void) {
    GPIO_Init();

    // Enable motor drivers first so motors work even if the LCD has a problem
    GPIOB->BSRR = (1U << ENA_PIN) | (1U << ENB_PIN);

    I2C_Init();
    LCD_Init();

    LCD_SetCursor(0, 0);
    LCD_Print("STM32 Motor Ctrl");

    while (1) {
        // 1. Forward
        LCD_SetCursor(0, 1);
        LCD_Print("Status: FORWARD ");
        GPIOA->BSRR = (1U << IN1_PIN) | (1U << IN3_PIN);
        GPIOA->BRR  = (1U << IN2_PIN) | (1U << IN4_PIN);
        GPIOC->ODR ^= (1U << LED_PIN);
        delay_ms(2000);

        // 2. Reverse
        LCD_SetCursor(0, 1);
        LCD_Print("Status: REVERSE ");
        GPIOA->BRR  = (1U << IN1_PIN) | (1U << IN3_PIN);
        GPIOA->BSRR = (1U << IN2_PIN) | (1U << IN4_PIN);
        GPIOC->ODR ^= (1U << LED_PIN);
        delay_ms(2000);

        // 3. Turn
        LCD_SetCursor(0, 1);
        LCD_Print("Status: TURN    ");
        GPIOA->BRR  = (1U << IN1_PIN) | (1U << IN4_PIN);
        GPIOA->BSRR = (1U << IN2_PIN) | (1U << IN3_PIN);
        GPIOC->ODR ^= (1U << LED_PIN);
        delay_ms(1000);

        // 4. Stop
        LCD_SetCursor(0, 1);
        LCD_Print("Status: STOPPED ");
        GPIOA->BRR = (1U << IN1_PIN) | (1U << IN2_PIN) | (1U << IN3_PIN) | (1U << IN4_PIN);
        GPIOC->ODR ^= (1U << LED_PIN);
        delay_ms(1000);
    }
}

6. Video Demonstration




Related:

Post a Comment

Previous Post Next Post