Obstacle Detector Robot
An Obstacle Avoidance Robot is an intelligent robot, that can automatically sense and overcome obstacles on its path. It contains an Arduino Uno to process the data, and Ultrasonic sensors to detect the obstacles on its path.

Overview
Arduino is the main controlling unit of the robot. Out of the 19 available digital and analog I/O pins, 7 pins are used in this project design.
The ultrasonic sensor have 4 pins: Vcc, Trig, Echo and Gnd. Vcc and Gnd are connected to the +5v and GND pins of the Arduino. Trig (Trigger) is connected to the analog pin A1 and Echo is connected to analog pin A2 of the Arduino UNO respectively.
A Servo Motor is used to rotate the Ultrasonic Sensor to scan for obstacles. It has three pins namely Control, VCC and GND. The Servo Control Pin is connected to pin 10 of Arduino while the VCC and GND are connected to +5V and GND.
L298N module has two screw terminal blocks for the motor A and B, and another screw terminal block for the Ground pin, the VCC for motor and a 5V pin which can either be an input or output. The Input 1 and Input 2 pins are used for controlling the rotation direction of the motor A, and the inputs 3 and 4 for the motor B. If input 1 is LOW and input 2 is HIGH the motor will move forward, and vice versa, if input 1 is HIGH and input 2 is LOW the motor will move backward. In case both inputs are same, either LOW or HIGH the motor will stop. The same applies for the inputs 3 and 4 and the motor B.
About Component
Arduino -
Arduino Uno is an ATmega 328p Microcontroller based prototyping board. It is an open source electronic prototyping platform that can be used with various sensors and actuators.
Out of the 19 available digital and analog I/O pins, 7 pins are used in this project design.


HC – SR04
It is an Ultrasonic Range Finder Sensor. It is a non-contact based distance measurement system and can measure distance of 2cm to 25 cm.


L298N Motor Driver
L298N Based Motor Driver Module is a high power motor driver perfect for driving DC Motors and Stepper Motors. It uses the popular L298 motor driver IC and has the onboard 5V regulator which it can supply to an external circuit. It can control up to 4 DC motors, or 2 DC motors with directional and speed control


Servo Motor
The Tower Pro SG90 is a simple Servo Motor which can rotate 90 degrees in each direction (approximately 180 degrees in total)


Components Needed for this Arduino Project
- Motor Chasis
- Arduino Uno
- Ultrasonic Sensor HC – SR04
- L298N Motor Driver
- Servo Motor SG90
- Male and Female jumper wire
- Glue Gun(Stick)
Necessary Tools And Machines
- Soldering iron
- Hot Glue Gun
Circuit Schematics

Building Connection
Step 1:
- The robot chassis needs to be assembled by looking at the images. Bare minimum you should have the chassis base assembled with motors and wheels
- Cut 8 pieces of connecting wires each of length 15-20 cm and strip out both ends of the wire 1-2 cm
- Take 4 wires & connect right side of the motor (front and rear). Repeat the same with other 4 wires using left side of the motor (front and rear).
- Take out a battery (it will be in basic electronics kit) and test motor wheel rotation by connecting other ends of the wires to battery






Step 2 Arduino
First of all, connect the Arduino board with laptop or computer.

Step 3
Install the Arduino software on the computer.

Step 4
Open the Arduino software. The following screen will appear.

Step 5
You can see the Main page of the software.

Step 6
Then, open the new page on the software.
Then select the File→ New.

A new page opens on the Arduino software.

Step 7
I have given a source code that you can use or you can use your own source code.
Then, select File → open → select written program →open the program.

Open the program folder.

Select the program and open it.

Step 8
The obstacle avoiding robot program coding will be there already. The value of distance for the ultrasonic sensors to detect the collision is set in the program as 30 cm.

Step 10
Click TOOL- Select BOARD – then press ARDUINO/GENUINO UNO R3.

Again Select TOOL >> Select a PORT >> COM3 ARDUINO UNO

Step 11
Verify the program or compile the program.

Step 12
Connect the hardware as per the connection diagram given for the Obstacle avoiding robot.

Step 13
Then connect the Arduino to the laptop and upload the code in Arduino.

Step 14
Then the obstacle avoiding robot will start working.

The ultrasonic sensor detects the collision using Arduino.
Interpretation of Code
#include <Servo.h>
#include <NewPing.h>
const int LeftMotorForward = 7;
const int LeftMotorBackward = 6;
const int RightMotorForward = 5;
const int RightMotorBackward = 4;
#include <Servo.h> This library allows an Arduino board to control RC (hobby) servo motors. Servo have integrated gears and a shaft that can be precisely controlled. Standard servos allow the shaft to be positioned at various angles, usually between 0 and 180 degrees
#include<NewPing.h> This library allows an ultrasonic sensor to receive and transmit obstacle signal .
The below code shows the connection between the Arduino and motor driver L298N control pins.
const int LeftMotorForward = 7;
const int LeftMotorBackward = 6;
const int RightMotorForward = 5;
const int RightMotorBackward = 4;

#define trig_pin A1
#define echo_pin A2
This sensor includes four pins and the pin configuration of this sensor is discussed below.
- Pin Vcc : This pin provides a +5V power supply to the sensor.
- Pin Trig: This is an input pin, used to initialize measurement by transmitting ultrasonic waves by keeping this pin high for 10us.
- Pin Echo: This is an output pin, which goes high for a specific time period and it will be equivalent to the duration of the time for the wave to return back to the sensor.
- Pin GND: This is a ground pin used to connect to the GND of the system.
Source Code
#include <Servo.h>
#include <NewPing.h>
const int LeftMotorForward = 7;
const int LeftMotorBackward = 6;
const int RightMotorForward = 5;
const int RightMotorBackward = 4;
#define trig_pin A1
#define echo_pin A2
#define maximum_distance 200
boolean goesForward = false;
int distance = 100;
NewPing sonar(trig_pin, echo_pin, maximum_distance);
Servo servo_motor;
void setup(){
pinMode(RightMotorForward, OUTPUT);
pinMode(LeftMotorForward, OUTPUT);
pinMode(LeftMotorBackward, OUTPUT);
pinMode(RightMotorBackward, OUTPUT);
servo_motor.attach(10);
servo_motor.write(115);
delay(2000);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
}
void loop(){
int distanceRight = 0;
int distanceLeft = 0;
delay(50);
if (distance <= 20){
moveStop();
delay(300);
moveBackward();
delay(400);
moveStop();
delay(300);
distanceRight = lookRight();
delay(300);
distanceLeft = lookLeft();
delay(300);
if (distance >= distanceLeft){
turnRight();
moveStop();
}
else{
turnLeft();
moveStop();
}
}
else{
moveForward();
}
distance = readPing();
}
int lookRight(){
servo_motor.write(50);
delay(500);
int distance = readPing();
delay(100);
servo_motor.write(115);
return distance;
}
int lookLeft(){
servo_motor.write(170);
delay(500);
int distance = readPing();
delay(100);
servo_motor.write(115);
return distance;
delay(100);
}
int readPing(){
delay(70);
int cm = sonar.ping_cm();
if (cm==0){
cm=30;
}
return cm;
}
void moveStop(){
digitalWrite(RightMotorForward, LOW);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(LeftMotorBackward, LOW);
}
void moveForward(){
if(!goesForward){
goesForward=true;
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
}
}
void moveBackward(){
goesForward=false;
digitalWrite(LeftMotorBackward, HIGH);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorForward, LOW);
}
void turnRight(){
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorForward, LOW);
delay(500);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
}
void turnLeft(){
digitalWrite(LeftMotorBackward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
delay(500);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
}