Illustration: on a living-room floor a robot car with an ultrasonic sensor on a servo approaches wooden blocks.Robotics
Illustration (made with AI): roughly what the finished project looks like.

A Car That Avoids Obstacles

A two-motor robot car drives forward, spots a wall from afar with its ultrasonic “eye”, stops, looks left and right and turns where the way is clear.

Difficulty
Intermediate
Time
2 h
Age
12+
Parts
8, 8 on the site

Written by the RoboHub team. A question? Write to us.

Variations

Choose a variation and the parts list updates.

Instructions

  1. Build the chassis

    Fix the two motors and wheels to the chassis plate, and the swivel wheel at the front or back. If the motors have no wires, solder them on.

  2. Motor driver

    Connect the left motor to the L298N's OUT1 and OUT2 terminals and the right one to OUT3 and OUT4. Take the jumpers off ENA and ENB and wire those pins to the board's pins 5 and 6. Connect IN1, IN2, IN3 and IN4 to pins 7, 8, 4 and 2.

    Take the jumpers off ENA and ENB. Check “+” and “−” twice before switching on: reversed polarity on VIN destroys the board. D2 is the board's digital pin 2, GND is ground. Red wires are power, black ground, the other colours signals (on a real servo the ground wire is often brown). In the steps, what a step adds is in colour and what is already connected in grey.

    • L298N driver OUT1 → Left motor 1
    • L298N driver OUT2 → Left motor 2
    • L298N driver OUT3 → Right motor 1
    • L298N driver OUT4 → Right motor 2
    • L298N driver ENA → Arduino D5
    • L298N driver IN1 → Arduino D7
    • L298N driver IN2 → Arduino D8
    • L298N driver ENB → Arduino D6
    • L298N driver IN3 → Arduino D4
    • L298N driver IN4 → Arduino D2
  3. Power

    Charge the cells fully and switch the holder off. Connect the holder's “+” to the L298N's 12V terminal and to the board's VIN, and “−” to the L298N's GND and the board's GND. Everything must share the same ground, or the motors won't work. Check “+” and “−” twice before switching on: reversed polarity on VIN destroys the board.

    Take the jumpers off ENA and ENB. Check “+” and “−” twice before switching on: reversed polarity on VIN destroys the board. D2 is the board's digital pin 2, GND is ground. Red wires are power, black ground, the other colours signals (on a real servo the ground wire is often brown). In the steps, what a step adds is in colour and what is already connected in grey.

    • L298N driver 12V → Holder, 2 × 18650 +
    • Holder, 2 × 18650 + → Arduino VIN
    • L298N driver GND → Holder, 2 × 18650 −
    • Holder, 2 × 18650 − → Arduino GND
  4. Sensor on the servo

    Fix the HC-SR04 to the servo arm with glue or a bracket, and the servo to the front of the car. Connect the servo to pin 3, the sensor's TRIG to pin 12 and ECHO to pin 11. Both take power from 5V (the servo's red wire and the sensor's VCC) and ground from GND (the servo's brown wire and the sensor's GND).

    Take the jumpers off ENA and ENB. Check “+” and “−” twice before switching on: reversed polarity on VIN destroys the board. D2 is the board's digital pin 2, GND is ground. Red wires are power, black ground, the other colours signals (on a real servo the ground wire is often brown). In the steps, what a step adds is in colour and what is already connected in grey.

    • SG90 servo SIG → Arduino D3
    • SG90 servo + → Arduino 5V
    • SG90 servo − → Arduino GND
    • HC-SR04 sensor VCC → Arduino 5V
    • HC-SR04 sensor TRIG → Arduino D12
    • HC-SR04 sensor ECHO → Arduino D11
    • HC-SR04 sensor GND → Arduino GND
  5. Upload and test

    Keep the car wheels-up while uploading. If a wheel spins backwards, swap that motor's two wires. If the car is too fast, lower SPEED.

    Arduino C++
    #include <Servo.h>
    
    // L298N: take the jumpers off ENA and ENB and wire them to PWM pins
    const int ENA = 5, IN1 = 7, IN2 = 8;   // left motor
    const int ENB = 6, IN3 = 4, IN4 = 2;   // right motor
    const int TRIG = 12, ECHO = 11, SERVO = 3;
    const int SPEED = 170;                 // 0-255: lower it if the car is too fast
    const int STOP_CM = 25;                // stop when something is this close
    Servo neck;
    
    void drive(int left, int right) {      // -255..255 for each side
      digitalWrite(IN1, left > 0 ? HIGH : LOW);
      digitalWrite(IN2, left < 0 ? HIGH : LOW);
      digitalWrite(IN3, right > 0 ? HIGH : LOW);
      digitalWrite(IN4, right < 0 ? HIGH : LOW);
      analogWrite(ENA, abs(left));
      analogWrite(ENB, abs(right));
    }
    
    long distanceCm() {
      digitalWrite(TRIG, LOW);
      delayMicroseconds(2);
      digitalWrite(TRIG, HIGH);
      delayMicroseconds(10);
      digitalWrite(TRIG, LOW);
      long us = pulseIn(ECHO, HIGH, 30000);
      return us == 0 ? 400 : us / 58;      // nothing in range counts as far away
    }
    
    long lookAt(int angle) {
      neck.write(angle);
      delay(400);                          // let the servo get there
      return distanceCm();
    }
    
    void setup() {
      int pins[] = {ENA, IN1, IN2, ENB, IN3, IN4, TRIG};
      for (int pin : pins) pinMode(pin, OUTPUT);
      pinMode(ECHO, INPUT);
      neck.attach(SERVO);
      neck.write(90);                      // look straight ahead
      delay(2000);                         // time to put the car on the floor
    }
    
    void loop() {
      if (distanceCm() > STOP_CM) {
        drive(SPEED, SPEED);
        delay(50);
        return;
      }
      drive(0, 0);                         // something ahead: stop
      delay(100);
      drive(-SPEED, -SPEED);               // back off a little
      delay(300);
      drive(0, 0);
      long left = lookAt(160);
      long right = lookAt(20);
      neck.write(90);
      if (left > right) drive(-SPEED, SPEED);   // turn on the spot to the left
      else drive(SPEED, -SPEED);                // or to the right
      delay(350);
      drive(0, 0);
      delay(200);
    }
  6. If it looks the wrong way

    If the car turns towards the wall, the servo is mounted the other way round: swap 160 and 20 in the code.

    Illustration: a two-wheel robot car with an ultrasonic sensor on a servo, wooden blocks ahead.

    Illustration, made with AI

  • Robotics
    Intermediate1 h 30 min12+

    A Line-Following Car

    Two infrared sensors see a black line on the floor and the car follows it on its own, round the bends too. On a wide black strip it stops, as if at a finish line.

    7/7 parts on the site
  • Robotics
    Intermediate1 h 30 min12+

    A Phone-Controlled Car (Bluetooth)

    An HC-05 Bluetooth module links the car to your phone. Drive it forward and back and turn it left and right with the buttons of an Android app, and change its speed too.

    8/8 parts on the site
  • NewRobotics
    Advanced4 h12+

    A PID Line Follower Robot

    Five infrared sensors measure how far the robot has drifted off the line, and a PID controller corrects the motors smoothly and fast. Built for competitions: the track, the sensor bar, the code and how to tune it.

    8/8 parts on the site