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

This circuit is designed to control an obstacle-avoiding robot. It uses an Arduino UNO as the central microcontroller to manage inputs and outputs, including signals to a servo motor and two DC motors through an L298N motor driver. An HC-SR04 ultrasonic sensor is used to detect obstacles. The robot changes direction when an obstacle is detected within a certain distance. A 3xAAA battery pack provides the power supply for the circuit.

Component List

Arduino UNO

DC Motor (x2)

Servo

HC-SR04 Ultrasonic Sensor

3xAAA Battery Pack with Switch and JST

L298N DC Motor Driver

Wiring Details

Arduino UNO

DC Motors

Servo

HC-SR04 Ultrasonic Sensor

L298N DC Motor Driver

3xAAA Battery Pack with Switch and JST

Documented Code

/*
 * This Arduino sketch controls an obstacle-avoiding robot with a servo motor.
 * The robot uses an HC-SR04 ultrasonic sensor to detect obstacles and a servo
 * motor to change direction. Two DC motors are used for movement, controlled
 * by an L298N motor driver. The robot stops and changes direction when an
 * obstacle is detected within a certain distance.
 */

#include <Servo.h>

// Pin definitions
const int trigPin = 2;
const int echoPin = 3;
const int servoPin = 5;
const int motor1Pin1 = 10;
const int motor1Pin2 = 9;
const int motor2Pin1 = 8;
const int motor2Pin2 = 7;
const int enA = 11;
const int enB = 6;

Servo myServo;

void setup() {
  // Initialize the motor control pins as outputs
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(motor2Pin1, OUTPUT);
  pinMode(motor2Pin2, OUTPUT);
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  // Initialize the ultrasonic sensor pins
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  // Attach the servo motor
  myServo.attach(servoPin);
  // Start the servo at 90 degrees
  myServo.write(90);
}

void loop() {
  // Measure the distance to an obstacle
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;

  if (distance < 20) { // If an obstacle is detected within 20 cm
    // Stop the motors
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, LOW);
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, LOW);
    // Turn the servo to look for a clear path
    myServo.write(0);
    delay(500);
    myServo.write(180);
    delay(500);
    myServo.write(90);
    // Reverse the motors to change direction
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, HIGH);
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, HIGH);
    delay(1000);
  } else { // If no obstacle is detected
    // Move the motors forward
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW);
    digitalWrite(motor2Pin1, HIGH);
    digitalWrite(motor2Pin2, LOW);
  }
}

This code is responsible for the operation of the obstacle-avoiding robot. It initializes the pins, sets up the servo, and controls the motors based on the distance readings from the ultrasonic sensor. The robot moves forward until an obstacle is detected within 20 cm, at which point it stops, looks for a clear path with the servo, and then reverses direction.