Arduino Obstacle Avoidance Line Follower Robot Pro
Arduino Obstacle Avoidance Line Follower Robot Pro
by MrElectroUino
Arduino Projects: I will teach you how to make an obstacle avoidance line follower robot with Arduino. The Arduino
smart car robot can follow the line with an IR sensor and obstacle avoidance with an ultrasonic sensor.
Supplies:
Do the same for other IR sensor but make sure that OUT pin is connected to motor driver A1.
Now after Doing all the connections, it's time to upload the code.
Connect the Arduino uno to pc via USB cable and open the Arduino IDE, select the Arduino board, and com port from
the tool menu after that upload the given code.
#include <NewPing.h>
#include <Servo.h>
#include <AFMotor.h>
//hc-sr04 sensor
#define TRIGGER_PIN A2
#define ECHO_PIN A3
#define max_distance 50
//ir sensor
#define irLeft A0
#define irRight A1
//motor
#define MAX_SPEED_OFFSET 20
Servo servo;
int distance = 0;
int leftDistance;
int rightDistance;
boolean object;
void setup() {
Serial.begin(9600);
pinMode(irLeft, INPUT);
pinMode(irRight, INPUT);
servo.attach(10);
servo.write(90);
motor1.setSpeed(120);
motor2.setSpeed(120);
motor3.setSpeed(120);
motor4.setSpeed(120);
void loop() {
objectAvoid();
//forword
objectAvoid();
Serial.println("TL");
//leftturn
moveLeft();
Arduino Obstacle Avoidance Line Follower Robot Projects 2021: Page 4
moveLeft();
objectAvoid();
Serial.println("TR");
//rightturn
moveRight();
//Stop
Stop();
void objectAvoid() {
distance = getDistance();
//stop
Stop();
Serial.println("Stop");
lookLeft();
lookRight();
delay(100);
//left
object = true;
turn();
Serial.println("moveLeft");
} else {
//right
object = false;
turn();
Serial.println("moveRight");
delay(100);
//forword
Serial.println("moveforword");
moveForward();
int getDistance() {
delay(50);
int cm = sonar.ping_cm();
if (cm == 0) {
cm = 100;
return cm;
int lookLeft () {
//lock left
servo.write(150);
delay(500);
leftDistance = getDistance();
delay(100);
servo.write(90);
Serial.print("Left:");
Serial.print(leftDistance);
return leftDistance;
delay(100);
int lookRight() {
//lock right
servo.write(30);
delay(500);
rightDistance = getDistance();
delay(100);
servo.write(90);
Serial.print("Right:");
Serial.println(rightDistance);
return rightDistance;
delay(100);
void Stop() {
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
void moveForward() {
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
void moveBackward() {
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
void turn() {
if (object == false) {
Serial.println("turn Right");
moveLeft();
delay(700);
moveForward();
delay(800);
moveRight();
delay(900);
if (digitalRead(irRight) == 1) {
loop();
} else {
moveForward();
Arduino Obstacle Avoidance Line Follower Robot Projects 2021: Page 7
moveForward();
else {
Serial.println("turn left");
moveRight();
delay(700);
moveForward();
delay(800);
moveLeft();
delay(900);
if (digitalRead(irLeft) == 1) {
loop();
} else {
moveForward();
void moveRight() {
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
void moveLeft() {
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
2. Calibrating the both IR sensor by rotating it potentiometer for tracking the black line watch the video if you getting any
problem.
3. Connect the 9v battery and enjoy the Arduino obstacle avoidance line follower robot car.
https://www.youtube.com/watch?v=ZiqAyuLpS3o
If you like the projects keep support me by subscribing to youtube channel, like, share and comment any doubt.
Topic :)
arduino self driving car 2021 arduino obstacle avoiding car arduino line follower with obstacle avoidance code arduino
line follow robot 2021 arduino make your own tesla