14. DC Motors & H-Bridges
Control motor direction, speed and stopping safely.
Learning outcomes
- Explain back EMF and motor starting current
- Use an H-bridge truth table
- Control direction and PWM speed
- Sequence direction changes safely
Parts and preparation
Uno, L298N/TB6612-style module, low-voltage DC motor and suitable motor supply.
Before power: inspect wiring, confirm supply voltage and ensure all connected circuits share GND.
DC motor
Applied voltage produces torque and rotation. Starting/stall current can be several times running current. The motor is inductive and generates electrical noise.
H-bridge
Four switches reverse current through the motor. Driver inputs select forward, reverse, coast or brake; consult the exact module truth table.
Speed
PWM on an enable/PWM input controls average motor voltage. Direction should not reverse instantly at high speedstop briefly first.
Wiring and safe build sequence

- D7 -> IN1
- D8 -> IN2
- D9/PWM -> ENA/PWMA
- Motor -> driver outputs
- Motor supply -> driver VM/GND
- Driver GND -> Uno GND
Worked sketch
Download .ino sketchconst byte in1 = 7;
const byte in2 = 8;
const byte pwmPin = 9;
void driveMotor(int speedValue) {
speedValue = constrain(speedValue, -255, 255);
digitalWrite(in1, speedValue > 0);
digitalWrite(in2, speedValue < 0);
analogWrite(pwmPin, abs(speedValue));
}
void setup() {
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(pwmPin, OUTPUT);
}
void loop() {
driveMotor(180);
delay(1500);
driveMotor(0);
delay(500);
driveMotor(-180);
delay(1500);
driveMotor(0);
delay(1000);
}How the code works
- Positive/negative signed speed combines direction and magnitude.
- A stop interval reduces mechanical and electrical stress.
Test and record evidence
Practical evidence checklist
Common faults and checks
- Use a motor supply able to provide stall current.
- Never power the motor from an Arduino I/O or 5 V pin.
- If only one direction works, inspect both logic inputs.
Check your understanding
Q1. What does an H-bridge change?
Show answer
The direction of current through the motor.
Q2. Why stop before reversing?
Show answer
To reduce current, mechanical shock and driver stress.