Arduino Projects

Obstacle Avoiding Robot using Arduino and Ultrasonic Sensor

Arduino based Obstacle Avoiding Robot using Ultrasonic Sensor

Overview: Obstacle Avoiding Robot using Arduino

In this project, we’ll combine an ultrasonic sensor with two DC motors and a servomotor to create a simple obstacle-avoiding robot using Arduino. The ultrasonic sensor sends and receives a signal to determine the distance of an object. If there is an object less than 15 centimeters away, the robot will stop. Then it looks around, turns toward a direction in which it doesn’t sense anything, and move in that direction. The ultrasonic sensor is mounted on a servo motor so that the robot can move and search for a clear path.

Obstacle Avoiding Robot using Arduino UNO

For more on how the HC-SR04 ultrasonic sensor works, see this Project. The L293d motor shield fits on top of the Arduino and controls the DC motors using the Adafruit Motor Shield library.


Components Required for Obstacle Avoiding Robot

The key components of the obstacle-avoiding robot are the HC-SR04 ultrasonic sensor, L293d motor shield, and motors. The motors I used here are purchased as part of a kit. You search online for an “Arduino robot kit,” you will be able to find a kit that contains the motors and wheels, base, battery pack, center wheel, and fittings needed. The one I bought is called the “2WD Smart Motor Robot Car Chassis Kit for Arduino“. Also, you can buy them from the Amazon links provided below.

S.NComponents NameQuantityGet Products from Amazon
1Arduino UNO1https://amzn.to/3HVcuTF
2L293D Motor Shield1https://amzn.to/3FIyZcL
3DC motors and wheels2https://amzn.to/3DT2xnk
4HC-SR04 ultrasonic sensor1https://amzn.to/3CNafOx
5Tower Pro SG90 9g servomotor1https://amzn.to/3xon0yc
6Few jumpers wires20https://amzn.to/3klh0A4
73 wheel car chassis combo 1https://amzn.to/3nQxGSV
89V AA battery pack1https://amzn.to/3p2igtY
Components Required for Obstacle Avoiding Robot

Hardware Assembly

  • You will need to solder wires to the DC motors as shown in below image. Solder the red, positive power wire to the left pin of one DC motor and the black ground wire to the right pin. Now reverse this order for the other motor. DC motors do not have polarity. So it doesn’t matter which way you hold the motors to determine which is left and right. But power and GND need to be in opposite positions on the motors so the direction of the revolution will be the same.

Note: Solder the red, positive power wire to the left pin of one DC motor, and the black ground wire to the right pin. Reverse this order for the other motor.

Soldering DC Motor
  • Attach the single wheel to the front of the robot base and the two rear wheels to the back using the screws and fittings provided. The underside of the robot should resemble.
Assemble Obstacle avoiding robot
  • Now you need the L293d motor shield. We’ll solder some wires to it to control the ultrasonic sensor.

We’ll solder four wires to the pins highlighted in the image.

L293d motor shield
  • Take four female jumper wires and strip about 5 millimeters from one end of each, as shown in image below. Strip the ends of four female jumper wires to solder onto the motor shield.
Strip the jumper cables
  • Solder the stripped ends to the highlighted pins on the motor shield, as shown in Figure 22-6. This can be tricky, so take your time to create the best connection you can.

Solder the jumper wires to the motor shield. The two pins below the power connections should connect to analog A4 and A5 to control the sensor.

Soldering Jumper cables on motor shield

About the Sponsor: PCBWay

This post is sponsored by the PCBWay Company. PCBWay offers Only $5 for 10 PCBs and $30 in total for 20 PCBs Assembly. Besides this PCBWay also provides a great variety of services including Aluminum PCB, Rigid-Flex, Metal Core, flexible, High Frequency, High-TG, Thick-Copper, HDI, and LED PCBs. The sign up process hardly takes 1 minute and you are welcomed with a $5 welcome bonus, what are you waiting for go and get your first prototype order for free.

PCBWay website

Circuit Connection

  • Once you’ve soldered the wires to the motor shield, place the shield on top of the Arduino so that the pins of the shield line up with the holders in the Arduino below. The shield should fit exactly, but take care to align the pins to the holes and gently lower it in place.
  • Next, connect the ultrasonic sensor to the female ends of the jumper wires you soldered to the motor shield. Connect VCC on the sensor to +5V on the motor shield, Trig to A4, Echo to A5, and GND to GND (see the following table).
ULTRASONIC SENSORMOTOR SHIELD
VCC+5V
Trig PinA4
Echo PinA5
GNDGND
  • Connect the wires from the DC motors to the motor shield as shown in the following tables. You connect the wires by feeding them through the pin and using the screws to grip the wires in place.
LEFT MOTORMOTOR SHIELDARDUINO
Red wireM1+5V
Black wireM1GND
RIGHT MOTORMOTOR SHIELDARDUINO
Red wireM3+5V
Black wireM3GND

