This circuit is designed to control a robotic vehicle with two DC motors, an ultrasonic sensor for distance measurement, two IR sensors for line following, and a servo motor for directional control. The brain of the vehicle is an Arduino UNO microcontroller, which interfaces with an L298N DC motor driver to control the motors' speed and direction. The ultrasonic sensor is used to detect obstacles, while the IR sensors are used to follow a line or path. A rocker switch is included to control the power supply to the circuit. The power is provided by a 2 x AA battery mount.
// Define L298N Motor Driver pins
#define enA 5
#define in1 7
#define in2 8
#define in3 9
#define in4 10
#define enB 6
// Define IR sensor pins
#define L_S A3 // Left IR sensor
#define R_S A4 // Right IR sensor
// Define Ultrasonic sensor pins
#define echo A2 // Echo pin
#define trigger A1 // Trigger pin
// Define Servo pin
#define servo 13
// Set distance for obstacle avoidance
int Set = 10;
int distance_L, distance_F, distance_R;
void setup() {
Serial.begin(9600); // Start serial communication at 9600bps
// Initialize IR sensors as inputs
pinMode(R_S, INPUT);
pinMode(L_S, INPUT);
// Initialize Ultrasonic sensor pins
pinMode(echo, INPUT);
pinMode(trigger, OUTPUT);
// Initialize L298N Motor Driver pins
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(enB, OUTPUT);
// Set initial motor speed
analogWrite(enA, 120);
analogWrite(enB, 120);
// Initialize Servo pin
pinMode(servo, OUTPUT);
// Sweep servo to check surroundings
sweepServo();
// Read distance from Ultrasonic sensor
distance_F = Ultrasonic_read();
delay(500);
}
void loop() {
// Read distance from Ultrasonic sensor
distance_F = Ultrasonic_read();
Serial.print("D F="); Serial.println(distance_F);
// Line following and obstacle avoiding logic
if ((digitalRead(R_S) == 1) && (digitalRead(L_S) == 1)) {
if (distance_F > Set) { forword(); }
else { Check_side(); }
}
else if ((digitalRead(R_S) == 1) && (digitalRead(L_S) == 0)) { turnRight(); }
else if ((digitalRead(R_S) == 0) && (digitalRead(L_S) == 1)) { turnLeft(); }
else if ((digitalRead(R_S) == 0) && (digitalRead(L_S) == 0)) { Stop(); }
delay(10);
}
// Function to control servo movement
void servoPulse(int pin, int angle) {
int pwm = (angle * 11) + 500; // Convert angle to microseconds
digitalWrite(pin, HIGH);
delayMicroseconds(pwm);
digitalWrite(pin, LOW);
delay(50); // Refresh cycle of servo
}
// Function to read distance from Ultrasonic sensor
long Ultrasonic_read() {
digitalWrite(trigger, LOW);
delayMicroseconds(2);
digitalWrite(trigger, HIGH);
delayMicroseconds(10);
long time = pulseIn(echo, HIGH);
return time / 29 / 2;
}
// Function to compare distances and decide direction
void compareDistance() {
if (distance_L > distance_R) {
turnLeft();
delay(200);
forword();
delay(700);
turnRight();
delay(500);
}
else {
turnRight();
delay(300);
forword();
delay(700);
turnLeft();
delay(500);
}
}
// Function to check side distances
void Check_side() {
Stop();
delay(100);
sweepServo();
compareDistance();
}
// Function to move forward
void forword() {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
// Function to turn right
void turnRight() {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}
// Function to turn left
void turnLeft() {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
// Function to stop the motors
void Stop() {
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
// Function to sweep the servo and read distances
void sweepServo() {
for (int angle = 70; angle <= 140; angle += 5) {
servoPulse(servo, angle);
}
delay(300);
distance_R = Ultrasonic_read();
Serial.print("D R