|
Arduino Motor Driver Complete Guide: A4988 Stepper Motor + L298N DC Motor Practice

Arduino Motor Driver Complete Guide: A4988 Stepper Motor + L298N DC Motor Practice

Introduction

In Arduino projects, motors are one of the most commonly used actuators. Whether it’s 3D printers, CNC engravers, or self-balancing robots, none can work without motor drivers. Arduino itself can only output weak digital signals (maximum 40mA) and cannot directly drive motors, so dedicated driver modules are needed to amplify current and control direction.

This guide will introduce two of the most commonly used motor driving solutions: A4988 driving stepper motors (precise position control) and L298N driving DC motors (speed control). Through this article, you will master the complete process from wiring, code to debugging.


Part 1: A4988 Driving Stepper Motor

Required Materials

NameQuantity
Arduino Uno1
A4988 stepper motor driver module1
42 stepper motor (NEMA 17)1
Breadboard1
9V power supply1
Jumper wiresSeveral

A4988 Module Introduction

A4988 is a microstepping stepper motor driver with built-in overcurrent protection. It can drive bipolar stepper motors, supporting up to 35V voltage and 2A/phase current. The module has a built-in potentiometer to adjust output current to prevent motor overheating.

Microstepping Configuration

The biggest highlight of A4988 is microstepping support. Through the combination of high/low levels on MS1, MS2, MS3 three pins, different stepping precision can be set:

ModeMS1MS2MS3Steps per Revolution
Full stepLOWLOWLOW200
1/2 stepHIGHLOWLOW400
1/4 stepLOWHIGHLOW800
1/8 stepHIGHHIGHLOW1600
1/16 stepHIGHHIGHHIGH3200

Taking a common 1.8° stepper motor as an example: full step mode requires 200 steps per revolution, while 1/16 microstep mode requires 3200 steps - precision improved by 16 times!

Wiring Diagram

Pin Description

A4988 PinArduino ConnectionDescription
EN (Enable)Digital pin 6Active LOW, driver works when pulled low
STEPDigital pin 5One pulse per step
DIR (Direction)Digital pin 4HIGH for forward, LOW for reverse
VMOTPower positive8V ~ 35V power supply
GNDArduino GNDCommon ground

Complete Code

// Define pins
int stepPin = 5;
int dirPin = 4;
int enPin = 6;

void setup() {
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(enPin, OUTPUT);

  digitalWrite(enPin, LOW);  // Enable driver
  digitalWrite(dirPin, HIGH); // Set direction
}

void loop() {
  // Send 200 pulses, stepper motor rotates one revolution (full step mode)
  for (int i = 0; i < 200; i++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(500);  // Pulse high time
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);  // Pulse low time
  }

  delay(1000);  // Pause 1 second

  // Reverse direction
  digitalWrite(dirPin, LOW);

  // Rotate one more revolution
  for (int i = 0; i < 200; i++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);
  }

  delay(1000);

  // Restore direction
  digitalWrite(dirPin, HIGH);
}

Key Notes

  • Pulse timing: The value of delayMicroseconds() determines rotation speed. The smaller the value, the faster the speed, but it cannot be too small causing the motor to lose steps (generally not lower than 100μs).
  • Current adjustment: When using for the first time, use a multimeter to measure the potentiometer, adjust the reference voltage to ensure it doesn’t exceed the motor’s rated current.
  • Heat dissipation: A4988 is easy to heat up when driving large currents, it’s recommended to install a heatsink.

Part 2: L298N Driving DC Motor

Required Materials

NameQuantity
Arduino Uno1
L298N motor driver module1
DC gear motor2
9V battery holder1
USB cable1
Jumper wiresSeveral

L298N Module Introduction

L298N is a dual H-bridge motor driver chip that can control two DC motors (or one stepper motor) simultaneously. It supports up to 46V voltage and 2A current output, with built-in flyback diodes for easy use.

Module NameDual H-bridge motor driver moduleOperating ModeH-bridge drive (dual channel)
Operating Voltage5V ~ 35VOperating Current2A (peak 3A)
Logic Voltage5V ~ 7VMaximum Power20W
Drive TypeDual H-bridgeOperating Temperature-20℃ ~ +135℃

Pin Description

PinDescription
12V InputConnect to external power positive (7V-12V)
GNDPower negative, common ground with Arduino
5V OutputCan be used as 5V power supply for Arduino
ENA / ENBEnable jumpers, control channel A/B (remove for PWM speed control)
IN1 / IN2Channel A direction control
IN3 / IN4Channel B direction control
OUT1 / OUT2Connect to motor A
OUT3 / OUT4Connect to motor B

Wiring Diagram

Key Notes

  • Power supply: DC motors need 7V-12V external power supply. Arduino’s USB 5V current is too small to drive motors, must use independent power supply.
  • PWM speed control: ENA and ENB jumpers are inserted by default, at this time motors run at full speed. For speed control, remove jumpers and connect ENA/ENB to Arduino’s PWM pins (5, 6, 9, 10).

Complete Code

// Motor A: ENA connected to pin 5, IN1 connected to pin 7, IN2 connected to pin 8
// Motor B: ENB connected to pin 6, IN3 connected to pin 9, IN4 connected to pin 10

int enA = 5;
int in1 = 7;
int in2 = 8;
int enB = 6;
int in3 = 9;
int in4 = 10;

void setup() {
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  // Set initial speed (PWM value 0-255)
  analogWrite(enA, 150);  // Motor A medium speed
  analogWrite(enB, 200);  // Motor B faster speed
}

void loop() {
  // Motor A forward, Motor B forward
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  delay(2000);

  // Motor A reverse, Motor B reverse
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  delay(2000);

  // Motor A stop, Motor B stop
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  delay(2000);
}

Part 3: A4988 vs L298N Comparison

FeatureA4988L298N
Motor TypeStepper motorDC motor
PrecisionHigh (microstepping)Low (switch control)
Speed Control MethodPulse frequencyPWM duty cycle
Typical Applications3D printers, CNCCars, robots
Drive Current2A/phase2A/bridge
Encoder Feedback RequiredNo (open-loop control)No (no position feedback)

Simply put: Choose A4988 + stepper motor for precise positioning, choose L298N + DC motor for high-speed rotation.


Common Troubleshooting

Stepper Motor Not Rotating

  • Check pulse timing: Is delayMicroseconds() value reasonable (recommend 300-1000μs for testing)
  • Check current setting: Use potentiometer to adjust A4988’s reference voltage, ensure motor has enough current
  • Check wiring: Confirm STEP, DIR, EN three pins are connected correctly, EN pin needs to be pulled low

DC Motor Not Rotating

  • Check supply voltage: Ensure external power is between 7V-12V, USB power is not enough
  • Check H-bridge wiring: Confirm IN1-IN4 are connected correctly to Arduino, OUT is connected correctly to motor
  • Check jumpers: Are ENA/ENB jumpers inserted (full speed) or connected to PWM pins

Module Overheating

  • Install heatsink: Both A4988 and L298N will heat up under high current, recommend adding heatsinks
  • Lower current: Adjust A4988 potentiometer to reduce output current
  • Check voltage: Ensure supply voltage is within module range (A4988: 35V, L298N: 46V)

Summary

Arduino motor driving is not complicated. Remember two core modules: A4988 for stepper motors (precise position control), L298N for DC motors (simple speed control). After mastering wiring and code, you can freely control various motors. Give it a try!