Connect the power wires of the DC motors as shown.

Connect the power wires of the DC motors
  • Next attach the servomotor to the shield, as shown in the following table.
SERVOMOTOR MOTOR SHIELD ARDUINO
Brown wireServo_2– GND
Red wireServo_2++5V
Yellow wireServo_2 sSignal

Connect the servomotor to the shield as shown.

Connect the servomotor to the shield
  • Attach the servomotor to the front of the robot using glue or tape. Then attach the ultrasonic sensor to the horn of the servomotor so it moves with the servo arm and your robot can look around. At this stage the robot should look something like below image.

The completed robot with ultrasonic sensor attached to the servo motor

Obstacle Avoiding Robot using Arduino UNO

The complete Circuit diagram of Obstacle Avoiding Robot using Arduino and Ultrasonic Sensor is given below.

Circuit diagram of Obstacle Avoiding Robot using Arduino and Ultrasonic Sensor
  • Once you’ve confirmed that your setup matches the above circuit diagram upload the code to the Arduino. The complete code is provided on program code section.

Program code for Obstacle avoiding robot using Arduino

Make sure you’ve downloaded the NewPing and Adafruit Motor Shield libraries and added them to your IDE. The Servo library is already included in the IDE, so you don’t need to install it.

The sketch starts by calling on the Adafruit Motor Shield, NewPing, and Servo libraries. The Trig pin of the ultrasonic sensor is defined as Arduino A4 and the Echo pin as Arduino A5. The maximum distance of the ultrasonic sensor is set at 200 centimeters and the speed of the DC motors is set at a medium speed of 190 (out of 255). The DC motors are defined to use connections M1 and M3 of the motor shield.

The servo is given a name and attached to pin 9 on the Arduino (via the connection on the motor shield). The loops after that take a reading from the ultrasonic sensor and if it detects that an object is less than 15 centimeters away. The motors stop and reverse slightly, the servo moves left and right once to look around, and the robot turns to the left and continues to move forward until it discovers another object.

#include <AFMotor.h>
#include <NewPing.h>
#include <Servo.h>
#define TRIG_PIN A4
#define ECHO_PIN A5
#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); // First motor to connection 1
AF_DCMotor motor2(3, MOTOR12_1KHZ); // Second motor to connection 3
Servo myservo; // Give the servo a name
boolean goesForward = false;
int distance = 100; // Define an int for distance and speed
int speedSet = 0;
void setup() {
myservo.attach(9); // Servo attached to pin 9
myservo.write(115); // Set servo at 115 degrees
delay(2000);
distance = readPing(); // Read the distance from the sensor
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 is less than 15 cm, carry out this function
if (distance <= 15) {
moveStop();
delay(100);
moveBackward();
delay(300);
moveStop();
delay(200);
distanceR = lookRight();
delay(200);
distanceL = lookLeft();
delay(200);
if (distanceR >= distanceL) {
turnRight();
moveStop();
} else { // Or else carry on
turnLeft();
moveStop();
}
} else {
moveForward();
}
distance = readPing();
}
int lookRight() { // The servo looks to the right
myservo.write(50);
delay(500);
int distance = readPing();
delay(100);
myservo.write(115);
return distance;
}
int lookLeft() { // The servo looks to the left
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);
}
void moveForward() {
if (!goesForward) { // If area is clear, motors move forward
goesForward = true;
motor1.run(FORWARD);
motor2.run(FORWARD);
// Slowly bring up speed to avoid loading down
// batteries too quickly
for (speedSet = 0; speedSet < MAX_SPEED; speedSet += 2) {
motor1.setSpeed(speedSet);
motor2.setSpeed(speedSet + MAX_SPEED_OFFSET);
delay(5);
}
}
}
void moveBackward() {
goesForward = false;
motor1.run(BACKWARD);
motor2.run(BACKWARD);
// Slowly bring up speed to avoid loading down
// batteries too quickly
for (speedSet = 0; speedSet < MAX_SPEED; speedSet += 2) {
motor1.setSpeed(speedSet);
motor2.setSpeed(speedSet + MAX_SPEED_OFFSET);
delay(5);
}
}
void turnRight() { // Movement for turning right
motor1.run(FORWARD);
motor2.run(BACKWARD);
delay(300);
motor1.run(FORWARD);
motor2.run(FORWARD);
}
void turnLeft() { // Movement for turning left
motor1.run(BACKWARD);
motor2.run(FORWARD);
delay(300);
motor1.run(FORWARD);
motor2.run(FORWARD);
}

Conclusion

Upload this code to your Arduino board by selecting its correct board and COM port. Afer successful upload of a program code, connect the 9V battery pack to your Arduino to see your robot in action!. So that’s all about Obstacle Avoiding Robot using Arduino and Ultrasonic Sensor. I hope this project would help you a lot in learning. If you have any doubts or queries do let me know in the comment section below.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button