In this tutorial, we will learn how to make an Arduino obstacle-avoiding robot using an L293D motor shield. In an earlier article, I taught how to make an Arduino Obstacle Avoiding Robot using an L298N motor driver. This robot, which is coded by Arduino using an ultrasonic sensor, is popular among many people. It is very simple and uses a few modules. Even those who are new to Arduino coding can learn a lot from this obstacle-avoiding robot. The specialty here is the use of an L293D Motor Shield.
You can watch the following video or read the written tutorial below.
Overview
In this robot car, the task is to detect obstacles in front and avoid them. This process occurs continuously. Ultrasonic sensor (HC-SR04) is used to detect the obstacle in front. If the robot car encounters an obstacle while moving forward, it stops and uses the ultrasonic sensor attached to the servo motor to monitor both sides. It then proceeds along the path with fewer obstacles. The specialty here lies in the use of an L293D Motor Shield, making it very easy to build and wire.
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
- 4-wheel chassis kit with DC motors
- L293D motor Shield 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
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 ( L293D Motor Shield )
The Obstacle Avoiding Robot Circuit Diagram is not complicated. First, you download the circuit diagram from the download button. It’s more obvious. 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.
Connect the L293D Motor Shield to the Arduino UNO board
The L293D motor shield should be connected on the Arduino UNO board. ensuring proper alignment with the pins. Check if the pins of the motor shield are properly connected.
Connect the Motors to the L293D Motor Shield
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 shield.
- Connect the front right motor Red wire to the “M1+” terminals on the motor shield.
- Connect the front right motor Black wire to the “M1-” terminals on the motor shield.
- Connect the front left motor Red wire to the “M2+” terminals on the motor shield.
- Connect the front left motor Black wire to the “M2-” terminals on the motor shield.
- Connect the back left motor Red wire to the “M3+” terminals on the motor shield.
- Connect the back left motor Black wire to the “M3-” terminals on the motor shield.
- Connect the back right motor Red wire to the “M4+” terminals on the motor shield.
- Connect the back right motor Black wire to the “M4-” terminals on the motor shield.
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(+) coming through the switch to the M(+) in the L293D Motor Shield.
- Connect the black wire(-) of the battery holder to the GND(-) of the L293D Motor Shield.
Attaching the Sg90 Servo Motor to the chassis and connecting it to the Arduino board.
The SG90 is a popular and widely used micro servo motor, often employed in robotics and other electronic projects. The SG90 servo motor consists of a small DC motor, a set of gears, a control circuit, and a potentiometer. Attach the servo motor to the chassis using a glue gun.
In this way, connect the SG90 Servo motor to the L293D motor shield.
- GND (Brown) to (-) on SER1 connector.
- VCC (Red Wire) to (+) on SER1 connector.
- Signal (Orange Wire) to a digital pin S on SER1 connector.
Connect the Ultrasonic sensor to the L293D Motor Shield
The HC-SR04 Ultrasonic Sensor is a popular module used for distance measurement in various electronic projects. First, glue the servo horn to the ultrasonic sensor using a glue gun. VCC to SERVO_2 (+) pin on L293D motor shield. Next, connect the ultrasonic sensor to the Arduino Uno with the following connections. The + and – pins of the Servo 2 connector is used to power the ultrasonic sensor. The Trig and Echo pins should be connected to the analog pins of the Arduino board.
In this way connect the ultrasonic sensor (HC-SR04) with the L293D Motor shield,
- GND to SERVO_2 (-) pin on L293D motor shield.
- TRIG to an Analog pin A0 on L293D motor shield.
- ECHO to another Analog pin A1 on L293D motor shield.
Upload the Arduino Sketch
Download Newping Library and Adafruit Motor Shield Library before uploading the code. Download from below. The file type of the Newping Library and Adafruit Motor Shield 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, copy the following Arduino code and paste it into the new sketch in the Arduino IDE. Select 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 <AFMotor.h>
#include <NewPing.h>
#include <Servo.h>
#define TRIG_PIN A0
#define ECHO_PIN A1
#define MAX_DISTANCE 200
#define MAX_SPEED 190 // sets speed of DC motors
#define MAX_SPEED_OFFSET 20
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);
AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor2(2, MOTOR12_1KHZ);
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);
Servo myservo;
boolean goesForward=false;
int distance = 100;
int speedSet = 0;
void setup() {
myservo.attach(10);
myservo.write(115);
delay(2000);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
}
void loop() {
int distanceR = 0;
int distanceL = 0;
delay(40);
if(distance<=30)
{
moveStop();
delay(100);
moveBackward();
delay(300);
moveStop();
delay(200);
distanceR = lookRight();
delay(200);
distanceL = lookLeft();
delay(200);
if(distanceR>=distanceL)
{
turnRight();
moveStop();
}else
{
turnLeft();
moveStop();
}
}else
{
moveForward();
}
distance = readPing();
}
int lookRight()
{
myservo.write(50);
delay(500);
int distance = readPing();
delay(100);
myservo.write(115);
return distance;
}
int lookLeft()
{
myservo.write(170);
delay(500);
int distance = readPing();
delay(100);
myservo.write(115);
return distance;
delay(100);
}
int readPing() {
delay(70);
int cm = sonar.ping_cm();
if(cm==0)
{
cm = 250;
}
return cm;
}
void moveStop() {
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void moveForward() {
if(!goesForward)
{
goesForward=true;
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly
{
motor1.setSpeed(speedSet);
motor2.setSpeed(speedSet);
motor3.setSpeed(speedSet);
motor4.setSpeed(speedSet);
delay(5);
}
}
}
void moveBackward() {
goesForward=false;
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly
{
motor1.setSpeed(speedSet);
motor2.setSpeed(speedSet);
motor3.setSpeed(speedSet);
motor4.setSpeed(speedSet);
delay(5);
}
}
void turnRight() {
motor1.run(BACKWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(BACKWARD);
delay(500);
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
void turnLeft() {
motor1.run(FORWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(FORWARD);
delay(500);
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
Code language: PHP (php)
This code uses a servo motor to scan for obstacles. When an obstacle is detected within 30 cm, the robot stops, rotates the servo to find a clear path, and then moves forward.
Install the ultrasonic sensor to the servo motor
Test Your Robot
Power up your robot car and test it in an open area. The ultrasonic sensor will detect obstacles, and your robot will avoid them by scanning the surroundings and finding a clear path.
Conclusion
In conclusion, the presented obstacle-avoiding robot car serves as an ideal platform for school projects, offering a straightforward yet versatile foundation for learners. While the current design is accessible and user-friendly, it also holds the potential for more intricate robotic projects. The modular nature of the robot, incorporating the L293D Motor Shield, ultrasonic sensor, and servo motor, allows for scalability and customization based on individual preferences and project requirements.