Try Embedded

Bipolar stepper with an L298N

Use both H-bridges of an L298N to drive a bipolar stepper motor with the Stepper library.

Motors · Intermediate

In a real product: Pan-tilt camera mount (Robotics): A bipolar stepper aims the camera precisely.

A bipolar stepper has two coils, and each must be driven both ways: that is exactly what an H-bridge does. The L298N's two bridges drive one coil each, and the Stepper library supplies the sequence.

What you'll learn

  • Why a bipolar stepper needs two H-bridges
  • The full-step sequence: both coils on, reversing one at a time
  • Using the Stepper library with a 4-wire driver

How it works

IN1–IN4 go to pins 8–11. OUT1/OUT2 (bridge A) drive coil A (A+, A−) and OUT3/OUT4 (bridge B) drive coil B. ENA and ENB are left unwired, as if their jumpers were on, so both bridges are always enabled.

Each step, the Stepper library flips one coil's direction. Seen as the four inputs, the sequence is 1010, 0110, 0101, 1001, and back to the start, which turns the rotor one step at a time. setSpeed(60) is 60 turns a minute; step(200) is one full turn of this 200-step motor.

The L298N wastes about 2 V as heat and runs the coils at full voltage all the time. For bigger steppers, a current-limiting driver such as the A4988 is better.

Try this

  • Halve the speed and double the steps: how long does a turn take now?
  • Count turns and print them.
  • Drive the same motor from an A4988 and compare the wiring.

sketch.ino

// A bipolar stepper through both bridges of an L298N (IN1-IN4 on pins 8-11): one turn each way.
#include <Stepper.h>

const int STEPS_PER_TURN = 200;
Stepper motor(STEPS_PER_TURN, 8, 9, 10, 11);

void setup() {
  motor.setSpeed(60);  // turns per minute
  Serial.begin(9600);
}

void loop() {
  Serial.println("One turn clockwise");
  motor.step(STEPS_PER_TURN);
  delay(500);
  Serial.println("One turn counter-clockwise");
  motor.step(-STEPS_PER_TURN);
  delay(500);
}

Lessons behind it