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

The circuit in question appears to be designed for a mobile robot platform equipped with an Arduino UNO as the main controller, an ultrasonic sensor for distance measurement, DC gearmotors for movement, a servomotor for directional control, an L298N motor driver for controlling the gearmotors, a power supply consisting of two 18650 batteries, and a rocker switch for power management. The Arduino UNO is extended with a Sensor Shield v5.0 to facilitate easy connections to various sensors and actuators.

Component List

Arduino UNO

HC-SR04 Ultrasonic Sensor

Gearmotor DC Wheels (Left and Right)

Servomotor SG90

L298N DC Motor Driver

Rocker Switch (SPST)

Arduino Sensor Shield v5.0

2x 18650 Batteries

Wiring Details

Arduino UNO

HC-SR04 Ultrasonic Sensor

Gearmotors (Left and Right)

Servomotor SG90

L298N DC Motor Driver

Rocker Switch (SPST)

2x 18650 Batteries

Documented Code

#include <Servo.h>

volatile int IN1 = 11;
volatile int IN2 = 6;
volatile int IN3 = 5;
volatile int IN4 = 3;
volatile int rightDistance;
volatile int leftDistance;
Servo servo_4;

void left_side_forward(int speed) {
  analogWrite(IN1, speed * 0.92);
  analogWrite(IN2, 0);
}

void right_side_forward(int speed) {
  analogWrite(IN3, speed);
  analogWrite(IN4, 0);
}

void left_side_backward(int speed) {
  analogWrite(IN1, 0);
  analogWrite(IN2, speed * 0.92);
}

void right_side_backward(int speed) {
  analogWrite(IN3, 0);
  analogWrite(IN4, speed);
}

float checkdistance_A1_A2() {
  digitalWrite(A1, LOW);
  delayMicroseconds(2);
  digitalWrite(A1, HIGH);
  delayMicroseconds(10);
  digitalWrite(A1, LOW);
  float distance = pulseIn(A2, HIGH) / 58.00;
  delay(10);
  return distance;
}

void forward(int speed) {
  left_side_forward(speed);
  right_side_forward(speed);
}

void backward(int speed) {
  left_side_backward(speed);
  right_side_backward(speed);
}

void left(int speed, int time) {
  left_side_backward(speed);
  right_side_forward(speed);
  delay(time);
  stop();
}

void right(int speed, int time) {
  left_side_forward(speed);
  right_side_backward(speed);
  delay(time);
  stop();
}

void stop() {
  left_side_forward(0);
  right_side_forward(0);
}

void setup() {
  servo_4.attach(4);
  pinMode(A1, OUTPUT);
  pinMode(A2, INPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
}

void loop() {
  servo_4.write(90);
  delay(200); // Reduce delay for better responsiveness

  float distance = checkdistance_A1_A2();
  while (distance > 15) {
    forward(150); // Increase speed for faster response
    distance = checkdistance_A1_A2(); // Continuously update distance
  }
  stop();
  delay(100); // Reduce delay for better responsiveness

  servo_4.write(0);
  delay(200); // Reduce delay for better responsiveness
  rightDistance = checkdistance_A1_A2();

  servo_4.write(180);
  delay(200); // Reduce delay for better responsiveness
  leftDistance = checkdistance_A1_A2();

  if (rightDistance >= leftDistance) {
    right(200, 300); // Reduce rotation time for more precision
  } else {
    left(200, 300); // Reduce rotation time for more precision
  }
}

This code is designed to control a robot with differential steering. It uses an ultrasonic sensor to avoid obstacles by checking distances and steering the robot accordingly. The servomotor is used to change the direction of the ultrasonic sensor for scanning the environment. The L298N motor driver controls the gearmotors for movement. The code includes functions for moving forward, backward, stopping, and turning left or right. The setup function initializes the pins, and the loop function contains the main logic for obstacle avoidance and movement.