Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 37 ยท Sensors and Actuators

DC Motors and the H-Bridge

Drive DC motors from an Arduino safely: why pins cannot power a motor, transistor and flyback diode basics, the H-bridge for direction, and PWM speed control.

A small DC motor is the simplest actuator: apply voltage and it spins, reverse the voltage and it spins the other way, apply more voltage and it spins faster. Yet connecting one straight to an Arduino pin is one of the fastest ways to damage a board. Motors need far more current than a pin can give, and they push electrical noise back into the circuit when they stop.

Motors and boards cannot run here. The Arduino sketches are read-only; the C programs are labelled stand-ins that model the logic tables and the speed ramps.

Why a pin cannot drive a motor

An Arduino Uno pin is rated for about 20 mA continuous (40 mA absolute maximum). A tiny toy motor draws 100 to 300 mA running and several times that when starting or stalled. So you place a switch between the pin and the motor: a transistor (or a MOSFET) that is controlled by the pin but carries the motor current from a separate supply.

A typical low-side switch with an NPN transistor such as the 2N2222 or a logic-level MOSFET like the IRLZ44N:

  • Pin to the transistor base through about 1 kilohm (MOSFET gate: 100 to 220 ohms).
  • Motor between the supply positive and the collector (drain).
  • Emitter (source) to ground, and that ground connected to the Arduino ground.
  • A flyback diode (such as the 1N4001) across the motor, cathode toward the positive side. When the current stops, the motor's coil produces a voltage spike; the diode gives that current a safe path.

With this one-direction circuit, analogWrite() gives speed control with PWM (see the Analog and PWM chapter):

C++
const int MOTOR_PIN = 5;   // a PWM-capable pin

void setup() {
  pinMode(MOTOR_PIN, OUTPUT);
}

void loop() {
  for (int speed = 0; speed <= 255; speed += 5) {
    analogWrite(MOTOR_PIN, speed);
    delay(50);
  }
  for (int speed = 255; speed >= 0; speed -= 5) {
    analogWrite(MOTOR_PIN, speed);
    delay(50);
  }
}

PWM switches the motor on and off many times a second; the duty cycle (the fraction of time it is on) sets the average voltage. At very low duty the motor does not have enough torque to start moving at all, so the useful range often begins at about 20 to 30 percent.

The H-bridge: both directions

To reverse a motor you need to reverse the voltage across it. An H-bridge is four switches arranged like the letter H with the motor on the crossbar. Closing the top-left and bottom-right switches pushes current one way; closing top-right and bottom-left pushes it the other way. Closing both switches on the same side would short the supply (called "shoot-through"), so the controller must never allow that.

flowchart TB
  V["Supply +"] --> S1["Switch A (top left)"]
  V --> S2["Switch B (top right)"]
  S1 --> M["Motor"]
  M --> S2
  S1 --> S3["Switch C (bottom left)"]
  S2 --> S4["Switch D (bottom right)"]
  S3 --> G["Ground"]
  S4 --> G

Forward: A and D closed. Reverse: B and C closed. Never A with C, or B with D.

You rarely build one from loose parts. Integrated drivers handle the dangerous combinations for you: the L293D (600 mA per channel) and the L298N module (up to about 2 A) are classics; newer modules such as the TB6612FNG are more efficient. The common pin pattern is two direction inputs (IN1, IN2) plus one enable input (EN) that accepts PWM.

IN1 IN2 EN Result
HIGH LOW PWM forward, speed set by PWM
LOW HIGH PWM reverse
LOW LOW any coast or brake (driver dependent)
HIGH HIGH any brake (driver dependent)
C Stand-in: H-bridge truth table (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

A complete driver sketch for an L298N-style module:

C++
const int IN1 = 7;
const int IN2 = 8;
const int EN  = 9;   // PWM

void drive(int speed) {            // -255..255
  if (speed > 0) {
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, LOW);
  } else if (speed < 0) {
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, HIGH);
  } else {
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, LOW);
  }
  analogWrite(EN, abs(speed));
}

void setup() {
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(EN, OUTPUT);
}

void loop() {
  drive(200);  delay(2000);
  drive(0);    delay(500);        // pause before reversing
  drive(-200); delay(2000);
  drive(0);    delay(500);
}

The signed-speed drive() function is a good pattern: one number carries both direction and magnitude. Pausing at zero before reversing avoids a violent jolt and reduces current spikes.

Ramping the speed

Switching from 0 to full speed instantly gives a current spike and makes wheels slip. A ramp changes the speed a little at a time. The stand-in shows the calculation of how long a ramp takes and what the commands would be.

C Stand-in: speed ramp toward a target (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Power and other cautions

  • Use a separate motor supply when the motor draws more than a few tens of milliamps, and share ground with the Arduino.
  • L298N modules drop around 1.5 to 2 V internally; a 6 V battery gives you a 4 V motor.
  • Motors cause electrical noise that resets boards or garbles sensors. Solder a 100 nF ceramic capacitor across the motor terminals and keep motor wires away from signal wires.
  • Heat: drivers get hot under load. Check ratings and add a heatsink if needed.
  • Encoders (a disk with slots and an optical or magnetic sensor) let you measure rotation to build speed control loops; see stepper motors for a precise alternative that needs no feedback.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why shouldn't you power a motor directly from a digital pin?

    Motors draw hundreds of milliamps or more. A transistor or driver chip must carry the motor current while the pin only controls it.
  2. 2. What is the job of the flyback diode across a motor?

    The motor's coil releases stored energy as a voltage spike when switched off. The diode gives that current a safe path and protects the transistor.
  3. 3. Which H-bridge combination is dangerous (shoot-through)?

    Turning on both switches on one side connects the supply directly to ground. Driver chips prevent this.
  4. 4. What does analogWrite(EN, 128) set on a 0 to 255 scale?

    128 of 255 is about 50 percent, so the motor sees an average of roughly half the supply voltage.

Summary

  • Pins control, transistors or drivers carry the motor current from a separate supply with shared ground.
  • A flyback diode protects against switch-off spikes.
  • An H-bridge reverses direction using two inputs plus a PWM enable pin; never close both switches on one side.
  • Ramp speed changes and pause between direction changes to reduce stress.

Next: Stepper Motors