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

Arduino UNO Obstacle Avoidance Robot with Ultrasonic Sensor and Servo Motor

View and Edit Project In Cirkit DesignerImage of Arduino UNO Obstacle Avoidance Robot with Ultrasonic Sensor and Servo Motor

Circuit Documentation

Summary

This document provides a detailed overview of an obstacle avoidance robot circuit. The circuit includes an Arduino UNO microcontroller, a Tower Pro SG90 servo motor, an L298N DC motor driver, an HC-SR04 ultrasonic sensor, two hobby motors, and a 7.4V power supply. The robot uses the ultrasonic sensor to detect obstacles and the servo motor to scan the environment. Based on the sensor readings, the Arduino UNO controls the motors via the L298N motor driver to navigate around obstacles.

Component List

  1. Arduino UNO

    • Description: Microcontroller board based on the ATmega328P.
    • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
  2. Tower Pro SG90 servo

    • Description: Small servo motor used for precise control of angular position.
    • Pins: Signal, +5V, GND
  3. L298N DC motor driver

    • Description: Dual H-Bridge motor driver used to control the speed and direction of two DC motors.
    • Pins: OUT1, OUT2, 12V, GND, 5V, OUT3, OUT4, 5V-ENA-JMP-I, 5V-ENA-JMP-O, +5V-J1, +5V-J2, ENA, IN1, IN2, IN3, IN4, ENB
  4. HC-SR04 Ultrasonic Sensor

    • Description: Ultrasonic distance sensor used to measure the distance to an object.
    • Pins: VCC, TRIG, ECHO, GND
  5. Motor amarillo motorreductor hobby

    • Description: Hobby motor with a gearbox for increased torque.
    • Pins: vcc, GND
  6. 7.4V Power Supply

    • Description: Power supply providing 7.4V for the circuit.
    • Pins: +, -

Wiring Details

Arduino UNO

Tower Pro SG90 servo

L298N DC motor driver

HC-SR04 Ultrasonic Sensor

Motor amarillo motorreductor hobby

7.4V Power Supply

Code Documentation

#include <Servo.h>

/*
 * Obstacle Avoidance Robot
 * This code controls a robot with an ultrasonic sensor, servo motor, and
 * L298N motor driver. The robot scans for obstacles at 30, 90, and 150 degrees
 * and decides the best direction to move.
 */

Servo myServo;

const int trigPin = 2;
const int echoPin = 3;
const int ENA = 4;
const int RVS1 = 5;
const int RVS2 = 6;
const int FWD1 = 7;
const int FWD2 = 8;
const int ENB = 9;
const int servoPin = 10;

int leftDistance;
int middleDistance;
int rightDistance;

void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(RVS1, OUTPUT);
  pinMode(RVS2, OUTPUT);
  pinMode(FWD1, OUTPUT);
  pinMode(FWD2, OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);
  myServo.attach(servoPin);
  myServo.write(90);
  delay(200);
}

void loop() {
  int currentDistance = measureDistance(); // front scan
  Serial.print("Current Distance: ");
  Serial.print(currentDistance);
  Serial.println(" cm");
  if (currentDistance < 20) {
    stopCar();
    leftDistance = checkDirection(30); // Check left
    Serial.print("Left Distance: ");
    Serial.print(leftDistance);
    Serial.println(" cm");
    delay(1500);
    middleDistance = checkDirection(90); // Check middle
    Serial.print("Middle Distance: ");
    Serial.print(middleDistance);
    Serial.println(" cm");
    delay(1500);
    rightDistance = checkDirection(150); // Check right
    Serial.print("Right Distance: ");
    Serial.print(rightDistance);
    Serial.println(" cm");
    delay(1500);
    myServo.write(90);
    if (leftDistance > middleDistance && leftDistance > rightDistance) {
      moveReverse();
      turnLeft();
    } else if (rightDistance > middleDistance) {
      moveReverse();
      turnRight();
    }
    moveForward();
  } else {
    moveForward();
  }
}

int measureDistance() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  long duration = pulseIn(echoPin, HIGH);
  int distance = duration * 0.034 / 2;
  return distance;
}

int checkDirection(int angle) {
  myServo.write(angle);
  delay(100);
  return measureDistance();
}

void moveForward() { // Forward
  digitalWrite(RVS1, HIGH);
  digitalWrite(RVS2, LOW);
  digitalWrite(FWD1, HIGH);
  digitalWrite(FWD2, LOW);
}

void moveReverse() { // Reverse
  digitalWrite(RVS1, LOW);
  digitalWrite(RVS2, HIGH);
  digitalWrite(FWD1, LOW);
  digitalWrite(FWD2