Stage 8: Solve the Maze
We code the mBot in mBlock 5. Keep this tab open all week. Open in a new tab — don’t use the buttons in this page to leave the course.
a robot that finds its own way through a walled maze
how a single clear rule can solve a whole maze, no map needed
an mBot that drives from START to FINISH on its own
Before campers code, show the room:
- Walk a maze yourself with one hand always touching the right wall. You reach the exit without ever seeing a map. Say: "That's the whole trick — keep one rule and never break it."
- Run the robot version: drive forward, and when a wall is close ahead, turn. Let it work through a corner.
- Frame it: "The robot can only see straight ahead. It will be clumsy. A good rule beats being smart."
The big idea
A maze looks like a thinking problem, but a robot solves it with the same parts you already have: drive forward (Stage 1), sense a wall (Stage 4), and decide with if/else (Stage 5). The only new thing is purpose — last time the robot wandered to avoid walls; this time it wanders toward an exit.
The oldest maze trick in the world is the wall-following rule: pick one side and always keep turning that way. Drive forward until a wall blocks you; then turn (say) right; repeat. You never need a map. As long as the maze's walls are all connected, this rule walks you to the exit. The robot is not clever — it just never breaks its rule.
forever:
wall close ahead?
yes → turn
no → drive forward
Use this as the floor plan. Build the real walls from stacked books or foam board about 10 cm tall so the ultrasonic sensor can see them. Start on the green square; reach the blue one.
- maze
- a path with walls and one way out
- rule
- one instruction the robot always follows
- wall-following
- always turning the same way to find an exit
- goal
- the place the robot is trying to reach
Make sure you've finished Stage 5: Don't Crash and Stage 7: You're in Control. Build a maze with walls tall enough for the ultrasonic sensor to see — short walls get missed.
Build it
Step 1 — Drive until a wall, then turn
This is the core rule. Drive forward; when the wall ahead is close, turn.
The wall rule
when green flag clicked forever if <(ultrasonic distance (cm) :: sensing) < (12)> then mBot stop moving :: motion mBot turn [right v] at speed (40) % :: motion wait (0.5) seconds mBot stop moving :: motion else mBot move [forward v] at speed (35) % :: motion end end
Set the robot in your maze and let it go. It drives down a corridor, stops at the wall, turns right, and continues. Watch it work through the first corner.
Step 2 — Tune the turn to your maze
The turn time decides whether the robot makes a clean 90-degree corner. Watch a few turns. Overshooting into the next wall? Shorten the wait. Not turning far enough? Lengthen it. Add a color so you can see it thinking:
Show its thinking
when green flag clicked forever if <(ultrasonic distance (cm) :: sensing) < (12)> then set led [all v] to color [#ffde59] :: looks mBot stop moving :: motion mBot turn [right v] at speed (40) % :: motion wait (0.5) seconds else set led [all v] to color [#47c621] :: looks mBot move [forward v] at speed (35) % :: motion end end
Yellow means "turning at a wall," green means "driving the corridor." Now you can read its decisions from across the room.
Step 3 — Celebrate the finish
When the robot reaches the FINISH square, give it a win. The simplest trigger is the board button — press it the moment the robot arrives, and have it stop, flash, and play a tune. (The hard stretch makes the robot detect the finish on its own.)
Pacing Lab
This lab is required before you move on. The goal is a rule the robot can run all the way through, not a robot you keep rescuing.
Part A — Corner clinic (25 minutes)
Set the robot at a single corner and run it ten times. Tally how many clean turns it makes:
Clean turns out of 10: ______
What went wrong on the misses: ______________
Turn wait now: ____ → new turn wait: ____
Wall threshold now: ____ → new threshold: ____
Tune the turn time and threshold until it makes the corner cleanly at least 8 of 10 times. A reliable corner is the whole maze, repeated.
Part B — Full-run partner check (10 minutes)
With a partner, run the robot from START. Each of you counts the rescues. Talk about where it struggles — corners? dead ends? — and make one change at a time, re-running after each, so you know what helped.
Understand it
The big idea here is that one steady rule can produce complex behavior. You did not program the whole maze. You programmed a rule — forward, or turn at a wall — and the maze's own shape turned that rule into a winning path. Simple rules, repeated again and again, can solve problems that look like they need a big plan.
Your robot is working with almost no information. It sees only what is straight ahead, it has no memory of where it has been, and it cannot see the goal. Still, a good rule can beat a messy plan. When your robot fails, the fix is usually not "make it smarter" — it is "make the rule cleaner" or "make the turn more reliable." That instinct, fix the rule before adding more rules, is real engineering.
Try this
Try this
Three short experiments. Predict before you run, then test your guess.
If you change the turn from right to left, will the robot still reach the exit? Predict for your maze, then test. (Sometimes one side works and the other gets stuck — why?)
Run it with a wall threshold of 12, then 25. Which one turns too early and bumps around the middle of corridors? Which one gets too close before reacting?
Your robot follows a rule to reach a goal. In Stage 9's sumo match, the goal is to push an opponent out. What sensing and what rule would chase a moving target instead of an exit?
Test your stage
- The robot drives the corridors and turns at walls on its own.
- Its turns are close to clean 90-degree corners.
- The LED shows when it is driving versus turning.
- It reaches the FINISH from START (with a button press or auto-detect at the end).
- Design check. When it gets stuck, can you name which part of the rule failed — the threshold, the turn time, or the wall height?
If it breaks
- It drives straight through where a wall should be. The walls are too short for the ultrasonic sensor to see, or shiny/angled so the echo misses. Use taller, flat walls.
- It turns too early, in the middle of a corridor. Your threshold is too big. Lower it so it only turns at a real wall.
- It overshoots corners into the next wall. The turn wait is too long. Shorten it, or lower the turn speed.
- It gets trapped going back and forth. A dead end can trap a one-side rule if the turn isn't a full corner. Tune the turn first; if it still traps, try the always-left rule instead, or the alternate stretch.
Wall height is everything. The number-one "it ignores walls" complaint is short walls the sensor flies over. Set a height standard — about 10 cm, flat surfaces — and most maze frustration disappears.
Resist the urge to let kids hand-tune a turn for one specific corner. Push them toward a rule that handles any corner; the corner clinic lab (8 of 10 clean) enforces that. A robot tuned to one corner falls apart on the next.
This is a great stage for the "simple rules, complex results" wonder moment. When a robot solves a maze nobody mapped, kids feel it. Name it out loud — it is one of the biggest ideas they will meet all week.