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

Arduino-Controlled Obstacle Avoiding Robot with Ultrasonic Sensor and Servo Motor

Image of Arduino-Controlled Obstacle Avoiding Robot with Ultrasonic Sensor and Servo Motor

Circuit Documentation

Summary

This circuit is designed to control a robot with obstacle avoidance capabilities. It uses an Arduino UNO as the central processing unit, interfaced with an HC-SR04 ultrasonic sensor for distance measurement, an SG90 servo motor for directional control, and a dual-channel L298N DC motor driver to control two DC motors for movement. The system is powered by a 12V battery, and there is also an LED for indication purposes.

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.

HC-SR04 Ultrasonic Sensor

  • An ultrasonic distance sensor that can measure distances from 2cm to 400cm with an accuracy of 3mm.
  • It has four pins: VCC, TRIG, ECHO, and GND.

SG90 Servo Motor

  • A small and lightweight servo motor capable of precise control.
  • It operates on 5V and has three pins: PWM, 5V, and GND.

L298N DC Motor Driver

  • A dual H-bridge motor driver capable of driving two DC motors or one stepper motor.
  • It has several control pins for motor direction and speed, as well as power input pins.

DC Motor (x2)

  • Standard DC motors used for the robot's movement.
  • Each motor has two pins for electrical connection.

Battery 12V

  • A 12V battery providing the power source for the motor driver and motors.

LED: Two Pin (red)

  • A basic red LED with an anode and cathode for indication purposes.

Wiring Details

Arduino UNO

  • 5V connected to HC-SR04 VCC and SG90 5V.
  • GND connected to HC-SR04 GND and SG90 GND.
  • Vin connected to L298N 5V.
  • A1 connected to HC-SR04 TRIG.
  • A2 connected to HC-SR04 ECHO.
  • D13 connected to SG90 PWM.
  • D5 connected to L298N ENA.
  • D6 connected to L298N ENB.
  • D7 connected to L298N IN1.
  • D8 connected to L298N IN2.
  • D9 connected to L298N IN3.
  • D10 connected to L298N IN4.

HC-SR04 Ultrasonic Sensor

  • VCC connected to Arduino UNO 5V.
  • TRIG connected to Arduino UNO A1.
  • ECHO connected to Arduino UNO A2.
  • GND connected to Arduino UNO GND.

SG90 Servo Motor

  • PWM connected to Arduino UNO D13.
  • 5V connected to Arduino UNO 5V.
  • GND connected to Arduino UNO GND.

L298N DC Motor Driver

  • GND connected to Arduino UNO GND and battery 12V -.
  • 5V connected to Arduino UNO Vin.
  • 12V connected to battery 12V +.
  • ENA connected to Arduino UNO D5.
  • ENB connected to Arduino UNO D6.
  • IN1 connected to Arduino UNO D7.
  • IN2 connected to Arduino UNO D8.
  • IN3 connected to Arduino UNO D9.
  • IN4 connected to Arduino UNO D10.
  • OUT1 connected to DC Motor 1 pin 2.
  • OUT2 connected to DC Motor 1 pin 1.
  • OUT3 connected to DC Motor 2 pin 2.
  • OUT4 connected to DC Motor 2 pin 1.

DC Motor

  • Motor 1: pin 1 connected to L298N OUT2, pin 2 connected to L298N OUT1.
  • Motor 2: pin 1 connected to L298N OUT4, pin 2 connected to L298N OUT3.

Battery 12V

  • + connected to L298N 12V.
  • - connected to L298N GND.

LED: Two Pin (red)

  • anode and cathode not connected in the provided net list.

Documented Code

#include <Servo.h>  // Include the Servo library

Servo myServo;  // Create a servo object to control the servo

#define enA 5  // Enable1 L298 Pin enA
#define in1 7  // Motor1 L298 Pin in1
#define in2 8  // Motor1 L298 Pin in2
#define in3 9  // Motor2 L298 Pin in3
#define in4 10 // Motor2 L298 Pin in4
#define enB 6  // Enable2 L298 Pin enB

#define echo A2    // Echo pin
#define trigger A1 // Trigger pin

#define servoPin 13 // Define the pin for the servo

int Set = 20;  // Set distance threshold in cm
int distance_L, distance_F, distance_R;

void setup() {
  Serial.begin(9600); // start serial communication at 9600bps

  pinMode(echo, INPUT); // declare ultrasonic sensor Echo pin as input
  pinMode(trigger, OUTPUT); // declare ultrasonic sensor Trigger pin as Output

  pinMode(enA, OUTPUT); // declare as output for L298 Pin enA
  pinMode(in1, OUTPUT); // declare as output for L298 Pin in1
  pinMode(in2, OUTPUT); // declare as output for L298 Pin in2
  pinMode(in3, OUTPUT); // declare as output for L298 Pin in3
  pinMode(in4, OUTPUT); // declare as output for L298 Pin in4
  pinMode(enB, OUTPUT); // declare as output for L298 Pin enB

  analogWrite(enA, 130); // Set speed for Motor1
  analogWrite(enB, 130); // Set speed for Motor2

  myServo.attach(servoPin);  // Attach the servo to pin 13

  // Sweep the servo from 70° to 140° and back
  for (int angle = 70; angle <= 140; angle += 5) {
    myServo.write(angle);
    delay(100);
  }
  for (int angle = 140; angle >= 0; angle -= 5) {
    myServo.write(angle);
    delay(100);
  }
  for (int angle = 0; angle <= 70; angle += 5) {
    myServo.write(angle);
    delay(100);
  }

  distance_F = Ultrasonic_read();  // Initial ultrasonic reading
  delay(500);
}

void loop() {
  // Obstacle Avoiding Logic
  distance_F = Ultrasonic_read();
  Serial.print("D F="); Serial.println(distance_F);

  // If no obstacle ahead, move forward
  if (distance_F > Set) {
    forword();
  } else {
    // Check left and right sides if obstacle is detected ahead
    Check_side();
  }

  delay(10);  // Short delay for stability
}

// Function to read the ultrasonic sensor
long Ultrasonic_read() {
  digitalWrite(trigger, LOW);  // Ensure trigger is LOW for a few microseconds
  delayMicroseconds(5);
  digitalWrite(trigger, HIGH); // Send 10-microsecond pulse to trigger the sensor
  delayMicroseconds(10);
  digitalWrite(trigger, LOW);  // Turn off trigger

  long time = pulseIn(echo, HIGH, 38000); // Limit to 38ms (max distance ~6.5m)

  if (time == 0) {
    Serial.println("No echo detected");
    return 9999; // Return large value if no echo is detected
  }

  long distance = time / 29 / 2;  // Convert time to distance in cm
  Serial.print("Distance: "); Serial.println(distance);
  return distance;
}

void compareDistance() {
  if (distance_L > distance_R) {
    turnLeft();
    delay(200);
    forword();
    delay(700);
    turnRight();
    delay(500);
  } else {
    turnRight();
    delay(300);
    forword();
    delay(700);
    turnLeft();
    delay(500);
  }
}

void Check_side() {
  Stop();  // Stop the robot before checking sides
  delay(100);

  // Move servo to check right side
  Serial.println("Checking Right");
  myServo.write(140);
  delay(300);
  distance_R = Ultrasonic_read();
  Serial.print("D R="); Serial.println(distance_R);
  delay(100);

  // Move servo to check left side
  Serial.println("Checking Left");
  myServo.write(0);
  delay(500);
  distance_L = Ultrasonic_read();
  Serial.print("D L="); Serial.println(distance_L);
  delay(100);

  // Reset servo to center
  myServo.write(70);
  delay