Introduction: Obstacle Avoiding Robot Using Ultrasonic Sensor

About: I love making things!

In this project I will show you how to create an Arduino based Obstacle Avoiding Robot using an HC-SR04 Ultrasonic Sensor and 9g Micro Servo. During the build I will explain to you why you should use a Motor Shield and I will also explain how does the HC-SR04 Ultrasonic Sensor Work. Let's get started!

Step 1: Order Your Components !

In this project you will need these components :

1x Arduino Mega 2560 (or Arduino Uno )

1x Motor Shield (Adafruit)

1x HC-SR04 Ultrasonic Sensor

1x Micro Servo 9g (SG90)

2x Motor + Gearbox

2x Wheels

1x Caster wheel

1x 9v Battery

1x 9v Battery Clip

Jumper Wires

Step 2: Print the Body!

The body is made with 5mm PVC. I used this material because It's very light, strong enough to support the weight of the components, it's cheap and you can easly find it.

The body of this robot is made with the help of a CNC Machine and has three seperate pieces.

Since I don't have a 3d Printer, I bended some of the parts like the HC-SR04 Ultrasonic Sensor Holder (Check the photos).

Step 3: A Brain !

For this project i used an Arduino (Mega 2560) because it's a compact, comfortable and relatively cheap micro-controller. In addition, it is very common and you can get it literally everywhere online.

Step 4: Motor Shield? Why?

You must know that any power coming through the Arduino pins has likely gone through the on-board voltage regulator on the board. The voltage regulator is not designed to handle large amounts of current. And if your board is being powered through USB, USB isn't designed to provide large amounts of current. Finding another way to power a motor where the current does not flow through the on-board regulator will reduce the amount of heat generated and save board power for any other sensors or controls that might be necessary.

Another advantage of a motor shield is that it make it much easier to interface with component like motors, and it simplifies the wiring and allowing features like motor direction reversal.

Step 5: How Does the HC-SR04 Ultrasonic Sensor Work?

An Ultrasonic sensor is a device that can measure the distance to an object by using sound waves. It measures distance by sending out a sound wave at a specific frequency and listening for that sound wave to bounce back. Ultrasonic sensors are characterized by their reliability. They have a level measurement with millimetre precision.

It is important to understand that some objects might not be detected by ultrasonic sensors. This is because some objects are shaped or positioned in such a way that the sound wave bounces off the object, but are deflected away from the Ultrasonic sensor. It is also possible for the object to be too small to reflect enough of the sound wave back to the sensor to be detected.

Step 6: Wire Up the Electronics!

Here you can find the schematic that I created for this project.

As you can see, this schematic is very easy:

  • 9v Battery connect to the Motor Shield
  • The Servo motor connect to Servo1 on the Motor Shield
  • Left Motor connect to M2 on the Motor Shield
  • Right Motor connect to M1 on the Motor Shield

The HC-SR04:

  • VCC connect to the Arduino +5V
  • GND connect to the Arduino Ground
  • Trig connect to the Arduino Analog 10
  • Echo connect to the Arduino Analog 11

Step 7: Upload the Code!

Here you can download the Arduino sketch that I created for this project. But make sure to download and include this AFMotor library : https://github.com/adafruit/Adafruit-Motor-Shield...

And this NewPing Library : https://github.com/adafruit/Adafruit-Motor-Shield...

Step 8: Success!

Awesome! You did it! You just created your own Obstacle Avoiding Robot!

If you have any questions about the project, I would be happy to answer them.

If you build an Obstacle Avoiding Robot, I'd love to hear from you! Let me know where I can make the instructions better. Thanks!


Thanks for reading, and please consider voting for this instructable!