Skip to content
MechStuff

MechStuff

''Making stuff… Simpler''

  • Home
  • MechStuff
  • News
  • How Stuff Works
  • Engines
  • Mech-Tech
  • TechStuff
  • Robotics
    • Robot Stuff
    • Tutorials
  • Engineering Marvels
  • For Mechanical Engineers
    • Games for Mechanical Engineers
    • Movies for Mechanical Engineers
    • Projects for Mechanical engineers
  • Contact Us
  • About us

How to make obstacle avoiding robot using Arduino & Ultrasonic sensor ?

14
obstacle avoiding robot using arduino

So you wanna build a robot which never hits anything; you’ve come to the right place! It barely takes 15 minutes to do this! So without wasting our time let’s learn how to make an obstacle avoiding robot!

Requirements :-

  1. Chassis OR any toy car.
  2. Arduino UNO/Mega.
  3. Ultrasonic sensor HC SR-04.
  4. 2 DC motors.
  5. 9V/12V 1A battery.
  6. Motor driver module L298.
  7. Jumpers.
  8. Single-stranded wires.
  9. A pizza slice & a can of beer to celebrate πŸ˜‰

Procedure :-

So yes, I’m going to divide the whole making into 4 parts –

Connections of Ultrasonic sensor    β†’    Connections of L298N    β†’    Code    β†’    Testing

If you haven’t used or experimented before with the ultrasonic sensor, I would suggest you to get familiar with it first! I have a detailed tutorial on ultrasonic sensor HC SR-04 which will guide you throughout!

Here is detailed Tutorial on Ultrasonic Sensor HC-SR04 

 Connections :-

Connections for Obstacle avoiding robot
Connections for Obstacle avoiding robot

Connections of Ultrasonic sensor –

  1. VCC – VCC terminal of Arduino.
  2. GND – GND terminal of Arduino.
  3. Trigpin – digital pin 9 on Arduino.
  4. Echo pin – digital pin 10 on Arduino.

Connections of L298N –

  1.  +12V – Positive terminal of the battery.
  2. GND – a)GND of Arduino   b)Negative terminal of battery.
  3. Input terminal 1 – Pin 4
  4. Input terminal 2 – Pin 5
  5. Input terminal 3 – Pin 6
  6. Input terminal 4 – Pin 7
  7. Output terminal 1 – Positive of first motor.
  8. Output terminal 2 – Negative of first motor.
  9. Output terminal 3 – Positive of second motor.
  10. Output terminal 4 – Negative of second motor.

Let me tell you, if you’re getting confused between the connections of motor driver and motors, you can first try only with one motor. For more clarity, I have made a separate tutorial for you on interfacing Arduino with Motor driver module L298N. I have provided the connections as well as the code for it.

Coding :-

int trigPin = 9;
int echoPin = 10;
int revright = 4;      //REVerse motion of Right motor
int fwdleft = 7;      
int revleft= 6;       
int fwdright= 5;       //ForWarD motion of Right motor
int c = 0;

void setup() {
  //Serial.begin(9600); 
   pinMode(5, OUTPUT);
   pinMode(6, OUTPUT);
   pinMode(4, OUTPUT);
   pinMode(7, OUTPUT);
   pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  // put your setup code here, to run once:

}

void loop() {
  long duration, distance;
  digitalWrite(trigPin,HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration=pulseIn(echoPin, HIGH);
  distance =(duration/2)/29.1;
  //Serial.print(distance);
  //Serial.println("CM");
  delay(10);
 
  
  if((distance>20))
 {
  digitalWrite(5,HIGH);                               //       If you dont get proper movements of your robot,
   digitalWrite(4,LOW);                               //        then alter the pin numbers
   digitalWrite(6,LOW);                               //
   digitalWrite(7,HIGH);                              //
 }
 
  else if(distance<20)  
 {
   digitalWrite(5,HIGH);
   digitalWrite(4,LOW);
   digitalWrite(6,HIGH);                                  //HIGH
   digitalWrite(7,LOW);
                                            
 }
 
}

You may like –

Control LEDs with help of Android (in 5 mins)

&

Control LEDs with Voice commands (in 8 mins)

Testing :-

If the distance detected by the ultrasonic sensor is less than 25cm, then the bot will take a turn else, it will keep moving straight ! Simple… right ?

Well, here is a video tutorial which will make your job more easy !

Do I need to say this, NO… but anyway I will,

If you have any problem regarding connections or coding or during interfacing, all you have to do is comment down below OR contact me πŸ™‚ I’ll very happy to help you

Share this Stuff :

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • More
  • Click to email a link to a friend (Opens in new window)
  • Click to print (Opens in new window)

Related

Tutorials Arduino, HC-SR04, L298N, mechatronics, mini-project, motor driver module, obstacle avoiding robot, simplest way, ultrasonic sensor, video tutorial

14 comments on “How to make obstacle avoiding robot using Arduino & Ultrasonic sensor ?”

  1. Ricky Cabanillas says:
    December 12, 2018 at 6:37 PM

    Hello, I was wondering what rpm your dc motors are at?

    Reply
    1. Jay Baviskar says:
      December 14, 2018 at 5:01 PM

      Hello Ricky, they are 100 rpm DC motors

      Reply
  2. Dika fitra says:
    December 19, 2018 at 7:30 PM

    Why my driver l298n not working even though i have given a 9v battery voltage?

    Reply
    1. Jay Baviskar says:
      January 24, 2019 at 9:54 PM

      Hi Dika, please check the connections again or there is a minor chance that your module must have fried because of prior wrong connection!

      Reply
  3. Chris says:
    February 26, 2020 at 12:49 AM

    Hi, I was wondering if I use a l293 motor driver instead of l298N would that mean that the arduino code has to be changed ?

    Reply
    1. Jay Baviskar says:
      August 10, 2020 at 5:02 PM

      No Chris, the code will be the same πŸ˜€

      Reply
  4. megh khara says:
    March 2, 2020 at 8:33 AM

    This is one of the best websites i have ever visited for doing my school projects.I just did a little modification .I actually attached a overhead roof and attached a bottle. This was a automatic floor cleaner.It won me a silver medal

    THANK YOU VERY MUCH!

    Reply
    1. Jay Baviskar says:
      April 4, 2020 at 12:00 PM

      Hello Megh, Thank you very much for your kind and encouraging comment! A great, quick and simple modification Megh. Glad to know I could help you! πŸ˜€

      Reply
  5. Dinesh Kuniyal says:
    November 22, 2020 at 8:57 PM

    I have used a 12 volt battery but still car is not moving please help…

    Reply
    1. Jay Baviskar says:
      November 27, 2020 at 6:23 PM

      Hello, please check the connections & code! If they are exactly the same as mine, then try to just rotate the motor first.

      Reply
  6. Therans K Jerome says:
    February 18, 2021 at 3:48 PM

    Can someone please tell me how can I edit the code provided so that I can make the robot turn wider after it stops in front of an object?

    Reply
    1. Jay Baviskar says:
      March 25, 2021 at 11:41 AM

      Hello, I think you can try entering a little delay() after or in the part where the bot takes turn. Try and let me know Therans

      Reply
  7. AR says:
    June 3, 2021 at 5:29 PM

    Thank you very much.Please upload another projects that can be done with ‘Arduino’πŸ˜„

    Reply
    1. Jay Baviskar says:
      July 22, 2021 at 12:42 PM

      Sure.. another one is one its way. Stay tuned πŸ˜€

      Reply

Leave a Reply Cancel reply

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

Idealist by NewMediaThemes