Maze-solving robot
What you’ll build
A robot that drives through a maze made of walls (cardboard boxes or planks), following the right-hand wall until it reaches the exit.
Parts
Your 2WD car, with two distance sensors: one facing forward and one facing right. Two HC-SR04s work (trigger them one after the other so they don’t hear each other’s echoes), or small ToF sensors such as the VL53L0X.
Build a test maze
- Corridors at least 2–3 times the robot’s width.
- Walls at least as high as the sensors.
- Start simple: an L-shape, then a U-turn, then add dead ends.
The algorithm
The right-hand rule (see Navigation):
| Front | Right | Action |
|---|---|---|
| Blocked | — | Turn left (on the spot) |
| Clear | Gap (far) | Turn right gently |
| Clear | Too close | Steer left a little |
| Clear | Good distance | Straight |
Tuning tips
- Turn into gaps gently, or the robot clips the end of the wall.
- Slow down: maze solving rewards reliability, not speed.
- A wall at a slant confuses ultrasonic sensors; keep walls square to the robot’s path.
Arduino wall follower
const int ENA = 5, IN1 = 7, IN2 = 8, ENB = 6, IN3 = 9, IN4 = 10;
void setMotor(int en, int a, int b, int speed) { // speed -255..255 digitalWrite(a, speed > 0); digitalWrite(b, speed < 0); analogWrite(en, min(abs(speed), 255));}
void drive(int left, int right) { setMotor(ENA, IN1, IN2, left); setMotor(ENB, IN3, IN4, right);}
void setupMotors() { int pins[] = {ENA, IN1, IN2, ENB, IN3, IN4}; for (int p : pins) pinMode(p, OUTPUT);}
const int F_TRIG = 3, F_ECHO = 2, R_TRIG = 12, R_ECHO = 4;
float readCm(int trig, int echo) { digitalWrite(trig, LOW); delayMicroseconds(2); digitalWrite(trig, HIGH); delayMicroseconds(10); digitalWrite(trig, LOW); long us = pulseIn(echo, HIGH, 20000); return us == 0 ? 300 : us * 0.0343 / 2;}
void setup() { setupMotors(); pinMode(F_TRIG, OUTPUT); pinMode(F_ECHO, INPUT); pinMode(R_TRIG, OUTPUT); pinMode(R_ECHO, INPUT); delay(2000);}
void loop() { float front = readCm(F_TRIG, F_ECHO); delay(10); // let echoes die down float right = readCm(R_TRIG, R_ECHO);
if (front < 15) drive(-150, 150); // wall ahead: turn left else if (right > 30) drive(170, 110); // gap: gentle right else if (right < 10) drive(110, 170); // too close: steer left else drive(170, 170);}Try it in the simulator
Robot commands
motors(left, right): wheel power, −100 to 100lineLeft(),lineRight():trueover the black linedistance(): cm to the nearest thing ahead (max 200)distanceLeft(),distanceRight(): cm to the sideslinePosition(): where the line is, −1 (left) to 1 (right), ornullmillis(): milliseconds since Runprint(value): show a message belowsetup()runs once;loop()runs 50 times a second
Reach the goalEasy
Run the wall follower and note the time it takes to reach the goal.
Beat 45 secondsMedium
Tune the speeds and distances to reach the goal in under 45 s.
Remember the wayHard
Real maze robots explore once, then race the shortest route. Describe how your robot could record its turns on the first run and remove dead ends for the second run. Record each decision as L, R, S (straight) or U (U-turn). A sequence like “L U L” can be replaced by “S”.
Quick check
Finished this lesson?