Unit C - Actuators & Displays

14. DC Motors & H-Bridges

Control motor direction, speed and stopping safely.

Estimated time 4 hours

Learning outcomes

  • Explain stall current, back EMF and why motors need a driver supply
  • Describe how an H-bridge reverses motor current
  • Use IN1/IN2 plus PWM enable for direction and speed
  • Stop briefly before reversing to reduce stress

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 Motors & H-Bridges instructional connection diagram

Brushed DC motor basics

A brushed DC motor has two leads. Apply voltage one way and it turns forward; reverse the polarity and it turns the other way. Higher average voltage generally means higher speed (under similar load).

When the shaft is stopped or just starting, current can be several times the normal running current (stall / start current). The windings are inductive and the brushes make electrical noise. Never drive a motor from an Arduino I/O pin - use a driver and an external supply sized for stall current, with common GND.

DC motor model showing polarity sets direction, plus a warning that stall current is several times running current
Polarity = direction. Size the supply and driver for stall current, not only running current.
IdeaWhy it matters
Running currentNormal spinning load
Stall / start currentMuch higher - can brown out a weak supply
Back EMFSpinning motor generates voltage; sudden reverse fights that energy
NoiseCan upset ADC / Serial - keep wiring tidy, share GND well

What an H-bridge does

An H-bridge is four switches arranged like an H around the motor. Turning on one diagonal pair sends current left-to-right (forward). The other diagonal pair sends current the other way (reverse).

Never turn on both switches in the same vertical leg at once - that shorts the motor supply (shoot-through). Modules such as L298N or TB6612 include drivers and protection so you control logic inputs instead of bare switches.

Two H-bridge diagrams showing diagonal switches ON for forward current and the opposite diagonal ON for reverse current
Forward and reverse use opposite diagonal pairs. Do not short a supply leg.

Module pins and truth table

This lesson uses a typical channel: IN1 and IN2 set direction, and ENA (or PWMA) sets speed with PWM. Exact labels differ by board - always check your module silkscreen and datasheet.

Flow from Uno D7 D8 D9 into H-bridge module IN1 IN2 ENA then out to a DC motor with external VM and common ground
Uno drives logic. Motor supply powers VM. GND is shared.
IN1IN2ENATypical result
HIGHLOWPWM / HIGHForward (speed from PWM)
LOWHIGHPWM / HIGHReverse (speed from PWM)
LOWLOWxCoast / stop (module dependent)
HIGHHIGHxBrake / stop (module dependent)

PWM speed control

With direction bits set, analogWrite on the enable pin changes the average motor voltage. A low duty cycle runs slower; a high duty cycle runs faster. The course sketch packs direction and magnitude into one signed value: positive = forward, negative = reverse, zero = stop.

Two PWM waveforms on the enable line showing low duty for slow speed and high duty for fast speed
Same direction inputs; only the ENA duty cycle changes speed.
void driveMotor(int speedValue) {
  speedValue = constrain(speedValue, -255, 255);
  digitalWrite(in1, speedValue > 0);
  digitalWrite(in2, speedValue < 0);
  analogWrite(pwmPin, abs(speedValue));
}

Stop before reversing

Reversing instantly while the motor is spinning hard forces a large current spike and mechanical shock. A short stop (coast or brake) lets speed fall before the opposite direction is commanded.

The worked sketch runs forward, pauses with driveMotor(0), then reverses - keep that habit in your own code.

Risky instant reverse contrasted with safer sequence of forward, brief stop, then reverse
Insert a brief stop between opposite directions.

Wiring and safe build sequence

Breadboard wiring for lesson 14: DC Motors & H-Bridges
Breadboard layout for this lesson. Match colours and pins before powering the circuit. Click the image for a larger view.
  1. D7 -> IN1
  2. D8 -> IN2
  3. D9/PWM -> ENA/PWMA
  4. Motor -> driver outputs
  5. Motor supply -> driver VM/GND
  6. Driver GND -> Uno GND
Power rule: switch off before moving wires. Arduino I/O pins are control signals; high-current loads require a driver and suitable external supply.
const 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

  1. Positive/negative signed speed combines direction and magnitude.
  2. A stop interval reduces mechanical and electrical stress.
  3. If your module needs ENA jumpered HIGH for full speed tests, still prefer PWM for controllable speed.

Test and record evidence

Expected result: The motor runs forward, stops, reverses, then stops.

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.
  • L298N voltage drop is large - a 6 V motor may need a higher VM than you expect; TB6612 wastes less.
Extension challenge: Use a potentiometer for signed speed: centre stop, left reverse, right forward.

Check your understanding

Q1. What does an H-bridge change?

Show answer

The direction of current through the motor.

Q2. Why is stall current important when choosing a supply?

Show answer

It can be several times running current and will brown out a weak pack.

Q3. What do IN1/IN2 usually set, and what does ENA PWM set?

Show answer

Direction; average speed/power.

Q4. Why stop before reversing?

Show answer

To reduce current spikes, mechanical shock and driver stress.