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

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

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

Circuit Documentation

Summary

This document describes an obstacle-avoiding robot circuit. The robot is controlled by an Arduino UNO microcontroller and includes two DC motors, an ultrasonic sensor, and a servo motor. The robot moves forward until an obstacle is detected, at which point it stops, scans for a clear path using the servo-mounted ultrasonic sensor, and then turns in the direction of the clear path.

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. L298N DC Motor Driver

    • Description: Dual H-Bridge motor driver module.
    • 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
  3. Ultrasonic Sensor

    • Description: Sensor for measuring distance using ultrasonic waves.
    • Pins: +VCC, Trigger, Echo, GND
  4. DC Motor

    • Description: Standard DC motor.
    • Pins: pin 1, pin 2
  5. Servo

    • Description: Servo motor for precise control of angular position.
    • Pins: gnd, vcc, pulse

Wiring Details

Arduino UNO

L298N DC Motor Driver

Ultrasonic Sensor

DC Motor 1

DC Motor 2

Servo

Code Documentation

/*
 * This Arduino sketch controls an obstacle-avoiding robot with two DC motors,
 * an ultrasonic sensor, and a servo motor. The robot moves forward until an
 * obstacle is detected, at which point it stops, scans for a clear path using
 * the servo-mounted ultrasonic sensor, and then turns in the direction of the
 * clear path.
 */

#include <Servo.h>

// Pin definitions
const int motor1Pin1 = 10;
const int motor1Pin2 = 9;
const int motor2Pin1 = 8;
const int motor2Pin2 = 7;
const int triggerPin = 2;
const int echoPin = 3;
const int servoPin = 5;

Servo myServo;

void setup() {
  // Initialize motor control pins as outputs
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(motor2Pin1, OUTPUT);
  pinMode(motor2Pin2, OUTPUT);
  // Initialize ultrasonic sensor pins
  pinMode(triggerPin, OUTPUT);
  pinMode(echoPin, INPUT);
  // Initialize servo
  myServo.attach(servoPin);
  myServo.write(90); // Center the servo
  Serial.begin(9600);
}

long readUltrasonicDistance() {
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  return pulseIn(echoPin, HIGH) * 0.034 / 2;
}

void moveForward() {
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin1, HIGH);
  digitalWrite(motor2Pin2, LOW);
}

void stopMotors() {
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin1, LOW);
  digitalWrite(motor2Pin2, LOW);
}

void turnLeft() {
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH);
  digitalWrite(motor2Pin1, HIGH);
  digitalWrite(motor2Pin2, LOW);
}

void turnRight() {
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin1, LOW);
  digitalWrite(motor2Pin2, HIGH);
}

void loop() {
  moveForward();
  if (readUltrasonicDistance() < 10) {
    stopMotors();
    delay(500);
    myServo.write(0);
    delay(1000);
    long distanceLeft = readUltrasonicDistance();
    myServo.write(180);
    delay(1000);
    long distanceRight = readUltrasonicDistance();
    myServo.write(90);
    delay(500);
    if (distanceLeft > distanceRight) {
      turnLeft();
    } else {
      turnRight();
    }
    delay(500);
  }
}

This code controls the obstacle-avoiding robot. It initializes the pins for the motors, ultrasonic sensor, and servo motor. The robot moves forward until an obstacle is detected. When an obstacle is detected, the robot stops, scans for a clear path using the servo-mounted ultrasonic sensor, and then turns in the direction of the clear path.