Obstacle Avoiding Robot Using Ultrasonic Sensor

24,618

46

17

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!

Be the First to Share

    Recommendations

    • Big and Small Contest

      Big and Small Contest
    • For the Home Contest

      For the Home Contest
    • Make It Bridge

      Make It Bridge

    17 Comments

    0
    syedz37
    syedz37

    Question 4 years ago on Step 7

    hye. im using 2 servo motors as for both wheels and my hc-sr04 sensor is just at fixed position at front. i dont know how to develop the code. help me please

    0
    JaviA6
    JaviA6

    Question 4 years ago

    This was a very useful instructable. But my wheels are stuck, the motors are not getting enough power (even when connected via USB and with the 9V battery). I don't know what to do. I'm using the default motor of the Arduino starter pack and another motor from an old toy rc car.

    0
    JaviA6
    JaviA6

    Answer 4 years ago

    I'm gonna try it. I didn't know that facts. Hope that works.
    Thank you for your patience!!!

    0
    Chihab
    Chihab

    Answer 4 years ago

    Hello!:) The motors are not getting enough current because of the 9v Battery you are using. I think you have no hope using this battery, these batteries provide a small current only, they are designed for powering radio controllers, not motors. The solution is to use at least a 9V rechargeable battery but I absolutely recommend to use a 12v lithium-ion battery pack that will power up the robot for more than 30 minutes, since the 9V Battery will power up the robot for a few minutes only.

    0
    JaviA6
    JaviA6

    Answer 4 years ago

    Hi!

    No, I'm using a non-rechargable 9V plus the 5V entrance from the Arduino's USB.

    0
    Chihab
    Chihab

    Answer 4 years ago

    Thank you soo much for your feedback! :) Are you using a rechargeable 9V battery?

    0
    AryaB7
    AryaB7

    Question 5 years ago

    the motors are not getting enough power . What should i do ?

    0
    Chihab
    Chihab

    Answer 5 years ago

    What is the battery pack that you are using?

    0
    NamalaA
    NamalaA

    Question 5 years ago on Step 8

    its showing error bro
    how can i correct say how to add that libraries

    0
    Chihab
    Chihab

    Answer 5 years ago

    In order to include this library you only need to download the AFMotor Library in ProgramFiles\Arduino\libraries\.

    0
    NamalaA
    NamalaA

    Reply 5 years ago

    is this working bro

    0
    NamalaA
    NamalaA

    Reply 5 years ago

    please send the source code

    0
    Chihab
    Chihab

    Reply 5 years ago

    The code is available at Step 7! :)

    0
    Chihab
    Chihab

    Reply 5 years ago

    Thanks!

    0
    KhaledA121
    KhaledA121

    5 years ago

    it's a very good idea..

    0
    Chihab
    Chihab

    Reply 5 years ago

    Thank you so much !! :)