Arduino UNO Controlled Line Follower and Obstacle Avoiding Robot with IR and Ultrasonic Sensors
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to control a robotic system that includes motors, sensors, and a servo. The core of the circuit is an Arduino UNO microcontroller, which interfaces with IR sensors, an ultrasonic sensor, a servo motor, and a DC motor driver that controls four gearmotors. A 4 x AAA battery mount provides power to the system, which is controlled by a rocker switch. The Arduino UNO manages sensor inputs and motor outputs to navigate the robot based on sensor feedback.
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.
4 x AAA Battery Mount
- A battery holder for four AAA batteries
- Provides power to the circuit.
Rocker Switch
- A simple on/off switch
- Controls the power flow from the battery to the circuit.
L298N DC Motor Driver
- A dual H-bridge motor driver
- Allows for the control of two DC motors or one stepper motor.
Gearmotor DC / Motorreductor
- A DC gearmotor
- Provides mechanical motion for the robot.
Servo
- A rotary actuator or linear actuator that allows for precise control of angular or linear position.
- Used for steering or other controlled motion tasks.
IR Sensor
- An infrared sensor capable of detecting obstacles and lines.
- Used for navigation and obstacle avoidance.
HC-SR04 Ultrasonic Sensor
- An ultrasonic ranging module
- Provides non-contact distance measurements.
Wiring Details
Arduino UNO
5VandGNDare connected to the power rails to provide power to the IR sensors, servo, and ultrasonic sensor.Vinis connected to the output of the rocker switch to receive power from the battery mount.A0is connected to the output of one IR sensor.A1is connected to the output of another IR sensor.A2is connected to theECHOpin of the ultrasonic sensor.A3is connected to theTRIGpin of the ultrasonic sensor.A5is connected to thepulsepin of the servo.- Digital pins
D10,D9,D8,D7,D6, andD5are connected to theENA,IN1,IN2,IN3,IN4, andENBpins of the L298N motor driver, respectively.
4 x AAA Battery Mount
+is connected to theinputof the rocker switch.-is connected to the common ground rail.
Rocker Switch
outputis connected to theVinof the Arduino UNO and12Vof the L298N motor driver.
L298N DC Motor Driver
12VandGNDare connected to the power supply through the rocker switch.5Vis not used in this configuration.OUT1andOUT2are connected to two gearmotors.OUT3andOUT4are connected to the other two gearmotors.ENA,IN1,IN2,IN3,IN4, andENBare controlled by the Arduino UNO.
Gearmotors
- Each gearmotor has two pins (
Pin1andPin2) connected to the correspondingOUTpins of the L298N motor driver.
Servo
gndandvccare connected to the common ground and power rails, respectively.pulseis controlled by the Arduino UNO.
IR Sensors
- Both
gndandvccare connected to the common ground and power rails, respectively. - The
outpins are connected to the Arduino UNO for signal processing.
HC-SR04 Ultrasonic Sensor
GNDandVCCare connected to the common ground and power rails, respectively.TRIGandECHOare controlled by the Arduino UNO.
Documented Code
#define enA 10 // Enable1 L298 Pin enA
#define in1 9 // Motor1 L298 Pin in1
#define in2 8 // Motor1 L298 Pin in2
#define in3 7 // Motor2 L298 Pin in3
#define in4 6 // Motor2 L298 Pin in4
#define enB 5 // Enable2 L298 Pin enB
#define L_S A0 // IR sensor Left
#define R_S A1 // IR sensor Right
#define echo A2 // Echo pin
#define trigger A3 // Trigger pin
#define servo A5 // Servo pin
int Set = 15; // Threshold distance for obstacle avoidance
int distance_L, distance_F, distance_R; // Distances from sensors
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 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, 200);
analogWrite(enB, 200);
// Initialize servo pin
pinMode(servo, OUTPUT);
// Sweep servo to initial positions
sweepServo();
// Read initial distance from ultrasonic sensor
distance_F = Ultrasonic_read();
delay(500);
}
void loop() {
// Read distances from sensors
distance_F = Ultrasonic_read();
Serial.print("D F="); Serial.println(distance_F);
// Line follower and obstacle avoiding logic
if ((digitalRead(R_S) == 0) && (digitalRead(L_S) == 0)) {
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();
}
delay(10);
}
// Function to pulse the servo to a given angle
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; // Convert time to distance
}
// Function to compare distances and decide direction
void compareDistance() {
if (distance_L > distance_R) {
turnLeft();
delay(500);
forword();
delay(600);
turnRight();
delay(500);
forword();
delay(600);
turnRight();
delay(400);
} else {
turnRight();
delay(500);
forword();
delay(600);
turnLeft();
delay(500);
forword();
delay(600);
turnLeft();
delay(400);
}
}
// Function to check side distances and decide next move
void Check_side() {
Stop();
delay(100);
sweepServo();
delay(300);
distance_R = Ultrasonic_read();
Serial.print("D R="); Serial.println(distance_R);
delay(100);
sweepServo();
delay(500);
distance_L = Ultrasonic_read();
Serial.print("D L="); Serial.println(distance_L);
delay(100);
sweepServo();
delay(300);
compareDistance();
}
// Function to move forward
void forword() {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
// Function to move backward
void backword() {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}
// 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 all motors
void Stop() {
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in