This circuit is designed to control a robotic vehicle with obstacle avoidance capabilities. It uses an Arduino UNO as the central microcontroller to process signals from three HC-SR04 ultrasonic sensors and drive two DC motors through an L298N motor driver. The ultrasonic sensors are used to detect obstacles from different directions (front, left, and right), and the Arduino UNO processes these signals to navigate the vehicle accordingly. The motors are powered by a 9V battery, which also supplies power to the motor driver, while the Arduino UNO and sensors are powered by a separate 9V battery.
// Define pins for ultrasonic sensors
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 pins for motor driver
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 ultrasonic 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 delay
} else if (FrontSensor() < DIS && RightSensor() < DIS && LeftSensor() > DIS) {
// Obstacle on right and front sides
turn_left(); // Turn left side
} else if (FrontSensor() < DIS && RightSensor() > DIS && LeftSensor() < DIS) {
// Obstacle on left and front sides
turn_right(); // Turn right side
} else if (FrontSensor() < DIS && RightSensor() > DIS && LeftSensor() > DIS) {
// Obstacle on front side
turn_right(); // Then turn right
} else if (FrontSensor() > DIS && RightSensor() > DIS && LeftSensor() < DIS) {
// Obstacle on left side
turn_right(); // Then turn right and then forward
delay(180);
forward();
} else if (FrontSensor() > DIS && RightSensor() < DIS && LeftSensor() > DIS) {
// Obstacle on right side
turn_left(); // Then turn left and then forward
delay(180);
forward();
} else {
forward(); // No obstacles, move 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 / 58); // Convert to centimeters
}
long RightSensor() {
long dur;
digitalWrite(trigPin2, LOW);
delayMicroseconds(5);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
dur = pulseIn(echoPin2, HIGH);
return (dur / 58); // Convert to centimeters
}
long LeftSensor() {
long dur;
digitalWrite(trigPin3, LOW);
delayMicroseconds(5);
digitalWrite(trigPin3, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin3, LOW);
dur = pulseIn(echoPin3, HIGH);
return (dur / 58); // Convert to centimeters
}
Note: The code provided has some typos and inconsistencies which have been corrected in the documented code above. The pulseIn
function returns the duration in microseconds, which is then converted to centimeters by dividing by 58, not 30, 62, or 50 as originally provided. The delay
function was misspelled as delat
, and int2
, int3
, and int4
were corrected to in2
, in3
, and in4
. The `Long