Obstacle-avoiding robot gets stuck in corners: add a backup turn and a stuck check

Back up before you turn, and change the turn direction after repeated hits.

What the message means

A rule like “turn left when blocked” can trap the robot in a corner, where every turn meets another wall.

Fix it in this order

  1. Reverse for a short time when an obstacle is close.
  2. Turn by a larger angle, with a little randomness.
  3. Alternate left and right turns after repeated blocks.
  4. Count hits in a short time and treat many as stuck.
  5. When stuck, do a longer reverse and a half turn.
  6. Test in a corner and a narrow gap, not only in open space.

Why it happens

In a corner, a robot that always turns the same way keeps meeting the same wall and repeats its turn forever. The sensor sees an obstacle again after every turn.

Back up for a moment before turning, remember the last turn direction and pick the other one, or turn by a random angle. A short timer that detects repeated turns and triggers a longer escape also helps.

If you see this, do this

You seeDo this
Turns left and right foreverCorner trap.
Hits the wall at an angleSensor beam misses it.

ultrasonic reads zero, delay blocks the robot.

Last reviewed: November 2026. These are the usual causes and the order that finds them fastest. Test on your own board; clones and cables differ.

Why this happens

A rule such as ‘if blocked, turn left’ has no memory. In a corner the robot turns left, faces another wall, sees it blocked, turns left again, and ends up repeating the same sequence. Turning on the spot also moves the sensor beam only a little, so it may keep reading the same wall.

Ultrasonic sensors add a second problem: a wall at a sharp angle reflects the sound away from the sensor, so the robot may not detect it until it is too close. A small dead zone near the sensor (about 2 cm for an HC-SR04) hides very near objects.

How to confirm the cause

  • Log each avoidance event with millis() and the turn direction. A repeating pattern every second or two shows a loop.
  • Place the robot in a corner made of two boxes and watch what happens in a minute.
  • Print the distance as the robot turns. If it stays small in all directions, the corner is too tight for the turn size.
  • Test with an angled wall. If the reading jumps to the maximum, the beam is being deflected.

Worked example

The helper functions stopMotors, backward, turnLeft and turnRight are assumed to exist in your sketch.

int lastDir = 1;                 // 1 = last turn right, -1 = left

void avoid() {
  stopMotors();
  backward();  delay(400);       // back off first
  if (lastDir > 0) turnLeft(); else turnRight();
  lastDir = -lastDir;            // alternate the direction
  delay(350 + random(0, 300));   // varied turn time
  stopMotors();
}

A stuck check can sit beside it: record the time of each avoidance and, if three happen within a few seconds, run a longer reverse and a half turn.

Common mistakes

  • Reversing without checking for something behind the robot.
  • Turning for a fixed time even though battery voltage changes the angle.
  • Setting the stop distance too short for the robot’s speed.
  • Using only one front sensor in a narrow gap.

If this did not fix it

  • Check the ultrasonic readings for zeros and spikes.
  • Check wheel slip on the floor, which makes turns smaller than planned.
  • Check the battery, since low voltage weakens the turns.
  • Consider two side sensors or a servo scan.