Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

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

View and Edit Project In Cirkit DesignerImage of Arduino-Controlled Obstacle Avoiding Robot with Ultrasonic Sensor and Servo Motor

Circuit Documentation

Summary

The circuit in question is designed to control a pair of DC motors using an L298N motor driver, interfaced with an Arduino UNO microcontroller. The system also includes an HC-SR04 ultrasonic sensor for distance measurement and an SG90 servo motor for directional control. A 12V battery provides power to the motor driver, which in turn powers the DC motors. The Arduino UNO is powered through its Vin pin connected to the battery and regulates power to the servo motor and the ultrasonic sensor. The circuit is intended for a robotic application that requires obstacle detection and navigation.

Component List

Arduino UNO

HC-SR04 Ultrasonic Sensor

SG90 Servo Motor

L298N DC Motor Driver

DC Motor (x2)

Battery 12V

LED: Two Pin (red)

Wiring Details

Arduino UNO

HC-SR04 Ultrasonic Sensor

SG90 Servo Motor

L298N DC Motor Driver

DC Motors

Battery 12V

LED: Two Pin (red)

Documented Code

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

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

// Define pins for L298N motor driver
#define enA 5
#define in1 7
#define in2 8
#define in3 9
#define in4 10
#define enB 6

// Define pins for HC-SR04 ultrasonic sensor
#define echo A2
#define trigger A1

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

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

  // Set ultrasonic sensor pins as input/output
  pinMode(echo, INPUT);
  pinMode(trigger, OUTPUT);

  // Set L298N motor driver pins as output
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(enB, OUTPUT);

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

  // Attach the servo to pin 13 and sweep it
  myServo.attach(servoPin);
  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);
  }

  // Initial ultrasonic reading
  distance_F = Ultrasonic_read();
  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);
  delayMicroseconds(5);
  digitalWrite(trigger, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigger, LOW);

  long time = pulseIn(echo, HIGH, 38000); // Limit to 38ms

  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(300);

  compareDistance();  // Compare distances and decide direction
}

void forword() {  // Move forward
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
}

void turnRight() { // Turn right
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
}

void turnLeft() {  // Turn left
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
}

void Stop() {  // Stop
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
}

This code controls the servo motor and the DC motors based on the distance measurements from the ultrasonic sensor. It includes functions for moving forward, turning, and stopping the robot. The servo motor is used to scan the environment by moving the ultrasonic sensor to the left and right. The robot uses this information to navigate and avoid obstacles.