Tutorial · Intermediate · 14 min read

Detecting Line Maze Junctions With an IR Array

How a line follower tells a T junction from a cross, a dead end from the finish—and why one sensor reading is never enough to decide.

Published

A line follower has one job: keep the line centred. A line maze robot has two, and the second one is where beginners get stuck. It must still follow the line, but it must also notice the moments when the line offers a choice—and decide what kind of choice it is.

That decision layer sits on top of the PID loop, not inside it. The controller should never know what a maze is. It steers toward a line; the junction layer watches the same sensor readings for the patterns that mean “something changed” and takes over when one appears.

Six things a junction can be

Every junction in a taped line maze reduces to one of six cases. With an 8-channel reflectance array reading 1 for black tape and 0 for the white board, each produces a signature:

Six panels showing line maze junction types drawn as black tape on a light board, each overlaid with an eight-channel sensor bar: left turn only reads 11111000, right turn only reads 00011111, T junction reads all ones, cross reads all ones, dead end reads all zeros, and the finish marker is a solid block reading all ones.
The six junction types and the pattern each one puts on an 8-channel array. Three of them are indistinguishable at the moment of arrival. Download SVG
  • Left turn only — the outer left sensors go black while the right side stays white.
  • Right turn only — the mirror image.
  • T junction — tape spans the full width, with nothing ahead.
  • Cross — tape spans the full width, and the line continues ahead.
  • Dead end — every sensor reads white; the line simply stopped.
  • Finish — a solid filled block, wide and long, that stays black as you drive into it.

The reading alone is ambiguous

Look at the T junction, the cross, and the finish marker. All three report 11111111. A left branch where the line also continues straight reports the same thing as a plain left turn.

This is the part worth internalising: the instantaneous sensor pattern cannot classify a junction. A robot that decides the moment its outer sensors go black will confidently turn left at a crossroads it should have driven straight through.

The fix is motion, not more sensors. When the array first reports a wide black pattern:

  1. Record which side (or sides) went black—that is the set of available branches.
  2. Keep driving forward by roughly one robot length.
  3. Read again. If the centre sensors still see tape, straight ahead was also an option. If everything is white, it was a T. If everything is still black, you have driven into the finish block.

Only now do you know what kind of junction you left behind, and only now can the maze layer choose.

Junction classify(Sensors& s) {
  Branches at = s.readWideBranches();      // left / right seen on arrival
  driveForward(ONE_ROBOT_LENGTH);          // commit past the intersection
  Reading after = s.read();

  if (after.allBlack())  return FINISH;    // still on tape = solid block
  at.straight = after.centreOnLine();      // line continued through
  return at.toJunction();
}

Note that the classifier drives. That is unusual for a sensing function and it is the point—junction type is a property of a short trajectory, not of a single sample.

Choosing a branch

Once a junction is classified, the maze layer picks a branch by a fixed rule. The left-hand rule—always take the leftmost available branch, and turn back at a dead end—is the standard choice because it is stateless, exhaustive on a maze without loops, and trivial to verify by hand.

Consistency matters more than cleverness here. A robot that always prefers left will eventually visit every reachable branch, and the record of its turns is what the route simplification step later collapses into the optimal path. Watch the same explore-then-optimise behaviour run in the Maze Solver Simulator—that one senses walls rather than tape, but the decision layer above the sensing is identical.

Getting it reliable on real tape

Three things break junction detection on hardware far more often than the algorithm does:

  • Calibration drift. Run the array’s calibration sweep over both black tape and white board every time you power up, and re-run it when you move to a different room. Ambient infrared from sunlight and fluorescent lights changes the readings.
  • Sensor height. 5–8 mm above the floor. Too high and the contrast collapses; too low and a slightly warped board scrapes the array.
  • Tape quality. Use 18–20 mm tape and make the junctions physically clean. A frayed corner reads as an intermittent branch, and no amount of code fixes tape that lies to you.

Get plain line following stable on a simple oval first—tune it in the Line Follower Simulator, then on real tape. A maze will hide mechanical and sensor problems behind what looks like a logic bug.

Explore the graph

Part of these builds

Projects and learning paths that include this tutorial.

Further reading

References