MyRoboPathOpen Robotics Lab
Arduino Uno #29 of 35
Est. 0.75 Hours
Budget: $4 - $8
Beginner

DC Motor PWM Speed & H-Bridge Direction Controller

Safely drive inductive DC motors with soft-start PWM ramps, flyback diode protection, and forward/reverse braking.

Never connect a DC motor directly to an Arduino pin! Learn why inductive motor windings require an H-bridge driver (L293D / L298N / transistor + 1N4007 flyback diode), wire a clean dual-rail power stage with common ground, and program smooth PWM speed ramping and directional reversals.

Arduino Uno R3DC Motor PWML293D / L298N H-BridgeFlyback ProtectionSoft-Start Ramp
DC Motor PWM Speed & H-Bridge Direction Controller Blueprint
DC Motor PWM Speed & H-Bridge Direction ControllerCAD Blueprint

Key Learning Objectives & Robotics Concepts

By building and programming this project, you will master the following core engineering skills:

1Understand inductive kickback (V = L·di/dt) and how flyback clamping diodes protect transistors and MCUs.
2Control DC motor direction via H-bridge IN1/IN2 truth tables (Forward, Reverse, Coast, Active Brake).
3Modulate rotor speed smoothly using 8-bit PWM duty cycle on ENA.
4Insert brief coasting dead-time before reversing direction to avoid huge shoot-through current surges.

Power Architecture & Voltage Staging

5V / 3.3V Regulated Power

Separate 6V battery pack (4×AA or 5V–9V bench supply) feeds the H-bridge motor power terminal (VCC2/12V), while Arduino 5V powers logic (VCC1). Both grounds meet at a solid common GND rail with a 100µF bulk decoupling cap. NEVER drive a DC motor straight from Arduino GPIO! H-bridge internal clamp diodes (or 1N4007 across motor) absorb lethal inductive back-EMF voltage spikes.

DC Motor PWM Speed & H-Bridge Direction Controller Power Architecture Schematic
Figure: Regulated Arduino Uno supply rails, current budget, and component protection.Power Schematic

Bill of Materials (BOM) & Hardware Components

Bill of Materials for DC Motor PWM Speed & H-Bridge Direction Controller
ComponentSpecifications & AlternativeQtyApprox Cost
Arduino Uno R3ATmega328P @ 16MHz1$8.00
L293D IC (or L298N / NPN+1N4007 Stage)Dual H-Bridge with internal clamp diodes1$2.00
3V–6V Brushed Hobby DC Motor + Fan Blade130-size brushed DC motor with propeller/tape flag1$1.50
4×AA Battery Holder (6V) + 100µF CapIsolated motor power rail1$2.00

Electrical & System Architecture

Connect Arduino D5 (~PWM) to L293D/L298N ENA, D6 to IN1, and D7 to IN2. Connect DC motor leads across OUT1 and OUT2, external 6V battery (+) to driver VM, and tie battery (-) directly to Arduino GND.

Figure: Complete Arduino Uno R3 pin-to-pin wiring schematic for DC Motor PWM Speed & H-Bridge Direction Controller.
Figure: Complete Arduino Uno R3 pin-to-pin wiring schematic for DC Motor PWM Speed & H-Bridge Direction Controller.Wiring Schematic

Autonomous Behavior & Finite State Machine (FSM)

Deterministic Arduino loop handling 5V–6V Brushed DC Motor + L293D / L298N Driver acquisition, filtering, and real-time output updates.

Figure: Firmware execution flow and state transitions for DC Motor PWM Speed & H-Bridge Direction Controller.
Figure: Firmware execution flow and state transitions for DC Motor PWM Speed & H-Bridge Direction Controller.Control Algorithm

Step-by-Step Physical Assembly & Wiring Tutorial

Follow each of the 5 sequential milestone stages with technical photos, wiring checks, and pre-flight tests.

1

Seat H-Bridge Driver & Verify Dual Power Rails

Duration: 10 Mins

Place the L293D across the breadboard center trench (notch left) or prepare your L298N module.

DC Motor PWM Speed & H-Bridge Direction Controller hardware layout diagram
Stage 1: 5V–6V Brushed DC Motor + L293D / L298N Driver hardware inspection & pinout overview.Stage 1 Diagram
Step Action Checklist:
  • For L293D: Pin 1=ENA(D5), Pin 2=IN1(D6), Pin 3=Motor+, Pin 4&5=GND, Pin 6=Motor-, Pin 7=IN2(D7), Pin 8=Battery 6V, Pin 16=Arduino 5V.
  • For L298N: remove the ENA jumper cap, wire ENA→D5, IN1→D6, IN2→D7, and connect motor to OUT1/OUT2.
  • Install a 100µF capacitor across the external 6V motor rail to suppress commutator brush noise.
