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.
Key Learning Objectives & Robotics Concepts
By building and programming this project, you will master the following core engineering skills:
Power Architecture & Voltage Staging
5V / 3.3V Regulated PowerSeparate 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.
Bill of Materials (BOM) & Hardware Components
| Component | Specifications & Alternative | Qty | Approx Cost |
|---|---|---|---|
| Arduino Uno R3 | ATmega328P @ 16MHz | 1 | $8.00 |
| L293D IC (or L298N / NPN+1N4007 Stage) | Dual H-Bridge with internal clamp diodes | 1 | $2.00 |
| 3V–6V Brushed Hobby DC Motor + Fan Blade | 130-size brushed DC motor with propeller/tape flag | 1 | $1.50 |
| 4×AA Battery Holder (6V) + 100µF Cap | Isolated motor power rail | 1 | $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.
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.
Step-by-Step Physical Assembly & Wiring Tutorial
Follow each of the 5 sequential milestone stages with technical photos, wiring checks, and pre-flight tests.
Seat H-Bridge Driver & Verify Dual Power Rails
Place the L293D across the breadboard center trench (notch left) or prepare your L298N module.
- 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.
Verify Mandatory Common Ground Link
Ensure external battery negative (-), H-bridge GND, and Arduino Uno GND all meet cleanly.
- 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!
Attach Visual Spin Flag & Check Stiction
Press a fan blade or piece of tape onto the motor shaft so direction and speed are unmistakable.
- 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.
Upload Soft-Start H-Bridge Controller Firmware
Flash the PWM ramp sketch, switch battery power ON, and watch smooth bidirectional spins.
- 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!
Test Active Brake vs. Coast Stopping
Compare setting ENA=0 (free coast) vs IN1=HIGH, IN2=HIGH with ENA=255 (electromagnetic active braking).
- 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.
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 DiagnosticMinimal standalone sketch to verify pin wiring and signal response for 5V–6V Brushed DC Motor + L293D / L298N Driver before running the full project firmware.
// 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);
}Complete Production Firmware
Full working C++ source code with pin mappings, non-blocking timers, and FSM avoidance routines.
/**
* 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
Input Signal vs. System Output Truth Table:
| Input / State | Pin / Signal | Detected Condition | Expected Output |
|---|---|---|---|
| IN1=H, IN2=L | ENA = 80..255 | Forward Drive | Spin CW Proportional to PWM |
| IN1=L, IN2=H | ENA = 80..255 | Reverse Drive | Spin CCW Proportional to PWM |
| IN1=L, IN2=L | ENA = 0 | Neutral Coast | Motor Free-Wheel Stop |
Engineering Troubleshooting Matrix
| Problem | Likely Cause | Diagnostic Check | Engineering Solution |
|---|---|---|---|
| Motor hums quietly at low speed without turning | PWM below stiction threshold or weak batteries | Test PWM=220 and measure battery voltage under load | Start ramp near PWM=80–100 and use fresh 6V batteries. |
| Motor never moves at any speed | Missing common GND or L298N ENA jumper still bridging 5V | Verify battery (-) ties to Uno GND and check OUT1/OUT2 voltage | Tie grounds together and verify driver logic supply (VCC1 = 5V). |
| Arduino resets when motor starts | Attempting to power motor from Uno 5V pin instead of external battery | Check driver VM wire | Move motor supply VM strictly to external 6V battery pack. |
Engineering Challenges & Upgrades
Take your build to the next level with these progressive engineering upgrades:
Feed Project 26 Joystick Y-axis (-100..+100) straight into setMotor()!
Scale fan PWM automatically as Project 22 thermistor climbs from 26°C to 34°C.
Use Project 12 IR remote keys 1/2/3/0 to select Low, Medium, High, and Off speeds.