← How-Tos
electronics May 7, 2026 ◑ 91 views ◯ 7 min read

How to Control Motors with Arduino and ESP32: Stepper, DC, and Servo Drivers

motorstepperdcservodrivera4988tmc2209drv8825escl298narduinoesp32

Introduction

Controlling motors is one of the most common tasks in robotics, CNC, 3D printing, and automation projects. Motors require more current than microcontroller pins can provide, so motor drivers are essential interface circuits that translate logic-level signals into power-level motor control. This guide covers the three main motor types — DC brushed, stepper, and servo — and their most popular driver modules. You will learn how to wire A4988 and TMC2209 stepper drivers, L298N and MOSFET DC motor controllers, and servo motor connections. Each section includes wiring diagrams (described), code examples for Arduino and ESP32, current limiting, heat management, and troubleshooting.

Part 1: Stepper Motors

Stepper motors move in discrete steps, providing precise position control without feedback. NEMA 17 is the most common size for 3D printers and CNC machines.

How Stepper Drivers Work

Stepper drivers convert step and direction signals into energized coil sequences. They also handle current limiting (chopping), microstepping, and thermal protection.

A4988 Driver

The A4988 is the classic entry-level stepper driver. Max current: 2A per coil (with heatsink and airflow).

Wiring

Microstepping Configuration

MS1MS2MS3Resolution
LOWLOWLOWFull step
HIGHLOWLOWHalf step
LOWHIGHLOWQuarter step
HIGHHIGHLOWEighth step
HIGHHIGHHIGHSixteenth step

Current Limiting

  1. Measure Vref (reference voltage) between the potentiometer center and GND
  2. Formula: Current limit = Vref × 2 (for A4988)
  3. Example: Vref = 0.7V → Current limit = 1.4A per coil
  4. Adjust pot with small screwdriver while measuring
  5. Always set below motor rated current and driver capacity

Arduino Code

// A4988 Basic Control
const int stepPin = 2;
const int dirPin = 3;

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

void loop() {
  digitalWrite(dirPin, HIGH);  // Set direction

  for(int i = 0; i < 200; i++) {  // 200 steps = 1 revolution (1.8° motor)
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1000);  // Step pulse width
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1000);  // Inter-step delay controls speed
  }

  delay(1000);
}

TMC2209 Driver

The TMC2209 is a silent, high-performance stepper driver with UART configuration, stall detection, and up to 2.8A current. It is the modern standard for 3D printers.

Key Advantages Over A4988

Wiring (Standalone Mode)

Current Setting

  1. TMC2209 current = Vref × 1.77 (RMS) or Vref × 2.5 (peak)
  2. Typical Vref for NEMA 17: 1.1-1.3V (RMS ~1.9A)
  3. Always heatsink the driver — TMC2209 runs hot at high current

UART Configuration (ESP32)

#include 

#define SERIAL_PORT Serial2  // ESP32 hardware serial
#define DRIVER_ADDRESS 0b00  // MS1=LOW, MS2=LOW

TMC2209Stepper driver(&SERIAL_PORT, DRIVER_ADDRESS);

void setup() {
  SERIAL_PORT.begin(115200);
  driver.begin();
  driver.toff(5);           // Enable driver
  driver.rms_current(1800); // Current in mA
  driver.microsteps(16);    // 16 microsteps
  driver.en_spreadCycle(false); // Use StealthChop (silent)
}

DRV8825 Driver

The DRV8825 handles higher current (2.5A) and voltage (8.2-45V) than the A4988. Pin-compatible but higher performance.

Part 2: DC Motors

DC motors spin continuously when powered. Speed is controlled by PWM (Pulse Width Modulation), direction by H-bridge polarity reversal.

L298N Dual H-Bridge

The L298N is the classic dual-channel DC motor driver. It handles 5-35V and up to 2A per channel (with heatsink).

Wiring

Control Logic

IN1IN2ENAResult
HIGHLOWPWMForward at PWM speed
LOWHIGHPWMReverse at PWM speed
LOWLOW-Stop (coast)
HIGHHIGH-Brake (short motor terminals)

Arduino Code

const int in1 = 8;
const int in2 = 9;
const int ena = 10;  // PWM pin

void setup() {
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(ena, OUTPUT);
}

void loop() {
  // Forward at half speed
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  analogWrite(ena, 128);  // 0-255
  delay(2000);

  // Stop
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  delay(1000);

  // Reverse at full speed
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  analogWrite(ena, 255);
  delay(2000);
}

MOSFET Motor Drivers (High Current)

For currents above 2A, use MOSFET-based drivers like BTS7960 (43A), VNH2SP30 (30A), or simple N-channel MOSFET modules.

Simple N-Channel MOSFET (Low-Side Switch)

Recommended MOSFETs: IRF3205, IRLZ44N (logic-level), AOD4184

ESC (Electronic Speed Controller) for Brushless Motors

Brushless DC (BLDC) motors require an ESC. Control is via servo-style PWM (1000-2000μs pulse width).

#include 
Servo esc;

void setup() {
  esc.attach(9);
  // Arm sequence
  esc.writeMicroseconds(1000);  // Minimum throttle
  delay(2000);  // Wait for ESC beep
}

void loop() {
  esc.writeMicroseconds(1500);  // 50% throttle
  delay(1000);
}

Part 3: Servo Motors

Servos are position-controlled motors with internal feedback. They rotate to a specific angle (0-180°) and hold it.

Wiring

Power note: Small 9g servos can run from Arduino 5V. Standard servos (MG995, DS3218) need external 5-6V power supply.

Arduino Code

#include 
Servo myServo;

void setup() {
  myServo.attach(9);  // PWM pin
}

void loop() {
  myServo.write(0);    // Full left
  delay(1000);
  myServo.write(90);   // Center
  delay(1000);
  myServo.write(180);  // Full right
  delay(1000);
}

ESP32 Servo Control

ESP32 uses the ESP32Servo library (different from Arduino Servo).

#include 
Servo myServo;
int servoPin = 18;

void setup() {
  myServo.attach(servoPin, 500, 2400);  // Pin, min us, max us
}

void loop() {
  myServo.write(90);
  delay(1000);
}

Power Supply Considerations

Heat Management

Troubleshooting

Motor Not Moving

Motor Vibrates But Does Not Spin

Driver Overheating / Shutting Down

L298N Motor Does Not Reach Full Speed

Pro Tips

Conclusion

Motor control bridges the gap between microcontroller logic and physical motion. Stepper drivers like the A4988 and TMC2209 provide precise position control for CNC and 3D printing. DC motor drivers like the L298N and MOSFET modules handle continuous rotation for robots and vehicles. Servos offer simple position control for arms, grippers, and camera gimbals. The keys to success are proper power supply sizing, current limiting, heat management, and common grounding between logic and motor power. With the right driver and wiring, any microcontroller can control powerful motors safely and precisely.