Blue Pill - L298N - Stepper Motor

This is an electronic circuit note on how to interface Blue Pill (STM32F103C8T6) with L298 motor drive module to control a stepper motor like Nema 17. The electrical connection between Blue Bill, L298N and stepper motor (4 wires) is fairly simple (just like Arduino with L298N and stepper motor) and is shown in the circuit diagram below.  

Blue Pill - L298N - Stepper Motor - circuit diagram

The Blue Bill PA0, PA1, PA2, PA3, PA4 and PA5 pins are connected to the EN2, IN4, IN3, IN2, IN1 and EN1 pins of the L298N motor driver module. The 12V,5V and ground pin of the L298N are connected to the 12V,5V and ground pin of the circuit. The 4 wires of the stepper motor are connected to the OUT1 to OUT4 pins of the L298N motor driver pins.

So, this is basically a simple circuit that shows how to interface Blue Pill microcontroller board with L298N motor driver module. This is helpful if you want to add stepper motor control in your project with the STM32 Blue Pill projects. The Blue Pill microcontroller board is based on the STM32F103C8T6 microcontroller.

The following code was used. The code basically configures pins used and rotate the stepper motor clockwise and anti-clockwise direction.

#include <Arduino.h>

// Control pins
const int ENA = PA5;
const int IN1 = PA4;
const int IN2 = PA3;
const int IN3 = PA2;
const int IN4 = PA1;
const int ENB = PA0;

// Full-step sequence table
const int stepSequence[4][4] = {
  {HIGH, LOW,  HIGH, LOW},  // Step 1
  {LOW,  HIGH, HIGH, LOW},  // Step 2
  {LOW,  HIGH, LOW,  HIGH}, // Step 3
  {HIGH, LOW,  LOW,  HIGH}  // Step 4
};

void enableDriver(bool enable) {
  digitalWrite(ENA, enable ? HIGH : LOW);
  digitalWrite(ENB, enable ? HIGH : LOW);
}

void setStep(int step) {
  digitalWrite(IN1, stepSequence[step][0]);
  digitalWrite(IN2, stepSequence[step][1]);
  digitalWrite(IN3, stepSequence[step][2]);
  digitalWrite(IN4, stepSequence[step][3]);
}

/**
 * Rotates the stepper motor.
 * @param steps Number of steps to execute
 * @param delayMs Delay between steps in milliseconds (controls speed)
 * @param clockwise Set true for CW rotation, false for CCW rotation
 */
void stepMotor(int steps, int delayMs, bool clockwise) {
  enableDriver(true); // Power up the bridge

  for (int i = 0; i < steps; i++) {
    // Determine sequence index based on direction
    int stepIndex;
    if (clockwise) {
      stepIndex = i % 4;             // Forward sequence (0, 1, 2, 3)
    } else {
      stepIndex = (3 - (i % 4));     // Reverse sequence (3, 2, 1, 0)
    }

    setStep(stepIndex);
    delay(delayMs);
  }

  enableDriver(false); // Cut power when motion completes to save power & avoid heating
}

void setup() {
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);

  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);

  enableDriver(false);
}

void loop() {
  // Rotate 200 steps Clockwise
  stepMotor(200, 5, true);
  delay(1000);

  // Rotate 200 steps Counterclockwise
  stepMotor(200, 5, false);
  delay(1000);
}

This Arduino-based code drives a bipolar stepper motor using an L298N driver connected to an STM32 Blue Pill by mapping pins PA0–PA5 to control signals and bridge enables. It defines a 4-step full-step switching matrix (stepSequence) to energize the motor windings in specific phase order, while the stepMotor function iterates through these steps based on the target step count, step delay (speed), and desired direction (indexing 0→3 for clockwise or 3→0 for counterclockwise). To prevent overheating and eliminate idle power draw, enableDriver pulls ENA and ENB high only during motion and disables the H-bridges immediately after stepping completes. Finally, the main loop demonstrates this functionality by automatically executing 200 steps clockwise, pausing for one second, and then executing 200 steps counterclockwise in an infinite loop.

Watch the following video demonstration of how the Blue Pill - L298N - Stepper Motor circuit works.

Here we have direct connection between the Blue Pill microcontroller board and the L298N motor driver. But we can also use interfacing IC like PWM interfacing IC as illustrated in the electronics circuit note How to control DC motor using Arduino, PCA9685 and L298N.

Other Blue Pill tutorials are as follows:


Post a Comment

Previous Post Next Post