Learning Goals 5 min
One transistor gives you on / off. To reverse a motor you'd have to physically swap wires — silly for a robot. The H-bridge solves this with four transistors per motor, arranged so software can choose direction, speed, brake, or coast. By the end of this lesson you will:
- Explain the four states of an H-bridge — forward, reverse, brake, coast — using the "close two switches" mental model.
- Wire the L298N module to a UNO and a 4 × AA pack with the correct power-rail discipline: VS from the battery, GND shared, VCC/5V either jumpered or supplied from the Arduino.
- Drive one motor forward, reverse, brake and coast using
digitalWriteon IN1 / IN2, controlled by serial commands typed into the Serial Monitor.
Warm-Up 10 min
Pull out your L298N module — the bright red PCB with a big silver heatsink in the middle. Identify these connectors and pins on the board:
- Big screw terminal block (one side): three terminals labelled
+12V(orVS),GND,+5V. - Two more screw terminal blocks (the other two corners):
OUT1 / OUT2andOUT3 / OUT4. These go to your two motors. - Header pins:
ENA,IN1,IN2,IN3,IN4,ENB— these are the control inputs from the Arduino. - Two jumpers: one labelled "5V enable", one across each of the
ENA/ENBpins. We'll discuss both in §3.
The big question
Why does the screw terminal have three pins — VS, GND, +5V — when most modules have only two (power + and power −)?
Reveal
The L298N is two boards in one:
- A high-power motor side that needs the full battery voltage (VS) — 6 V to 35 V depending on your motor.
- A low-power logic side that runs the chip's internal control circuitry on 5 V.
The +5V terminal is bidirectional: with the "5V enable" jumper installed, the board generates 5 V from VS (via an on-board regulator) and outputs it on the +5V pin — handy if you want to power your Arduino from the same battery. Without the jumper, you must supply 5 V into that pin from the Arduino. Both wiring styles are valid.
New Concept · Four switches, four states 25 min
The H-bridge mental model
Imagine the motor as a vertical bar in the middle of an H. Four switches sit at the corners of the H: top-left (S1), top-right (S2), bottom-left (S3), bottom-right (S4). Each top switch connects its corner to the battery+; each bottom switch connects its corner to GND.
| State | Closed switches | What the motor sees |
|---|---|---|
| Forward | S1 (top-left) + S4 (bottom-right) | Battery+ on left lead, GND on right — motor spins one way. |
| Reverse | S2 (top-right) + S3 (bottom-left) | Battery+ on right lead, GND on left — motor spins the other way. |
| Brake | S3 + S4 (both bottoms) | Both motor leads shorted to GND. Back-EMF dumps into a short → strong braking. |
| Coast | None (or both tops only) | Motor disconnected. Spins down slowly under its own friction. |
| FORBIDDEN — "shoot-through" | S1 + S3 (or S2 + S4) | Both transistors in one column on → direct short across the battery. Smoke. |
The L298N chip enforces that you can never accidentally close S1 + S3 at the same time, because IN1 controls the pair S1/S3 and IN2 controls S2/S4 — only one switch per column can be closed.
The four control combinations on the L298N
| IN1 | IN2 | ENA | Result for Motor A |
|---|---|---|---|
| HIGH | LOW | HIGH | Forward at full speed |
| LOW | HIGH | HIGH | Reverse at full speed |
| HIGH | HIGH | HIGH | Brake (both outputs HIGH = both shorted to VS) |
| LOW | LOW | HIGH | Brake (both outputs LOW = both shorted to GND) |
| anything | anything | LOW | Coast (outputs floating) |
So ENA is the gate: when low, the motor is disconnected regardless of IN1/IN2. When high, IN1/IN2 set direction. PWM on ENA gives speed control — that's L03-09.
The jumpers explained
- 5V enable jumper: ON if your battery VS is ≤ 12 V. The on-board regulator generates 5 V from VS for the chip's logic. You can also use that 5 V to power your Arduino if you want a single-supply robot. OFF if VS is > 12 V — the regulator overheats above 12 V.
- ENA / ENB jumpers: shorting these jumpers ties the enable pin permanently HIGH — useful if you don't need PWM speed control and want to free up an Arduino pin. Remove them when you want to drive ENA / ENB from the Arduino for PWM.
Wiring diagram (in words)
IN1 and IN2 choose the direction, ENA decides whether anything happens at all, and the truth table above is the complete list of what those two pins can mean.- Battery + → L298N
VS(or+12V) screw terminal. - Battery − → L298N
GNDscrew terminal. - L298N
GND→ ArduinoGND(common ground!). - Either: L298N
+5Vscrew → Arduino5V(board powers Arduino), OR: 5V enable jumper OFF, Arduino5V→ L298N+5V(Arduino powers L298N logic). Don't do both at once. - Arduino D9 → L298N
IN1. - Arduino D8 → L298N
IN2. - L298N
ENA: jumper installed for now (always-on). - Motor leads → L298N
OUT1+OUT2.
Worked Example · Forward / reverse / brake / coast from serial 25 min
Step 1 — wire it
Follow the wiring list from §3. For this lesson, set the "5V enable" jumper to ON (board generates 5V) and leave the ENA jumper installed (motor always enabled when IN pins call for it). One motor on OUT1/OUT2.
Step 2 — the sketch
// L03-08 · L298N H-bridge — forward/reverse/brake/coast from serial.
// f = forward, b = back, s = brake, c = coast. + Enter to send.
const int IN1 = 9;
const int IN2 = 8;
void forward() { digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
}
void reverse() { digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
}
void brake() { digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
}
// "coast" requires releasing ENA. With ENA jumpered HIGH (this lesson),
// we approximate coast with brake — the motor stops, just more abruptly.
// L03-09 wires ENA to a PWM pin and gives us a real coast.
void coast() { brake();
}
void setup() {
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
brake();
Serial.begin(9600);
Serial.println("# H-bridge ready. f/b/s/c + Enter.");
}
void loop() {
if (Serial.available() == 0) return;
char cmd = Serial.read();
switch (cmd) {
case 'f': case 'F': forward();
Serial.println(">> FORWARD");
break;
case 'b': case 'B': reverse();
Serial.println(">> REVERSE");
break;
case 's': case 'S': brake();
Serial.println(">> BRAKE");
break;
case 'c': case 'C': coast();
Serial.println(">> COAST");
break;
default: return;
}
}Step 3 — upload and try every command
Power the L298N from the battery pack. Open Serial Monitor at 9600 baud, line ending "Newline". Type:
f→ motor spins one ways→ motor stops abruptlyb→ motor spins the other ways→ stops
If the motor spins the "wrong" way on f, swap the two motor leads at OUT1/OUT2. Convention done — "forward" should mean "the direction you call forward".
Step 4 — feel the brake vs the coast (preview)
With the ENA jumper installed we can't really coast — the chip will always actively drive both outputs. To see the difference, momentarily lift the ENA jumper while the motor is spinning. You should feel the motor go from "hard hold" (brake) to "spins down freely" (coast). Put the jumper back. (We get proper PWM-controlled enable in L03-09.)
Step 5 — the "both motors" preview
Wire the second motor to OUT3/OUT4, and the second pair of control pins to D7 (IN3) and D6 (IN4). Add equivalent functions for Motor B. Now you have a 2-motor driver — you're ready for the chassis in L03-10.
Step 6 — sanity-check the heatsink temperature
After a few minutes of driving, touch the silver heatsink — gently. It should feel warm, not hot. The L298N is an old chip and dissipates ~1.5 V per output transistor times the motor current — for a 1 A motor that's 3 W of heat. If it's painful to touch, you're drawing too much current and either need a bigger motor driver or a smaller motor.
Basic 5 min
Goal: Make the motor automatically alternate every 2 seconds: forward, brake, reverse, brake, repeat. No user input.
1. Think first (on paper, no typing yet):
- Which helper functions do you already have from the Worked Example?
- What happens between forward and reverse in the diagram — and why is it there?
- Write
loop()as plain-English steps, one per line.
2. Code it. Start from the Worked Example sketch and remove the Serial command handler. Rule: loop() calls only your helpers and delay() — no digitalWrite.
3. Upload and observe. Watch one whole cycle and compare with the diagram.
It works if:
- One full cycle takes 5 s and matches the diagram.
- The motor never snaps straight from forward into reverse.
Challenge 1 5 min
Goal: Convert to non-blocking timing (L02-35 / L03-03 pattern). Same behaviour, but other things can run in parallel.
The motor must do exactly what the Basic diagram shows. To prove nothing blocks, add an LED on D13 that toggles every 250 ms — it must never pause.
1. Think first (on paper, no typing yet):
- How many different phases does the pattern have? Give each one a name.
- What two things must the sketch remember between passes of
loop()? - When a phase's time is up, what two things happen?
2. Code it. Start from your Basic sketch. Rule: no delay() anywhere in loop().
3. Upload and observe. Watch the motor and the LED together for a few cycles.
It works if:
- The motor pattern is the same 5 s cycle as the Basic task.
- The D13 LED flashes steadily the whole time, even while the motor changes direction.
loop()contains nodelay().
Challenge 2 5 min
Goal: Two motors, two buttons. Button A on D2 selects Motor A; Button B on D3 selects Motor B. The Serial monitor commands (f / b / s) only affect the currently-selected motor.
1. Think first (on paper, no typing yet):
- What must the sketch remember between key presses?
- Your direction functions drive one fixed pair of pins. What would they need to be told so they can drive either motor?
- Which pins does Motor B use?
2. Code it. Motor B on OUT3/OUT4 with IN3 on D7 and IN4 on D6. Buttons on D2 and D3 to GND with INPUT_PULLUP. Start from the Worked Example sketch.
3. Upload and observe. Select A, send f; select B, send b; send s.
It works if:
- After pressing A,
f/b/smove only Motor A. - After pressing B, they move only Motor B — Motor A keeps doing whatever it was doing.
- The selection stays put until the other button is pressed.
Challenge 3 · Write a small motor helper file 10 min
The four direction functions (forward, reverse, brake, coast) will be used in every Cluster B sketch. Refactor them into a reusable header next to your servo-config.h from L03-02.
Goal: a file called motor.h that any sketch can include. It describes a motor as a Motor struct holding its two pin numbers (in1, in2), and gives four helpers that each take a motor: motorInit, motorForward, motorReverse and motorBrake. Your main sketch then declares Motor A on pins 9 and 8, and Motor B on pins 7 and 6.
1. Think first (on paper, no typing yet):
- Your Worked Example functions use
IN1andIN2directly, so they only work for one motor. What would each function need to be told so it works for either motor? - What does
motorInithave to do thatsetup()did before? Write it in plain English. - A header is not an
.inofile, so it does not getpinModeanddigitalWritefor free. Which file would you include at the top ofmotor.hto get them? And which three lines from L03-02 stop the file being included twice?
2. Code it. Add a new tab called motor.h. Rule: motor.h must not mention any pin number — the pins live only in your main sketch. Function bodies written inside a header need the word inline in front of them. Then include motor.h in your main sketch, declare motorA and motorB, and call motorInit on both in setup().
3. Upload and observe. Rewrite your Serial f / b / s handler to use the new helpers on Motor A, then on Motor B.
It works if:
- The sketch compiles, and
f,bandsbehave exactly as they did in the Worked Example. - Switching from Motor A to Motor B changes only the motor you pass in — not the helpers.
- No pin number appears anywhere inside
motor.h.
Questions:
- What error did you get if you left out the Arduino include in the header? ____
- Why does it make sense for the struct to be passed in as
const? ____
Now any sketch that needs a motor just #include "motor.h", declares its motor as a Motor struct with pin numbers, and uses the friendly helpers. We'll add a motorSpeed(m, duty) helper in L03-09 (PWM control), and finally a full Motor class in L03-41.
Recap 5 min
The H-bridge is the foundation of every powered robot you'll ever build. Four transistors per motor let software choose direction, brake, or coast. The L298N module is the classroom standard — a chip + heatsink on a red PCB with screw terminals for power and motors and header pins for control. Two pins per motor (INx) set direction; one enable pin (ENx) gates it on / off (and later, takes a PWM signal for speed). The mandatory rules: common ground; one source of 5 V logic, not two; don't exceed the chip's 2 A continuous limit. Tomorrow we add PWM to ENA and unlock continuous speed control — the L03-09 lesson is what makes a robot feel smooth instead of switched.
- H-bridge
- A circuit of four transistors arranged in an "H" with the motor in the middle bar. Lets software switch the motor between forward, reverse, brake, and coast.
- L298N
- A classic dual H-bridge IC (two motors per chip). Cheap, robust, but old: 1.5–2 V drop per transistor, no current sensing, big heatsink needed.
- VS (motor supply)
- The voltage rail that powers the motors. Comes from a battery, separate from the Arduino's logic supply.
- VCC / +5V (logic supply)
- The voltage that powers the chip's internal logic — always 5 V for the L298N. Can be supplied by the board itself (5V enable jumper) or by the Arduino.
- Common ground
- The Arduino's GND and the motor pack's − must be connected to each other. Without it, the IN pin signals from the Arduino have no shared reference at the L298N.
- Enable pin (ENA / ENB)
- The "master switch" for each motor. When low, the motor is disconnected; when high (or PWM'd), the IN pins control direction (and speed).
- Brake vs coast
- Brake = both motor leads shorted to the same rail; back-EMF causes rapid deceleration. Coast = motor disconnected; spins down under friction alone.
- Shoot-through
- A failure mode where the top and bottom transistor in the same H-bridge column conduct simultaneously, shorting the battery. The L298N's logic prevents this by design — but home-made H-bridges from discrete transistors must be designed carefully to avoid it.
Extra Mission 5 min
Part 1 — Design a two-way motor gadget
An H-bridge lets a motor go both ways, brake or coast. Design a gadget that needs all of that. Ideas: a toy drawbridge, a curtain opener, or a tiny winch that lifts a bucket.
Your design must include:
- The gadget's job, in one sentence.
- A table of moments: for each one, write the H-bridge state (forward, reverse, brake or coast) and why.
- The IN1 and IN2 levels for each state.
- What starts each move: a Serial Monitor command, a button or a timer.
- A wiring drawing of the L298N: VS from the battery, GND shared, IN1 and IN2 to the UNO.
Part 2 — Make it
Build the core of your gadget with one motor on the L298N. Use the motor.h helpers from Challenge 3. Make the motor run your sequence of moves. Brake for at least 500 ms before every change of direction.
It works if the motor never snaps straight from forward into reverse.
Bring back next class: your design, your uploaded sketch and motor.h, and a photo of the wired L298N. Keep both motors wired, and bring a potentiometer for L03-09.