In this tutorial, we will learn how to make an Arduino obstacle-avoiding robot car. This robot, which is coded by Arduino using an ultrasonic sensor, is popular among many people. It is very simple and uses few modules. Even those who are new to Arduino coding can learn a lot from this obstacle-avoiding robot.
You can watch the following video or read the written tutorial below.
The New Update Version
The Old Version
From the above two videos, I recommend watching the New Update Version.
Overview
The Arduino obstacle-avoiding robot car is designed to detect obstacles in its path using an ultrasonic sensor, specifically the HC-SR04 model. The robot continuously navigates forward, and upon encountering an obstacle, it halts its movement. The system then utilizes a servo motor to pivot the ultrasonic sensor, allowing it to scan both sides and identify a clearer path with fewer obstacles. This dynamic obstacle avoidance mechanism enhances the robot’s adaptability, enabling it to navigate efficiently through environments with varying degrees of obstruction.
Components Needed
Before we begin, make sure you have gathered all the necessary components. The purchase links are in the description of my YouTube video.
- Arduino Uno or compatible board
- 4-wheel chassis kit with DC motors
- L298N motor driver module
- Ultrasonic sensor (HC-SR04)
- SG90 servo motor
- Jumper wires
- Two 18650 Rechargeable Battery 3.7V
- 18650 Li-ion Battery Holder 2-Way
- Toggle Switch
- USB cable for programming the Arduino
- Screwdriver and assorted screws
- Glue Gun
**Optional:** You can also include additional features like a smartphone holder or a camera mount for added functionality and fun.
Assemble the Chassis
How to assemble the 4 Wheel Smart Car Chassis kit is explained in the post I have presented before. This can be assembled very easily. The link of that article is mentioned below.
Link – How To Assemble 4WD Robot Smart Car Chassis Kits
Obstacle Avoiding Robot Circuit Diagram
The Obstacle Avoiding Robot Circuit Diagram is not complicated. First, you download the circuit diagram from the download button. It’s more obvious. Small jumpers should be connected to ENA and ENB on the L298N motor driver you are using. It is connected by default when you buy it. Never apply power by switching the VCC and GND of the ultrasonic sensor. If so, it may be damaged. Use two 3.7V 18659 li-ion batteries to provide power. Do not use more than two batteries. It cannot use 9V battery. If a 9V battery is used, only the ultrasonic sensor and servo motor will work. Gear motor not working. The reason for that is that the 9V battery does not have enough amperage.
Connecting the four-gear motors to the l298N Motor Driver.
The L298N features two H-bridges, each capable of controlling one motor. An H-bridge is a circuit that allows the motor to be driven in both directions (forward and reverse) by controlling the flow of current. The two terminals of each motor are connected to the output terminals of the L298N. Motors are typically connected to the OUT1/OUT2 terminals for one motor and OUT3/OUT4 for the other.
If you assembled the 4 Wheel Smart Car Chassis kit in the same way I did, connect the motor wires to the motor drive as mentioned below. If not, the direction of motor operation may change in some cases. It can be fixed by changing the motor connection wires of the motor drive. The two gear motors on the right are connected to OUT3 and OUT4 of the L298N Motor Driver. The two gear motors on the left are connected to OUT2 and OUT1 of the L298N Motor Driver.
In this way, connect the wires of the gear motors to the motor power connectors of the l298N motor driver.
- Connect the two right motor Red wires to the “OUT4” terminals on the L298N motor driver.
- Connect the two right motor Black wires to the “OUT3” terminals on the motor driver.
- Connect the two left motor Black wires to the “OUT2” terminals on the motor driver.
- Connect the two left motor Red wires to the “OUT1” terminals on the motor driver.
Connecting the four inputs of the L298N Motor Driver to the Arduino UNO board.
The control signals for each motor come from the Arduino or microcontroller. There are two input pins for each motor (IN1/IN2 for one motor and IN3/IN4 for the other). By applying different combinations of high and low signals to these pins, the Arduino controls the direction and speed of each motor.
The L298N has two enable pins (ENA and ENB) that allow the user to enable or disable the corresponding H-bridge. PWM (Pulse Width Modulation) signals can be applied to these pins to control the speed of the motors. Here I am not using ENA and ENB. It has a mini jumper connected to 5V.
In this way, connect the digital pins of the Arduino board and the inputs of the L298N motor driver by jumper wires.
- Connect the IN1 pin of the motor driver to digital pin D4 on the Arduino.
- Connect the IN2 pin of the motor driver to digital pin D5 on the Arduino.
- Connect the IN3 pin of the motor driver to digital pin D6 on the Arduino.
- Connect the IN4 pin of the motor driver to digital pin D7 on the Arduino.
Attaching the Sg90 Servo Motor to the chassis and connecting it to the Arduino board.
Attach the servo motor to the chassis using a glue gun or glue as shown in the image below.
In this way, connect the Arduino board and the SG90 Servo motor by jumper wires.
- Connect the Signal (Orange Wire) from the servo motor to a digital pin D10 on the Arduino.
- Connect the VCC (Red Wire) from the servo motor to the 5V pin on the Arduino.
- Connect the GND (Brown Wire) from the servo motor to the GND pin on the Arduino.
Connecting the Ultrasonic sensor (HC-SR04) with the Arduino board
In this way connect the ultrasonic sensor (HC-SR04) with the Arduino board,
- Connect the VCC (power) pin of the ultrasonic sensor to the 5V pin on the Arduino.
- Connect the GND (ground) pin of the sensor to the GND pin on the Arduino.
- Connect the TRIG (trigger) pin of the ultrasonic sensor to Analog pin A1 on the Arduino.
- Connect the ECHO pin of the sensor to another Analog pin A2 on the Arduino.
Connect the Battery Power Connection
Use two 3.7V 18659 li-ion batteries to provide power. Do not use more than two batteries. It cannot use 9V battery. If a 9V battery is used, only the ultrasonic sensor and servo motor will work. Gear motor not working. The reason for that is that the 9V battery does not have enough amperage.
First solder the switch to the battery holder. Solder the red wire of the battery holder to one end of the switch and solder a red wire to the other end of the switch. Stick the switch to the battery holder with the glue gun.
- Connect the Red wire (+) of the battery holder to the 12V In of the L298N Motor Driver and to the VIN Pin of the Arduino board.
- Connect the Black wire (-) of the battery holder to the GND In of the L298N Motor Driver and to the GND Pin of the Arduino board.
Upload the Arduino Sketch
Before uploading the code, download the New Ping Library and add it to the Arduino IDE. The Newping Library should be added to the Library of the Arduino IDE. Otherwise the code cannot be uploaded. Download from below.
The file type of the Newping Library you downloaded is a zip file. Follow the steps below and add the Zip file to the Arduino IDE.
In the Arduino IDE, navigate to Sketch > Include Library > Add . ZIP Library. At the top of the drop-down list, select the option to “Add . ZIP Library”.
After that, Select the board and port and upload the code. If this is difficult to do, watch a tutorial video.
You can download the Arduino code and open it directly through the Arduino IDE. Click the Download button below to download the Arduino code
#include <Servo.h> //Servo motor library. This is standard library //Robot Lk
#include <NewPing.h> //Ultrasonic sensor function library. You must install this library //Robot Lk
//Robot Lk YouTube Channel-https://www.youtube.com/c/RobotLk
//our L298N control pins
const int LeftMotorForward = 7;
const int LeftMotorBackward = 6;
const int RightMotorForward = 5;
const int RightMotorBackward = 4;
//LED Robot Lk
const int ForwardLED = 8;
const int BackwardLED = 9;
const int LeftLED = 11;
const int RightLED = 12;
const int LeftSensorLED = 13;
const int RightSensorLED = 3;
//sensor pins
#define trig_pin A1 //analog input 1
#define echo_pin A2 //analog input 2
#define maximum_distance 200
boolean goesForward = false;
int distance = 100;
NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
Servo servo_motor; //our servo name
//Robot Lk
void setup(){
pinMode(RightMotorForward, OUTPUT);
pinMode(LeftMotorForward, OUTPUT);
pinMode(LeftMotorBackward, OUTPUT);
pinMode(RightMotorBackward, OUTPUT);
pinMode(ForwardLED, OUTPUT);//Robot Lk
pinMode(BackwardLED, OUTPUT);//Robot Lk
pinMode(LeftLED, OUTPUT);//Robot Lk
pinMode( RightLED, OUTPUT);
pinMode(LeftSensorLED, OUTPUT);
pinMode( RightSensorLED, OUTPUT);
servo_motor.attach(10); //our servo pin
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 <= 45){
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(){
digitalWrite(RightSensorLED, HIGH);
delay(200);
digitalWrite(RightSensorLED, LOW);
servo_motor.write(50);
delay(500);
int distance = readPing();
delay(100);
servo_motor.write(115);
return distance;
}
int lookLeft(){
digitalWrite(LeftSensorLED, HIGH);//Robot Lk
delay(500);
digitalWrite(LeftSensorLED, LOW);//Robot Lk
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=250;
}
return cm;
}
void moveStop(){
digitalWrite(RightMotorForward, LOW);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(ForwardLED, LOW);
digitalWrite(BackwardLED, LOW);
digitalWrite(LeftLED, LOW);
digitalWrite(RightLED, LOW);
digitalWrite(LeftSensorLED, LOW);
digitalWrite(RightSensorLED, LOW);
}
void moveForward(){
if(!goesForward){
goesForward=true;
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(ForwardLED, HIGH);//Robot Lk//Robot Lk
}
}
void moveBackward(){
goesForward=false;
digitalWrite(LeftMotorBackward, HIGH);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorForward, LOW);
digitalWrite(BackwardLED, HIGH);//Robot Lk
}
void turnRight(){
digitalWrite(RightLED, HIGH);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorForward, LOW);
delay(250);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
}
void turnLeft(){
digitalWrite(LeftLED, HIGH);
digitalWrite(LeftMotorBackward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
delay(250);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
}
Code language: Arduino (arduino)
This code uses a servo motor to scan for obstacles. When an obstacle is detected within 45 cm, the robot stops, rotates the servo to find a clear path, and then moves forward.
Conclusion
Finally, the Arduino Obstacle Avoiding Robot Car presented in this tutorial would be great for a variety of school projects. As I explained in this article, it can be easily made by anyone interested in Arduino. Try making it in different ways. For example, make an obstacle-avoiding robot using several ultrasonic sensors.