All projects
πŸš—πŸ”΄ Advanced 3 hrs

Obstacle-Avoiding Car

A robot car that drives on its own and steers away from walls and furniture.

What You'll Build

Here you build a real robot. Two motors drive the wheels, an ultrasonic sensor on a servo looks ahead, and the Arduino decides where to go. Put it on the floor and it will explore your room by itself, backing up and turning whenever something blocks the way.

What You Need

  • βœ“Arduino Uno
  • βœ“L298N motor driver
  • βœ“2 Γ— DC gear motor
  • βœ“HC-SR04
  • βœ“Servo
  • βœ“Battery pack
  • βœ“Chassis

How It Works

The Arduino cannot power motors directly β€” its pins are far too weak. The L298N driver sits in between: the Arduino sends small control signals, and the driver switches the big battery current to the motors. The robot repeats a simple think-loop: measure the distance ahead; if the path is clear, drive forward; if not, stop, look left and right, and turn toward whichever side has more space.

Wiring

  1. 1Battery + β†’ L298N 12V, battery βˆ’ β†’ L298N GND, and join that GND to Arduino GND.
  2. 2L298N IN1–IN4 β†’ Arduino pins 5, 6, 7, 8. ENA/ENB β†’ PWM pins 9 and 10.
  3. 3Motors screw into OUT1/OUT2 and OUT3/OUT4.
  4. 4HC-SR04 Trig β†’ pin 12, Echo β†’ pin 13. Servo signal β†’ pin 11.

Step-by-Step Instructions

  1. 1

    Build the chassis

    Bolt the motors and wheels on, then add the caster wheel at the back. Keep the battery low and central so the car does not tip while turning.

  2. 2

    Wire the driver

    Double-check the shared ground. A robot with two separate power supplies and no common ground behaves randomly, and it is the single most common robot bug.

  3. 3

    Test each motor alone

    Write a tiny sketch that drives forward for two seconds. If one wheel spins backwards, just swap that motor's two screw-terminal wires.

  4. 4

    Add the scanner

    Mount the servo at the front with the ultrasonic sensor on top. Centre the servo at 90Β° before you glue anything.

  5. 5

    Upload the brain

    Now put it together: sense, decide, drive. Start with a generous 30 cm safety distance and lower it once you trust the robot.

  6. 6

    Tune the behaviour

    Change the turn duration to make the robot more or less curious, and adjust speed so it does not crash before it can react.

Arduino Code

obstacle-avoiding-car.ino
#include <Servo.h>

Servo look;
const int TRIG = 12, ECHO = 13;
const int IN1 = 5, IN2 = 6, IN3 = 7, IN4 = 8;
const int ENA = 9, ENB = 10;
const int SPEED = 150;
const int SAFE_CM = 30;

int distance() {
  digitalWrite(TRIG, LOW);  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH); delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  return pulseIn(ECHO, HIGH) * 0.034 / 2;
}

void drive(int a, int b, int c, int d) {
  digitalWrite(IN1, a); digitalWrite(IN2, b);
  digitalWrite(IN3, c); digitalWrite(IN4, d);
  analogWrite(ENA, SPEED); analogWrite(ENB, SPEED);
}

void forward() { drive(HIGH, LOW, HIGH, LOW); }
void back()    { drive(LOW, HIGH, LOW, HIGH); }
void left()    { drive(LOW, HIGH, HIGH, LOW); }
void right()   { drive(HIGH, LOW, LOW, HIGH); }
void stopCar() { analogWrite(ENA, 0); analogWrite(ENB, 0); }

void setup() {
  for (int p = 5; p <= 10; p++) pinMode(p, OUTPUT);
  pinMode(TRIG, OUTPUT); pinMode(ECHO, INPUT);
  look.attach(11);
  look.write(90);
}

void loop() {
  if (distance() > SAFE_CM) {
    forward();
  } else {
    stopCar();  delay(200);
    back();     delay(400);
    stopCar();

    look.write(160); delay(400);
    int leftSpace = distance();
    look.write(20);  delay(600);
    int rightSpace = distance();
    look.write(90);  delay(400);

    if (leftSpace > rightSpace) left(); else right();
    delay(450);
    stopCar();
  }
}

Understanding the Code

void drive(int a, ...)

A helper function. Instead of writing four digitalWrite lines everywhere, you describe the motion once and reuse it.

analogWrite(ENA, SPEED)

PWM controls how fast the motors spin. 0 is stopped, 255 is full speed.

if (distance() > SAFE_CM)

The whole robot brain in one line: clear ahead means keep going.

look.write(160)

Turns the sensor to peek left before deciding which way to escape.

Challenges & Upgrades

Add a line-following sensor so the car can follow a track instead.
Add a buzzer that beeps while reversing, like a real truck.
Control the robot from your phone over Bluetooth.

Troubleshooting

πŸ˜– Arduino resets when motors start

Fix: The motors are dragging the supply down. Use a separate battery for the driver and share only the ground.

πŸ˜– Car spins in circles

Fix: One motor is wired backwards β€” swap its two output wires.

Up next

🦾 Servo-Controlled Robot Arm

Build This