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

Arduino UNO Controlled Obstacle Avoiding Robot with L293D Motor Driver and Ultrasonic Sensor

View and Edit Project In Cirkit DesignerImage of Arduino UNO Controlled Obstacle Avoiding Robot with L293D Motor Driver and Ultrasonic Sensor

Circuit Documentation

Summary

This circuit is designed to control a robotic platform equipped with DC motors for movement, an ultrasonic sensor for distance measurement, and a servo motor for directional control of the sensor. The brain of the circuit is an Arduino UNO microcontroller, which interfaces with an L293D motor driver shield to control the motors. The circuit also includes a 4 x AAA battery mount for power and an LCD display for user interface and feedback.

Component List

L293D Motor Driver Shield

DC Motor (x4)

4 x AAA Battery Mount

Servo (Wokwi Compatible)

HC-SR04 Ultrasonic Sensor

Arduino UNO

LCD Display 16x4 I2C

Wiring Details

L293D Motor Driver Shield

DC Motors

4 x AAA Battery Mount

Servo (Wokwi Compatible)

HC-SR04 Ultrasonic Sensor

Arduino UNO

LCD Display 16x4 I2C

Documented Code

#include <Servo.h>
#include <NewPing.h>

// Definition of pins
#define TRIG_PIN 9
#define ECHO_PIN 10
#define MAX_DISTANCE 200 // Maximum measurable distance (in cm)

#define SERVO_PIN 11
#define MOTOR_A_1 2 // Motor A - IN1
#define MOTOR_A_2 3 // Motor A - IN2
#define MOTOR_B_1 4 // Motor B - IN1
#define MOTOR_B_2 5 // Motor B - IN2
#define MOTOR_SPEED 150 // Motor speed (0-255)

#define CENTER_ANGLE 90
#define LEFT_ANGLE 150
#define RIGHT_ANGLE 30

Servo myServo;
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);

// Variables
int distanceFront = 0;
int distanceLeft = 0;
int distanceRight = 0;

void setup() {
  Serial.begin(9600); // Initialize serial communication
  myServo.attach(SERVO_PIN);

  // Configure the motors as outputs
  pinMode(MOTOR_A_1, OUTPUT);
  pinMode(MOTOR_A_2, OUTPUT);
  pinMode(MOTOR_B_1, OUTPUT);
  pinMode(MOTOR_B_2, OUTPUT);

  myServo.write(CENTER_ANGLE); // Initialize the sensor pointing forward
  delay(1000);
  Serial.println("System initialized. Robot ready.");
}

void loop() {
  scanEnvironment(); // Scan the environment

  if (distanceFront > 20) {
    // No obstacles ahead, move forward
    Serial.println("No obstacles detected ahead. Moving forward.");
    moveForward();
  } else if (distanceFront <= 20) {
    // Obstacle ahead, check sides
    Serial.println("Obstacle ahead. Checking sides...");
    if (distanceLeft > distanceRight && distanceLeft > 20) {
      // More space on the left
      Serial.println("More space on the left. Turning left.");
      turnLeft();
    } else if (distanceRight > distanceLeft && distanceRight > 20) {
      // More space on the right
      Serial.println("More space on the right. Turning right.");
      turnRight();
    } else {
      // Insufficient space on both sides, stop
      Serial.println("Insufficient space on both sides. Stopping.");
      stopMoving();
    }
  }
}

void scanEnvironment() {
  // Check the distance in front
  myServo.write(CENTER_ANGLE);
  delay(300);
  distanceFront = sonar.ping_cm();
  
  // Check the distance to the left
  myServo.write(LEFT_ANGLE);
  delay(300);
  distanceLeft = sonar.ping_cm();

  // Check the distance to the right
  myServo.write(RIGHT_ANGLE);
  delay(300);
  distanceRight = sonar.ping_cm();

  // Display the measured distances on the Serial Monitor
  Serial.print("Distance Front: ");
  Serial.print(distanceFront);
  Serial.println(" cm");

  Serial.print("Distance Left: ");
  Serial.print(distanceLeft);
  Serial.println(" cm");

  Serial.print("Distance Right: ");
  Serial.print(distanceRight);
  Serial.println(" cm");
}

void moveForward() {
  // Move the robot forward
  analogWrite(MOTOR_A_1, MOTOR_SPEED);
  analogWrite(MOTOR_A_2, 0);
  analogWrite(MOTOR_B_1, MOTOR_SPEED);
  analogWrite(MOTOR_B_2, 0);
  Serial.println("Moving forward.");
}

void stopMoving() {
  // Stop the robot
  analogWrite(MOTOR_A_1, 0);
  analogWrite(MOTOR_A_2, 0);
  analogWrite(MOTOR_B_1, 0);
  analogWrite(MOTOR_B_2, 0);
  Serial.println("Robot stopped.");
}

void turnLeft() {
  // Turn the robot to the left
  analogWrite(MOTOR_A_1, 0);
  analogWrite(MOTOR_A_2, MOTOR_SPEED);
  analogWrite(MOTOR_B_1, MOTOR_SPEED);
  analogWrite(MOTOR_B_2, 0);
  delay(500); // Adjust time as necessary
  Serial.println("Turning left.");
}

void turnRight() {
  // Turn the robot to the right
  analogWrite(MOTOR_A_1, MOTOR_SPEED);
  analogWrite(MOTOR_A_2, 0);
  analogWrite(MOTOR_B_1, 0);
  analogWrite(MOTOR_B_2, MOTOR_SPEED);
  delay(500); // Adjust time as necessary
  Serial.println("Turning right.");
}

This code is designed to be uploaded to an Arduino UNO microcontroller. It includes the necessary libraries and definitions for controlling the servo motor, reading from the ultrasonic sensor, and driving the DC motors through the L293D motor driver shield. The robot's behavior is to move forward until an obstacle is detected within 20 cm, at which point it will check for more space on the left or right and turn accordingly. If there is insufficient space, the robot will