Projects / Obstacle-avoiding robot

Obstacle-avoiding robot

Advanced2 hGuided buildReviewed
Arduino UnoD5D6D11D12GNDHC-SR04ForwardBack and turnahead 80 cmState

Tap a part to see what it does.

Tap the parts to see what each one does. A teaching simulation, not a circuit simulator.

Build it step by step

0 of 5 done
  1. UNO

    Build the chassis

    Motors, wheels, caster and battery holder on the base.

  2. UNO

    Driver and power

    Battery to VM, Uno 5V to VCC and STBY, grounds joined.

  3. UNO

    Motors

    Left to AO1/AO2, right to BO1/BO2, capacitor across each.

  4. UNO

    Sensor at the front

    Trig D11, Echo D12, as level as you can.

  5. UNO

    Test on blocks first

    Wheels off the ground, then on the floor.

In short

How do you build an obstacle-avoiding robot?

A two-wheel robot drives forward until the ultrasonic sensor sees something within 25 cm, then stops, backs up and turns away. You need Arduino Uno R3, HC-SR04 Ultrasonic Sensor, TB6612FNG Motor Driver, TT gear motor 3 to 6 V × 2, Ceramic Capacitor 100 nF × 2 and Breadboard 830 Tie Points. It takes 5 steps and about 120 minutes.

Level
Advanced
Time
About 120 min
Parts
6

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
4 × AA packTB6612 VM, − to common GNDMotor supply
Uno 5VTB6612 VCC and STBYLogic
D5, D7, D8PWMA, AIN1, AIN2Left motor
D6, D9, D10PWMB, BIN1, BIN2Right motor
HC-SR04 Trig / EchoD11 / D12Distance

Code

No libraries. Tune the speeds and turn time for your chassis. If it turns the wrong way, swap that motor's two wires.

Runs on a virtual Arduino Uno in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

const int PWMA = 5, AIN1 = 7, AIN2 = 8, PWMB = 6, BIN1 = 9, BIN2 = 10, TRIG = 11, ECHO = 12;

void motor(int pwm, int in1, int in2, int speed) {
  digitalWrite(in1, speed > 0); digitalWrite(in2, speed < 0);
  analogWrite(pwm, abs(speed));
}
void drive(int l, int r) { motor(PWMA, AIN1, AIN2, l); motor(PWMB, BIN1, BIN2, r); }

long cm() {
  digitalWrite(TRIG, LOW); delayMicroseconds(2);
  digitalWrite(TRIG, HIGH); delayMicroseconds(10); digitalWrite(TRIG, LOW);
  long t = pulseIn(ECHO, HIGH, 25000);
  return t ? t / 58 : 999;
}

void setup() {
  for (int p : {PWMA, AIN1, AIN2, PWMB, BIN1, BIN2, TRIG}) pinMode(p, OUTPUT);
  pinMode(ECHO, INPUT);
  delay(2000);   // time to put it on the floor
}

void loop() {
  if (cm() > 25) { drive(170, 170); delay(50); return; }
  drive(0, 0); delay(150);
  drive(-150, -150); delay(400);
  drive(160, -160); delay(350);   // spin away
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Wheels liftedBoth spin forward; hand in front makes them stop, reverse and turn
On the floor towards a wallStops 20 to 25 cm away and turns
Battery changeBehaviour the same; slower if cells are low

If it does not work

Spins in circles

One motor wired backwards: swap its wires.

Uno resets on start

Motors on the Uno's 5 V: use the battery for VM.

Hits chair legs

Thin objects reflect poorly; mount the sensor higher or add a second one.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the obstacle-avoiding robot?

Arduino Uno R3, HC-SR04 Ultrasonic Sensor, TB6612FNG Motor Driver, TT gear motor 3 to 6 V × 2, Ceramic Capacitor 100 nF × 2 and Breadboard 830 Tie Points. Ceramic Capacitor 100 nF × 2: One across each motor. Breadboard 830 Tie Points: A half-size board fits a chassis better.

How long does the obstacle-avoiding robot take to build?

About 120 minutes for a advanced maker, in 5 steps.

How do I know the obstacle-avoiding robot works?

Wheels lifted: Both spin forward; hand in front makes them stop, reverse and turn. On the floor towards a wall: Stops 20 to 25 cm away and turns. Battery change: Behaviour the same; slower if cells are low.

Spins in circles: what should I check?

One motor wired backwards: swap its wires.

Uno resets on start: what should I check?

Motors on the Uno's 5 V: use the battery for VM.

Hits chair legs: what should I check?

Thin objects reflect poorly; mount the sensor higher or add a second one.