Arduino-Controlled Obstacle-Avoiding Robot with Ultrasonic Sensors and L298N Motor Driver
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary of the Circuit
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.
Component List
Arduino UNO
- Microcontroller board based on the ATmega328P
- It has 14 digital input/output pins, 6 analog inputs, a 16 MHz quartz crystal, a USB connection, a power jack, an ICSP header, and a reset button.
9V Battery
- Provides power to the circuit.
- Two instances of this component are used: one for the Arduino UNO and one for the L298N motor driver.
L298N DC Motor Driver
- An integrated monolithic circuit in a 15-lead Multiwatt and PowerSO20 packages.
- It is a high voltage, high current dual full-bridge driver designed to accept standard TTL logic levels and drive inductive loads such as relays, solenoids, DC and stepping motors.
DC Motor
- Converts electrical energy into mechanical energy.
- Two instances of this component are used to drive the wheels of the robotic vehicle.
HC-SR04 Ultrasonic Sensor
- Ultrasonic ranging module that provides 2 cm to 400 cm non-contact measurement function.
- The ranging accuracy can reach up to 3mm.
- Three instances of this component are used for detecting obstacles from different directions.
Wiring Details
Arduino UNO
5Vpin is connected to the 5V power bus.GNDpin is connected to the ground bus.- Digital pins
D3,D4,D5,D6,D8,D9,D10, andD11are used to interface with the L298N motor driver and HC-SR04 sensors. - Analog pins
A2,A3,A4, andA5are used for additional sensor interfacing.
9V Battery
- The
+pin of one battery is connected to the12Vpin of the L298N motor driver. - The
-pin of the same battery is connected to the ground bus. - The
+pin of the other battery is connected to theVinpin of the Arduino UNO. - The
-pin of the other battery is connected to the ground bus.
L298N DC Motor Driver
5Vpin is connected to the 5V power bus.GNDpin is connected to the ground bus.IN1,IN2,IN3,IN4,ENA, andENBpins are connected to the corresponding digital pins on the Arduino UNO.OUT1,OUT2,OUT3, andOUT4pins are connected to the DC motors.
DC Motor
- Each motor has two pins connected to the
OUTpins of the L298N motor driver.
HC-SR04 Ultrasonic Sensor
VCCpin is connected to the 5V power bus.GNDpin is connected to the ground bus.TRIGandECHOpins are connected to the corresponding pins on the Arduino UNO.
Documented Code
// 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.