🚧

Obstacle-Avoiding Robot

Builder · Ages 10–12

  1. 1Read the conceptLearn the big idea behind this project.
  2. 2Check the partsSee which parts the robot needs.
  3. 3Follow the build stepsBuild it step by step, then test it.
  4. 4Answer the quizTap one answer for each of the 10 questions to earn XP.

💡 The concept

An ultrasonic sensor sends out sound pulses and times the echo to measure distance, like a bat. The robot drives forward in a loop and uses an IF condition: if something is closer than about 20 cm, it stops, turns and tries a new direction. Loops + conditions make it act smart.

Parts you need

  • Ultrasonic sensor
  • 2 DC motors + wheels
  • Motor driver
  • Microcontroller
  • Battery pack

🔎 Parts specification

Exactly what each part is, which models to buy, and what it does.

Microcontroller▸

Suggested models: Arduino Uno R3, BBC micro:bit V2, or Raspberry Pi Pico

Features: The robot's brain. Needs at least 4 free pins: trigger + echo for the sensor, and 2 direction pins for the motor driver. The Arduino Uno is the most common choice for wheeled robots.

Ultrasonic sensor▸

Suggested models: HC-SR04

Features: Measures distance from 2cm to 400cm by sending a sound pulse and timing the echo. Has 4 pins: VCC, Trig (output pulse), Echo (timed reply), GND. Works at 5V.

DC motors + wheels▸

Suggested models: TT yellow geared motors (3–6V) with 65mm wheels

Features: Geared motors trade speed for strength — perfect for small robots. Two motors, one per side, let the robot steer by spinning them differently.

Motor driver▸

Suggested models: L298N module or TB6612FNG

Features: The muscle. A microcontroller pin is too weak to spin a motor, so the driver takes small signals from the board and delivers real power from the battery to the motors. L298N can drive 2 motors at once.

Battery pack▸

Suggested models: 4×AA holder (6V) or 18650 shield

Features: Motors are hungry — power them from the battery through the driver, not from the board's 5V pin. Keep a common ground between battery and board.

💻 Program the microcontroller

📲 App to use: Arduino IDE (arduino.cc) with an HC-SR04 example, or MakeCode with the 'sonar' extension for micro:bit

Arduino IDE is a free download. In MakeCode, add the 'sonar' extension to get a distance block.

// Arduino IDE (C++) — Obstacle Avoider
const int TRIG = 7, ECHO = 8;
const int LEFT = 4, RIGHT = 5; // motor driver pins

long distance() {
  digitalWrite(TRIG, LOW); delayMicroseconds(2);
  digitalWrite(TRIG, HIGH); delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  return pulseIn(ECHO, HIGH) / 58; // cm
}

void loop() {
  if (distance() > 20) {
    digitalWrite(LEFT, HIGH);   // drive forward
    digitalWrite(RIGHT, HIGH);
  } else {
    digitalWrite(LEFT, LOW);    // stop, then turn
    digitalWrite(RIGHT, HIGH);
    delay(400);
  }
}

🧠 How it works: distance() sends a 10-microsecond pulse and converts the echo time to centimeters (sound travels ~1cm per 29µs, there and back). The loop drives forward while the path is clear beyond 20cm, otherwise it stops one motor to spin-turn before checking again.

🛠️ Build it step by step

Press Play to watch every part go into place and every wire connect. Ask a grown-up to help with real wiring!

BaseWheelWheel

Step 1 of 6

🛞 Build the base

Attach one motor and wheel to each side of a flat base, and add a small ball caster or skid at the front or back so the robot balances.

🧠 Project quiz

Question 1/10⭐ 0

How does an ultrasonic sensor measure distance?