Engineering & Tuning Tip: Using a single NPN transistor (2N2222 / TIP120) instead of an H-bridge? Wire 1kΩ base resistor to D5 and ALWAYS place a 1N4007 flyback diode cathode-to-5V across the motor!
2

Verify Mandatory Common Ground Link

Duration: 5 Mins

Ensure external battery negative (-), H-bridge GND, and Arduino Uno GND all meet cleanly.

DC Motor PWM Speed & H-Bridge Direction Controller breadboard wiring check diagram
Stage 2: Breadboard layout & Arduino Uno pin-to-pin wiring audit.Stage 2 Diagram
Step Action Checklist:
  • Connect 4×AA battery pack (-) to breadboard GND rail.
  • Connect Arduino Uno GND directly to that same GND rail.
  • Never connect 6V+ directly to Arduino 5V pin—keep motor (+) isolated on driver VM!
Engineering & Tuning Tip: No common ground = floating control signals = motor never spins!
3

Attach Visual Spin Flag & Check Stiction

Duration: 5 Mins

Press a fan blade or piece of tape onto the motor shaft so direction and speed are unmistakable.

DC Motor PWM Speed & H-Bridge Direction Controller power rail protection diagram
Stage 3: Supply rail check (External 6V (4×AA) Motor Rail + USB 5V Logic (Common GND)) and circuit protection verification.Stage 3 Diagram
Step Action Checklist:
  • Keep fingers, hair, and loose jumper wires clear of the spinning shaft.
  • Verify battery pack switch is OFF until wiring check is complete.
  • Connect USB cable to Arduino Uno.
Engineering & Tuning Tip: Small 130-size DC motors usually require PWM ≥75–90 (about 30% duty) to overcome static brush friction.
4

Upload Soft-Start H-Bridge Controller Firmware

Duration: 10 Mins

Flash the PWM ramp sketch, switch battery power ON, and watch smooth bidirectional spins.

DC Motor PWM Speed & H-Bridge Direction Controller serial telemetry verification diagram
Stage 4: Firmware upload & 115200 baud Serial Monitor verification.Stage 4 Diagram
Step Action Checklist:
  • Upload the production sketch below and open Serial Monitor at 115200 baud.
  • Turn on the 6V battery rail.
  • Watch the shaft ramp smoothly forward, coast to a gentle stop, then ramp cleanly in reverse!
Engineering & Tuning Tip: Ramping PWM smoothly prevents sudden gear shock and battery voltage sags on wheeled robots.
5

Test Active Brake vs. Coast Stopping

Duration: 5 Mins

Compare setting ENA=0 (free coast) vs IN1=HIGH, IN2=HIGH with ENA=255 (electromagnetic active braking).

DC Motor PWM Speed & H-Bridge Direction Controller live operational logic diagram
Stage 5: Live operational logic flow & calibration verification.Stage 5 Diagram
Step Action Checklist:
  • Observe how fast the fan stops under free coast vs shorting motor terminals through the H-bridge.
  • Check L293D / L298N temperature—it should stay cool to mildly warm.
  • Switch off the motor battery when done testing.
Engineering & Tuning Tip: Adding a 0.1µF ceramic capacitor directly across the two motor brass tabs drastically reduces RF brush noise.

Subsystem Benchtop Verification Scripts

Test each hardware subsystem independently before deploying the full autonomous firmware.

1Bench Diagnostic: 5V–6V Brushed DC Motor + L293D / L298N Driver Hardware Check

Subsystem Diagnostic

Minimal standalone sketch to verify pin wiring and signal response for 5V–6V Brushed DC Motor + L293D / L298N Driver before running the full project firmware.

Arduino IDE Diagnostic ScriptC++ / Arduino
// Diagnostic: Fixed 60% Forward Pulse Check
void setup() { pinMode(5,OUTPUT); pinMode(6,OUTPUT); pinMode(7,OUTPUT); }
void loop() {
  digitalWrite(6, HIGH); digitalWrite(7, LOW); analogWrite(5, 160); delay(1500);
  analogWrite(5, 0); delay(1500);
}
Expected Output: Motor spins steadily forward for 1.5s, coasts 1.5s repeatedly.

Complete Production Firmware

Full working C++ source code with pin mappings, non-blocking timers, and FSM avoidance routines.

