Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

Arduino UNO Controlled Obstacle Avoiding Robot with Ultrasonic Sensors and L298N Motor Driver

Image of Arduino UNO Controlled Obstacle Avoiding Robot with Ultrasonic Sensors and L298N Motor Driver

Circuit Documentation

Summary

This circuit is designed to control a robotic vehicle with obstacle avoidance capabilities. It uses an Arduino UNO as the central microcontroller to process signals from three HC-SR04 ultrasonic sensors and drive two DC motors through an L298N motor driver. The ultrasonic sensors are used to detect obstacles from different directions (front, left, and right), and the Arduino UNO processes these signals to navigate the vehicle accordingly. The motors are powered by a 9V battery, which also supplies power to the motor driver, while the Arduino UNO and sensors are powered by a separate 9V battery.

Component List

Arduino UNO

  • Microcontroller board based on the ATmega328P
  • It has 14 digital input/output pins, 6 analog inputs, a 16 MHz quartz crystal, a USB connection, a power jack, an ICSP header, and a reset button.

9V Battery

  • Two 9V batteries are used in the circuit. One provides power to the Arduino UNO and sensors, and the other powers the L298N motor driver and the DC motors.

L298N DC Motor Driver

  • A module that can control up to two DC motors with directional and speed control.
  • It has an onboard 5V regulator which can be used to power the Arduino UNO if required.

DC Motor (x2)

  • Standard DC motors used for the movement of the robotic vehicle.

HC-SR04 Ultrasonic Sensor (x3)

  • Ultrasonic distance sensors that can measure distances by sending out a sound wave and measuring the time it takes to come back after hitting an obstacle.

Wiring Details

Arduino UNO

  • 5V to HC-SR04 sensors' VCC pins, L298N 5V pin
  • GND to HC-SR04 sensors' GND pins, L298N GND pin
  • A0 to 9V Battery (-)
  • A1 to 9V Battery (+)
  • A2 to HC-SR04 (Sensor 3) TRIG pin
  • A3 to HC-SR04 (Sensor 2) TRIG pin
  • A4 to HC-SR04 (Sensor 2) ECHO pin
  • A5 to HC-SR04 (Sensor 3) ECHO pin
  • D11 to HC-SR04 (Sensor 1) TRIG pin
  • D10 to HC-SR04 (Sensor 1) ECHO pin
  • D9 to L298N IN2 pin
  • D8 to L298N IN1 pin
  • D6 to L298N ENA pin
  • D5 to L298N ENB pin
  • D4 to L298N IN4 pin
  • D3 to L298N IN3 pin

L298N DC Motor Driver

  • 12V to 9V Battery (+)
  • GND to 9V Battery (-)
  • OUT1 to DC Motor 2 pin 2
  • OUT2 to DC Motor 2 pin 1
  • OUT3 to DC Motor 1 pin 1
  • OUT4 to DC Motor 1 pin 2

DC Motors

  • Motor 1 and Motor 2 are connected to the L298N motor driver's output pins as described above.

HC-SR04 Ultrasonic Sensors

  • All sensors have their VCC and GND connected to the Arduino UNO's 5V and GND.
  • Sensor 1 TRIG and ECHO pins are connected to Arduino UNO's D11 and D10.
  • Sensor 2 TRIG and ECHO pins are connected to Arduino UNO's A3 and A4.
  • Sensor 3 TRIG and ECHO pins are connected to Arduino UNO's A2 and A5.

Documented Code

// Define pins for ultrasonic sensors
const int trigPin1 = 11;
const int echoPin1 = 10;
const int trigPin2 = A3;
const int echoPin2 = A4;
const int trigPin3 = A2;
const int echoPin3 = A5;

// Define pins for motor driver
const int in1 = 9;
const int in2 = 8;
const int in3 = 4;
const int in4 = 3;
const int enA = 5;
const int enB = 6;

// Define constants for PWM and distance threshold
#define PWM 200
#define DIS 25

void setup() {
    // Initialize ultrasonic sensor pins
    pinMode(trigPin1, OUTPUT);
    pinMode(echoPin1, INPUT);
    pinMode(trigPin2, OUTPUT);
    pinMode(echoPin2, INPUT);
    pinMode(trigPin3, OUTPUT);
    pinMode(echoPin3, INPUT);

    // Initialize motor driver pins
    pinMode(in1, OUTPUT);
    pinMode(in2, OUTPUT);
    pinMode(in3, OUTPUT);
    pinMode(in4, OUTPUT);
    pinMode(enA, OUTPUT);
    pinMode(enB, OUTPUT);
}

void loop() {
    // Obstacle avoidance logic
    if (FrontSensor() < DIS && RightSensor() < DIS && LeftSensor() < DIS) {
        // Obstacle in front of all 3 sides
        turn_right();
        delay(3000); // Reverse after delay
    } else if (FrontSensor() < DIS && RightSensor() < DIS && LeftSensor() > DIS) {
        // Obstacle on right and front sides
        turn_left(); // Turn left side
    } else if (FrontSensor() < DIS && RightSensor() > DIS && LeftSensor() < DIS) {
        // Obstacle on left and front sides
        turn_right(); // Turn right side
    } else if (FrontSensor() < DIS && RightSensor() > DIS && LeftSensor() > DIS) {
        // Obstacle on front side
        turn_right(); // Then turn right
    } else if (FrontSensor() > DIS && RightSensor() > DIS && LeftSensor() < DIS) {
        // Obstacle on left side
        turn_right(); // Then turn right and then forward
        delay(180);
        forward();
    } else if (FrontSensor() > DIS && RightSensor() < DIS && LeftSensor() > DIS) {
        // Obstacle on right side
        turn_left(); // Then turn left and then forward
        delay(180);
        forward();
    } else {
        forward(); // No obstacles, move forward
    }
}

// Movement functions for the motors
void forward() {
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    analogWrite(enA, PWM);
    analogWrite(enB, PWM);
}

void turn_left() {
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    analogWrite(enA, PWM);
    analogWrite(enB, PWM);
}

void turn_right() {
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    analogWrite(enA, PWM);
    analogWrite(enB, PWM);
}

void reverse() {
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    analogWrite(enA, PWM);
    analogWrite(enB, PWM);
}

void stop() {
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW);
    analogWrite(enA, LOW);
    analogWrite(enB, LOW);
}

// Sensor functions to measure distance
long FrontSensor() {
    long dur;
    digitalWrite(trigPin1, LOW);
    delayMicroseconds(5);
    digitalWrite(trigPin1, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin1, LOW);
    dur = pulseIn(echoPin1, HIGH);
    return (dur / 58); // Convert to centimeters
}

long RightSensor() {
    long dur;
    digitalWrite(trigPin2, LOW);
    delayMicroseconds(5);
    digitalWrite(trigPin2, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin2, LOW);
    dur = pulseIn(echoPin2, HIGH);
    return (dur / 58); // Convert to centimeters
}

long LeftSensor() {
    long dur;
    digitalWrite(trigPin3, LOW);
    delayMicroseconds(5);
    digitalWrite(trigPin3, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin3, LOW);
    dur = pulseIn(echoPin3, HIGH);
    return (dur / 58); // Convert to centimeters
}

Note: The code provided has some typos and inconsistencies which have been corrected in the documented code above. The pulseIn function returns the duration in microseconds, which is then converted to centimeters by dividing by 58, not 30, 62, or 50 as originally provided. The delay function was misspelled as delat, and int2, int3, and int4 were corrected to in2, in3, and in4. The `Long