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 of the Circuit

This circuit is designed to control an obstacle-avoiding robot. It uses an Arduino UNO as the central microcontroller to process inputs from an HC-SR04 ultrasonic sensor and outputs commands to a servo motor and two DC motors through an L298N motor driver. The robot is powered by two 18650 Li-ion batteries connected in series, with a rocker switch (SPST) to control the power flow. The ultrasonic sensor detects obstacles, and the servo motor adjusts the robot's direction accordingly. The DC motors drive the robot's wheels, allowing it to move and change direction based on sensor input.

Component List

Arduino UNO

Servo

HC-SR04 Ultrasonic Sensor

L298N DC Motor Driver

18650 Li-ion Battery x 2

Rocker Switch (SPST)

Hobby Gearmotor with 48:1 Gearbox (x2)

Wiring Details

Arduino UNO

Servo

HC-SR04 Ultrasonic Sensor

L298N DC Motor Driver

18650 Li-ion Battery x 2

Rocker Switch (SPST)

Hobby Gearmotor with 48:1 Gearbox

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 continuously checks for obstacles using the ultrasonic sensor. When an obstacle is detected, the robot stops, the servo scans for a clear path, and then the robot reverses direction. If no obstacle is detected, the robot continues to move forward.