Skip to content
GEEKnodes

Electronics

Arduino Motors: How to Wire DC, Servo and Stepper Motors

How to control DC, servo and stepper motors with Arduino: L298N or TB6612FNG H-bridge, 50 Hz PWM, A4988 and DRV8825 drivers, and how to power it all.

Hooking a motor straight to an Arduino pin is the quickest way to reset the board or fry the pin. Each type of motor needs its own go-between: an H-bridge, a pulse signal or a driver. In this guide you’ll see the differences between DC, servo and stepper motors, which driver to use with each one and how to power everything without trouble.

In short

  • An Arduino Uno pin is rated for about 20 mA at most: no motor connects directly.
  • DC with an H-bridge (TB6612FNG beats the L298N), servo with a 50 Hz signal, stepper with an A4988 or DRV8825 type driver.
  • Motors run from a separate power supply with a common GND shared with the Arduino.

The three types of motor

DC motorServoStepper
What you controlSpeed and directionAngleExact position, step by step
Wires23 (power, GND, signal)4 on the usual bipolar ones
Go-betweenH-bridge or MOSFETNone for the signal; separate supply, yesDedicated driver
Position accuracyNone without a sensorGood within its rangeVery good if it doesn’t skip steps
Idle consumptionZero when stoppedHolds torque, draws currentHolds current, gets hot
Typical usesRobot wheels, fans, pumpsArms, rudders, flaps3D printers, CNC, platforms

Why they don’t connect directly to a pin

There are three reasons, and any one of them is enough:

  1. Current: a pin on the Uno’s ATmega328P has an absolute maximum of 40 mA, and it’s best not to go over 20 mA. A small DC motor draws hundreds of mA when it starts.
  2. Inductive voltage: when the current is cut off, the motor coil generates a voltage spike that can damage the pin. It’s the same problem as with relays.
  3. Noise: brushes and start-ups cause supply dips that reset the microcontroller.

If you only need one direction of rotation, a MOSFET with a diode is enough, as explained in the transistor as a switch. To change direction, you need an H-bridge.

DC motors: the H-bridge

An H-bridge is four switches arranged so that current can flow through the motor in one direction or the other. The Arduino only tells it the direction (two pins) and the speed (one PWM pin).

H-bridge with four switches: closing one diagonal makes current flow through the motor in one direction, and the other diagonal in the opposite one
Four switches in an H: each diagonal sends current through the motor in one direction

L298N vs TB6612FNG

L298NTB6612FNG
TechnologyBipolar transistorsMOSFET
Voltage dropHigh: you lose around 2 V or moreLow
Motor voltageUp to 46 VUp to 15 V
Current per channelUp to 2 A with a heat sink1.2 A continuous (3.2 A peaks)
HeatA lot; it has a heat sinkLittle
Module sizeLargeVery small

Why the drop matters: with an L298N powered at 6 V, the motor may only get about 4 V. You lose speed and torque, and the chip turns the rest into heat. For small 3 to 12 V motors, the TB6612FNG is almost always the better choice. The L298N makes sense with voltages or currents the TB6612FNG can’t handle.

TB6612FNG example

Wiring for one channel (A):

Arduino D5 (PWM) -> PWMA
Arduino D7       -> AIN1
Arduino D8       -> AIN2
Arduino 5V       -> VCC  (logic)
Motor supply +   -> VM
Common GND       -> GND
5V or pin        -> STBY (high to enable)
Motor            -> A01 and A02
const int PWMA = 5, AIN1 = 7, AIN2 = 8;

void setup() {
  pinMode(PWMA, OUTPUT);
  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, OUTPUT);
}

void motor(int velocidad) {  // -255 to 255
  digitalWrite(AIN1, velocidad > 0);
  digitalWrite(AIN2, velocidad < 0);
  analogWrite(PWMA, abs(velocidad));
}

void loop() {
  motor(200);  delay(2000);
  motor(0);    delay(500);
  motor(-200); delay(2000);
  motor(0);    delay(500);
}

Reversing direction at full speed causes heavy current spikes. Stop at 0 for a moment before reversing, as in the example.

Servos: 50 Hz pulses

A hobby servo contains a DC motor, a gearbox, a potentiometer and its own control circuit. You only send it a 50 Hz signal (one pulse every 20 ms) whose width sets the position:

