This circuit is designed to control a robotic vehicle with obstacle avoidance capabilities. It uses an Arduino UNO as the central processing unit, interfacing with three HC-SR04 ultrasonic sensors for distance measurement. The Arduino controls two DC motors through an L298N DC motor driver, which allows for directional movement. The motors are powered by a 9V battery, and the Arduino is also connected to a separate 9V battery for its power requirements.
5V
pin is connected to the 5V power bus.GND
pin is connected to the ground bus.D3
, D4
, D5
, D6
, D8
, D9
, D10
, and D11
are used to interface with the L298N motor driver and HC-SR04 sensors.A2
, A3
, A4
, and A5
are used for additional sensor interfacing.+
pin of one battery is connected to the 12V
pin of the L298N motor driver.-
pin of the same battery is connected to the ground bus.+
pin of the other battery is connected to the Vin
pin of the Arduino UNO.-
pin of the other battery is connected to the ground bus.5V
pin is connected to the 5V power bus.GND
pin is connected to the ground bus.IN1
, IN2
, IN3
, IN4
, ENA
, and ENB
pins are connected to the corresponding digital pins on the Arduino UNO.OUT1
, OUT2
, OUT3
, and OUT4
pins are connected to the DC motors.OUT
pins of the L298N motor driver.VCC
pin is connected to the 5V power bus.GND
pin is connected to the ground bus.TRIG
and ECHO
pins are connected to the corresponding pins on the Arduino UNO.// Define sensor pins
const int trigPin1 = 11;
const int echoPin1 = 10;
const int trigPin2 = A3;
const int echoPin2 = A4;
const int trigPin3 = A2;
const int echoPin3 = A5;
// Define motor driver pins
const int in1 = 9;
const int in2 = 8;
const int in3 = 4;
const int in4 = 3;
const int enA = 5;
const int enB = 6;
// Define constants for PWM and distance threshold
#define PWM 200
#define DIS 25
void setup() {
// Initialize sensor pins
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);
// Initialize motor driver pins
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
}
void loop() {
// Obstacle avoidance logic
if (FrontSensor() < DIS && RightSensor() < DIS && LeftSensor() < DIS) {
// Obstacle in front of all 3 sides
turn_right();
delay(3000); // Reverse after turning
} else if (FrontSensor() < DIS && RightSensor() < DIS && LeftSensor() > DIS) {
// Obstacle on right and front sides
turn_left();
} else if (FrontSensor() < DIS && RightSensor() > DIS && LeftSensor() < DIS) {
// Obstacle on left and front sides
turn_right();
} else if (FrontSensor() < DIS && RightSensor() > DIS && LeftSensor() > DIS) {
// Obstacle on front side
turn_right();
} else if (FrontSensor() > DIS && RightSensor() > DIS && LeftSensor() < DIS) {
// Obstacle on left side
turn_right();
delay(180);
forward();
} else if (FrontSensor() > DIS && RightSensor() < DIS && LeftSensor() > DIS) {
// Obstacle on right side
turn_left();
delay(180);
forward();
} else {
// No obstacles detected
forward();
}
}
// Movement functions for the motors
void forward() {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enA, PWM);
analogWrite(enB, PWM);
}
void turn_left() {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
analogWrite(enA, PWM);
analogWrite(enB, PWM);
}
void turn_right() {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enA, PWM);
analogWrite(enB, PWM);
}
void reverse() {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
analogWrite(enA, PWM);
analogWrite(enB, PWM);
}
void stop() {
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
analogWrite(enA, LOW);
analogWrite(enB, LOW);
}
// Sensor functions to measure distance
long FrontSensor() {
long dur;
digitalWrite(trigPin1, LOW);
delayMicroseconds(5);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
dur = pulseIn(echoPin1, HIGH);
return (dur / 30); // Convert to cm
}
long RightSensor() {
long dur;
digitalWrite(trigPin2, LOW);
delayMicroseconds(5);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
dur = pulseIn(echoPin2, HIGH);
return (dur / 62); // Convert to cm
}
long LeftSensor() {
long dur;
digitalWrite(trigPin3, LOW);
delayMicroseconds(5);
digitalWrite(trigPin3, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin3, LOW);
dur = pulseIn(echoPin3, HIGH);
return (dur / 50); // Convert to cm
}
Note: The code provided has some typos and inconsistencies which have been corrected in the documentation. For example, int2
has been corrected to in2
, delat
to delay
, and Long
to long
. Additionally, the turn_left
function had a duplicate analogWrite(enA, PWM);
which has been corrected to analogWrite(enB, PWM);
. The pulseIn
function calls have been corrected to use the proper case for pin names, e.g., trigpin1
to trigPin1
.