Skip to content

Robotics

How an obstacle-avoiding robot works

An obstacle-avoiding robot reads a distance sensor pointing ahead on every pass of its loop. When something is far away it drives; as an obstacle gets closer it slows down; when the obstacle is too close it turns away, and it keeps turning until the path ahead is well clear. Two thresholds, one to start turning and a farther one to stop, keep it from twitching.

Look ahead

The usual sensor is an ultrasonic one like the HC-SR04. It sends a burst of 40 kHz sound and times the echo. Sound travels about 343 m/s and has to go there and back, so each centimetre of distance adds about 58 µs:

distance (cm) ≈ echo time (µs) over 58

Infrared distance sensors do the same job with light. Either way, the code gets a number that's small near a wall and large in open space.

Slow down as you close in

Speed is distance per pass of loop(). Rushing up to a wall gives the robot no room to turn, and one bad reading at speed can mean a crash. So set the speed from the range itself:

int range = analogRead(A0);   // 0 touching, 1023 far
int speed = range / 4;        // nearer means slower

Close in slowly and the robot reaches its turning line with room to spare, and the sensor gets more looks at the obstacle on the way.

Two thresholds, not one

Start turning when the range drops under 300, and keep turning until it's over 500:

if (range < 300) turning = true;
if (range > 500) turning = false;

if (turning) {
  analogWrite(9, 200);   // pivot right
  analogWrite(10, 0);
} else {
  analogWrite(9, speed);
  analogWrite(10, speed);
}

With a single line at 300 for both, a reading that wobbles 295, 305, 295 flips the robot between turning and driving on every wobble, and it shudders along the wall. The 200-count gap is hysteresis: once it starts turning, it turns until it faces well away, then drives on.

Live experiment

One threshold or two?

The rover cruises at range ÷ 4 and starts pivoting right when RANGE drops under 300. With one threshold it twitches along the walls, 39 turns in 15 s; keep turning until RANGE is over 500 and it makes 4 calm ones. Try flat out until 300 too.

Try it yourself: free lesson

“Look before you leap” is lesson 5 of 6 in Waypoint Wharf, Robotics World 3. Free with an account. Lessons unlock in order, so you’ll do the 18 before it first.

Getting smarter

Turning the same way every time can trap a robot in a corner. Better robots pick the clearer side, by turning the sensor on a servo to look left and right or by fitting two sensors angled outward. Add bump switches as a last line of defence, and organise the behaviours, cruise, turn, back off, as a state machine. Robot vacuums use exactly these ideas, with many more sensors.

Free lesson

Now build it yourself

Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.

Try it yourself: free lesson

“Look before you leap” is lesson 5 of 6 in Waypoint Wharf, Robotics World 3. Free with an account. Lessons unlock in order, so you’ll do the 18 before it first.

Or start from the very first lesson, no account needed

Practice questions

Three quick questions from the lessons. Pick an answer to check it.

  1. Question 1 of 3The rover is turning. RANGE then reads 280, 350, 420, 480. With turn under 300 and stop over 500, at 480 it's…
    Show the answer

    Answer: Still turning: it hasn't been over 500 yet. Only a reading over 500 ends the turn. Every reading in that list is under 500.

    • Why not “Stopped”? Nothing in these rules stops the rover; it either turns or drives.
    • Why not “Turning the other way”? The turn direction is fixed by the code, and nothing here changes it.
    • Why not “Driving: it's over 300”? Being over 300 doesn't end a turn. Only a reading over the 500 stop line does.
  2. Question 2 of 3With one threshold, 300, for both starting and stopping, a reading that wobbles 295, 305, 295, 305 makes the rover…
    Show the answer

    Answer: Flip between turning and driving on every wobble. Each wobble crosses the single line, so each one flips the decision. That's the twitch the gap cures.

    • Why not “Turn smoothly away”? A smooth turn needs the decision to stay put, but each wobble across 300 flips it.
    • Why not “Stop dead”? Neither rule stops the rover; it switches between turning and driving instead.
    • Why not “Drive straight on”? It can't drive straight on: every dip under 300 starts a turn again.
  3. Question 3 of 3Why does the rover slow down as it closes in, instead of driving flat out until 300?
    Show the answer

    Answer: Slower, it can turn away in less distance, and the sensor gets more looks at the obstacle. Speed is distance per pass. Arriving slowly means the 300 line is reached with room to spare, and a reading that's wrong for a pass or two does less harm.

    • Why not “It makes no difference”? It makes a big difference: flat out until 300, the rover has no time to turn and hits the wall.
    • Why not “Only to save the battery”? Battery life isn't the main reason. Arriving slowly gives room to turn and more readings before the line.
    • Why not “The range sensor needs it”? The sensor works at any speed; slowing down just gives it more looks per centimetre travelled.
Keep practising: free lesson

“Look before you leap” is lesson 5 of 6 in Waypoint Wharf, Robotics World 3. Free with an account. Lessons unlock in order, so you’ll do the 18 before it first.

Questions people ask

What sensor does an obstacle avoiding robot use?

Usually an ultrasonic sensor like the HC-SR04, which times an echo to measure distance from about 2 cm to a few metres, or an infrared distance sensor. Some use simple bump switches as a last line of defence.

How do I measure distance with an HC-SR04 on Arduino?

Send a 10 µs pulse to its TRIG pin, then time the HIGH pulse on its ECHO pin with pulseIn(). Sound goes there and back, so divide the microseconds by about 58 to get centimetres.

Why does my obstacle avoiding robot twitch near walls?

It's using one threshold to both start and stop turning, so a reading that wobbles across the line flips the decision on every wobble. Start turning below one distance and stop only above a larger one: hysteresis.

Every topic