Pulse widthUsual position
about 1 msOne end
about 1.5 msCenter
about 2 msOther end
50 Hz servo signal with one pulse every 20 ms: 1 ms one end, 1.5 ms center and 2 ms the other end
The width of the pulse, repeated every 20 ms, sets the servo angle

The exact ranges vary from model to model. The Servo library takes care of generating the signal; you’ll find the code and the PWM details in the guide to PWM on Arduino and ESP32.

What really matters here is power. The signal wire goes to the pin, but the servo’s positive lead shouldn’t go to the Arduino’s 5V unless it’s an unloaded micro servo. When moving or under load, a servo can draw peaks of several hundred mA, and with several of them, more than an amp. The symptoms of powering them from the board are classic: jitter, erratic movements and resets.

If you drive lots of servos, a PCA9685 module generates up to 16 signals over I2C and frees up the Arduino’s timers.

Continuous rotation servo

It looks like a servo, but it has no position: a 1.5 ms pulse stops it and the extremes set speed and direction. It’s a DC motor with a gearbox and built-in control.

Stepper motors

A stepper motor moves in fixed steps; the typical NEMA 17 does 200 steps per revolution (1.8° per step). Bipolar steppers have two coils and four wires. They don’t move by “switching on and off”: the coils have to be energized in sequence, and that’s what the driver does.

A4988 vs DRV8825

A4988DRV8825
Motor voltageUp to 35 VUp to 45 V
Current without heat sinkAbout 1 A per phaseAbout 1.5 A per phase
MicrosteppingUp to 1/16Up to 1/32
Usual current limit formulaI = Vref / (8 × Rs)I = Vref × 2 (modules with 0.1 Ω Rs)

Both use just two pins: STEP (each pulse is one step or microstep) and DIR (direction).

const int STEP = 3, DIR = 4;

void setup() {
  pinMode(STEP, OUTPUT);
  pinMode(DIR, OUTPUT);
}

void loop() {
  digitalWrite(DIR, HIGH);
  for (int i = 0; i < 200; i++) {   // one revolution in full-step mode
    digitalWrite(STEP, HIGH);
    delayMicroseconds(800);
    digitalWrite(STEP, LOW);
    delayMicroseconds(800);
  }
  delay(1000);
}

For smooth acceleration, the AccelStepper library keeps the motor from skipping steps when it starts.

Setting the current limit is mandatory

These drivers regulate the coil current, and the maximum is set with the module’s potentiometer by measuring Vref with a multimeter. Steps:

  1. Look up the motor’s rated current in its datasheet (for example, 1.5 A per phase).
  2. Identify the module’s sense resistors (Rs): they’re usually marked R050, R068 or R100 (0.05, 0.068 or 0.1 Ω).
  3. Calculate Vref with your driver’s formula and start somewhat below the maximum.
  4. With the driver powered, measure between the potentiometer and GND and adjust.

On an A4988 with 0.1 Ω Rs, for 1 A:

Vref = I × 8 × Rs = 1 × 8 × 0.1 = 0.8 V

The multimeter procedure is covered in detail in calibrating the drivers (Vref).

Separate power and common GND

The rule applies to all three types:

  • Motors are powered from their own supply, sized for the total current.
  • The Arduino can run from USB or another supply.
  • The GNDs are tied together. Without a common ground, the Arduino’s signal has no reference and the driver doesn’t understand it or behaves erratically.
  • Put an electrolytic capacitor (100 µF or more) between VM/VMOT and GND, close to the driver. It absorbs the spikes when you plug in.
Motor supply +    ->  driver VM / VMOT
Motor supply -    ->  driver GND  ->  Arduino GND
Arduino pins      ->  driver control inputs

Common mistakes

  • Powering motors from the Arduino’s 5V. Resets and hot regulators.
  • Forgetting the common GND. The motor doesn’t respond or moves in jerks.
  • Disconnecting a stepper while the driver is powered. The voltage spike can destroy the driver instantly.
  • Not setting Vref. Too low and it skips steps; too high and the motor and driver overheat.
  • Using an L298N at 5 or 6 V. The internal drop leaves the motor starved of voltage.
  • Reversing direction abruptly at full speed. Stop for an instant first.

Accessibility preferences

Quick profiles

Text and reading

Text size
Default
Line height
Default
Letter spacing
Default
Word spacing
Default

Colour

Motion and orientation

Cursor