arduino_uno_dc_motor_pwm_driver.ino
arduino_uno_dc_motor_pwm_driver.inoArduino C++
/**
 * Project 29: DC Motor Soft-Start PWM & H-Bridge Direction Controller
 * Board: Arduino Uno R3 | Driver (L293D/L298N): ENA=D5(PWM), IN1=D6, IN2=D7
 */
const uint8_t PIN_ENA = 5;
const uint8_t PIN_IN1 = 6;
const uint8_t PIN_IN2 = 7;

void setMotor(int speedSigned) {
  int pwm = constrain(abs(speedSigned), 0, 255);
  if (speedSigned > 0) {
    digitalWrite(PIN_IN1, HIGH);
    digitalWrite(PIN_IN2, LOW);
  } else if (speedSigned < 0) {
    digitalWrite(PIN_IN1, LOW);
    digitalWrite(PIN_IN2, HIGH);
  } else {
    digitalWrite(PIN_IN1, LOW);
    digitalWrite(PIN_IN2, LOW);
  }
  analogWrite(PIN_ENA, pwm);
}

void rampSpeed(int fromSpd, int toSpd, uint16_t stepDelayMs) {
  int step = (toSpd >= fromSpd) ? 5 : -5;
  for (int s = fromSpd; (step > 0 ? s <= toSpd : s >= toSpd); s += step) {
    setMotor(s);
    delay(stepDelayMs);
  }
  setMotor(toSpd);
}

void setup() {
  pinMode(PIN_ENA, OUTPUT);
  pinMode(PIN_IN1, OUTPUT);
  pinMode(PIN_IN2, OUTPUT);
  setMotor(0);
  Serial.begin(115200);
  Serial.println(F("DC Motor H-Bridge Controller Ready"));
}

void loop() {
  Serial.println(F("Ramping FORWARD -> 100%"));
  rampSpeed(80, 255, 25);
  delay(1200);

  Serial.println(F("Decelerating to STOP (dead-time)"));
  rampSpeed(255, 0, 15);
  delay(700);

  Serial.println(F("Ramping REVERSE -> 100%"));
  rampSpeed(-80, -255, 25);
  delay(1200);

  Serial.println(F("Decelerating to STOP"));
  rampSpeed(-255, 0, 15);
  delay(1000);
}

Firmware Architecture Explanation:

  • Provides a clean signed setMotor(-255..+255) abstraction matching real robotics motor drivers.
  • Ramps PWM gradually above stiction (80→255) to cut inrush current spikes cleanly.
  • Enforces a 700ms coast dead-time before reversing polarity to protect the H-bridge and gearbox.

Operational Verification & Pin State Matrix

1Double-check all 4 Arduino Uno pin connections against the wiring schematic before plugging in USB.
2Upload the Bench Diagnostic sketch first and confirm Serial Monitor at 115200 baud matches: Motor spins steadily forward for 1.5s, coasts 1.5s repeatedly.
3Upload the complete production sketch and verify every condition in the operational truth table below.

Input Signal vs. System Output Truth Table:

Operational truth table for DC Motor PWM Speed & H-Bridge Direction Controller
Input / StatePin / SignalDetected ConditionExpected Output
IN1=H, IN2=LENA = 80..255Forward DriveSpin CW Proportional to PWM
IN1=L, IN2=HENA = 80..255Reverse DriveSpin CCW Proportional to PWM
IN1=L, IN2=LENA = 0Neutral CoastMotor Free-Wheel Stop

Engineering Troubleshooting Matrix

Engineering troubleshooting matrix for DC Motor PWM Speed & H-Bridge Direction Controller
ProblemLikely CauseDiagnostic CheckEngineering Solution
Motor hums quietly at low speed without turningPWM below stiction threshold or weak batteriesTest PWM=220 and measure battery voltage under loadStart ramp near PWM=80–100 and use fresh 6V batteries.
Motor never moves at any speedMissing common GND or L298N ENA jumper still bridging 5VVerify battery (-) ties to Uno GND and check OUT1/OUT2 voltageTie grounds together and verify driver logic supply (VCC1 = 5V).
Arduino resets when motor startsAttempting to power motor from Uno 5V pin instead of external batteryCheck driver VM wireMove motor supply VM strictly to external 6V battery pack.

Engineering Challenges & Upgrades

Take your build to the next level with these progressive engineering upgrades:

Level 1Joystick Throttle & Reverse

Feed Project 26 Joystick Y-axis (-100..+100) straight into setMotor()!

Level 2Temperature-Controlled Smart Fan

Scale fan PWM automatically as Project 22 thermistor climbs from 26°C to 34°C.

Level 3IR Remote 3-Speed Desk Fan

Use Project 12 IR remote keys 1/2/3/0 to select Low, Medium, High, and Off speeds.