Arduino Robot Project: A DIY obstacle avoiding robot using an SG90 servo from Banggood.com

Arduino Robot Project: A DIY obstacle avoiding robot using an SG90 servo from Banggood.com


Dear friends welcome to another video! In this video we are going to build an Arduino
Robot that can avoid obstacles. It is a fun project and a great learning experience,
so without any further delay, let’s get started! As you can see the robot that we are going
to build today is moving around and it can detect obstacles and avoid them. It uses a ultrasonic distance sensor in order
to measure the distance from its front side. When it detects and obstacle it stops, goes
backward for a few cms, looks around and then it turns to the direction with the more space
available. As you are going to find out, building this
impressive little robot is extremely easy and fun. It will not take you more than a couple of
hours from start to finish. Then you can use my code, modify it and implement
your own robot behavior easily. It is a great learning experience and great
introduction to robotics for kids and adults. Let’s build it! The parts needed in order to build this robot
are these: • The Robot Chassis Kit
• An Arduino Uno • A Motor Shield
• A SG90 servo • An Ultrasonic Distance Sensor
• A switch • Two 100nF capacitors
• 1 330μF capacitor or similar • Some wires
The cost of the project is around 25$. You can find links for all the parts I use
in the description of the video. Before attempting to build this project I
encourage you to watch these two tutorials first. The first one is about the servo motor, and
the second one is about the supersonic sensor. You can watch these tutorials by clicking
on the card here. First we have to build the robot chassis. The kit, contains the chassis, two motors
with the wheels attached, a front wheel, a battery holder, some screws and wires. We solder the thick black and red wires at
the motors. Next we have to solder the small ceramic capacitors
to the motors. We do so in order to reduce the electrical
noise that the motor produces. Since we are using ceramic capacitors which
do not have polarity, just solder one leg to each motor connection. Repeat the procedure with the second motor
and we are ready to continue. We then attach the front wheel. My little nephew helps me with that as you
can see. This project is great for kids! Next we attach the rear wheels according to
the instructions leaflet that comes with kit. The next step is to attach the switch to the
battery holder. I cut the battery holder wires and I solder
the red wire from the battery holder to the first pin of the switch. The black wire from the battery holder goes
to the third pin. Now I solder a small red wire to the middle
pin of the switch. Another small black wire is then soldered
to the third pin of the switch. Our switch is ready. I then attach it to the battery holder using
hot glue. We move on to the motor shield. This shield provides power to the motors and
the servo motor and makes our life much easier. The motors need a lot of current, and this
shield can provide up to 600mA of current to each motor, that’s why we need it. We have to solder 4 wires to the motor shield. These wires will be then connected with the
supersonic sensor. We solder one to 5V, one to GND, one to Analog
Pin 5, and the last one to analog pin 4. Next we attach the motor shield to the Arduino
Uno board. With a double sided tape I attach the Arduino
Uno and the Battery holder to the chassis. With a double sided tape I also attach the
supersonic sensor to the Servo motor and both of them to the robot chassis. I could 3D print a part in order to achieve
that but since most of the viewers don’t have access to a 3D printer yet, I chose this
simple solution to achieve that. We are almost ready! All we have to do is to attach the wires! First we connect the Ultrasonic sensor. Vcc goes to 5V via the wire we attached earlier. GND goes to the GND wire. ECHO pin goes to Analog Pin 5, and TRIG pin
goes to Analog Pin 4. Next have to connect the power. We connect the black wire from the switch
to the motor shield. It goes to the GND pin of this external power
connector. The red wire from the switch goes to the M+
connector. If we open the switch we can see that the
green LED lights up, the Arduino is receiving power from the batteries. The next step is to connect the motors. We connect the left motor to the M1 connector
of the shield like this. We connect the right motor to the M3 connector
of the shield this way. Lastly we have to connect the servo motor. We connect it to the SERVO_2 connector of
the shield. The Brown wire is GND, the RED is Vcc and
the orange is Signal. That’s it! Our robot is now ready! All we have to do is to load the code to it. The code uses three libraries. Two of them must be downloaded in order the
program to compile. The first one is the motor shield driver from
Adafruit. The second library is the NewPing library
for the supersonic distance sensor. You can find the links for both libraries
in the description of the video. We press upload and after a few seconds the
robot awakes! You can watch it drive, look around and avoid
the obstacles. It is a fun little robot that you build! Impressive isn’t it? If your robot does not drive like this, check
your motor connections again. You may have inversed polarity and one motor
goes forward and the other backwards. Another problem that it might appear is with
the servo motor. If the servo motor moves randomly or stop
at some times, maybe you need to add a capacitor to it. I had such problems with my servo motor. So, I added a 330μF electrolytic capacitor
and the problem was solved. The positive leg of the capacitor goes to
the RED wire and negative, the short one, goes to the brown wire. Let’s now take a quick look at the code
in order to see how it works. At first we declare some variables. In this one we set the speed of the motors. You can set any value up to 255. Next, we initialize the servo motor and we
set it to “look” straight. In my case this was at 115 degrees angle. Next we read the distance a few times in order
to get a valid distance measurement. Now we go to the loop function which executes
every 40 ms. If the distance we measured is less than or
equal to 15 cm, we stop the motors, we go backwards for 300ms and then we stop again. We look right and left and we measure the
distance at each direction. If the distance at one direction is greater
than the other we turn the robot the largest distance. If the distance from the front of the robot
is greater than 15 cm, we move the robot forward. That’s the simple algorithm I developed
for this robot so far. As always you can find the code of the project
in a link in the description of the video. You can easily modify the code I provide in
order to modify the robot behavior. It is very easy. As you can see building a robot with Arduino
is very easy. By building this project you will understand
better the Arduino board and it capabilities. You will learn how to control motors and how
to program them. This robot is going to be the base for many
more robot projects to come. We can add an NRF24L01 module to it to wirelessly
control it and get telemetry data from it. We can add sound, lights, more sensors, and
better batteries. There are a ton of improvements to be made. That’s why I would love to hear your opinion
on this. What kind of features do you want to add to
this simple robot? Please post your ideas in the comments section
below. Thanks! If this is your first time here, I would love
to have you subscribed. In this channel I post videos about DIY projects
every Saturday. I love making things and I believe that anyone
can make things, anyone can become a maker. That’s why I created this channel, in order
to share my knowledge with the community and learn from the community. I hope you will join us. Until next Saturday, Watch, Learn, Build! [ Translating these subtitles? Add your name here! ]

100 Replies to “Arduino Robot Project: A DIY obstacle avoiding robot using an SG90 servo from Banggood.com”

  1. Being new to this I have a question about the code. Once uploaded to the UNO will the UNO store and hold the code. I am confused about the power connection. The USB cable provides the power and the code transfer – so how once power is removed does your robot remember the code??

  2. Wow! I have never seen so many people without any knowlegde about Arduino trying to build an Arduino-robot.Please! To all you beginners, try and go to the Arduino site and learn a bit Arduino before making big projects and you will have more fun instead of frustration.(Sorry for bad gramma and spelling)

  3. i almost completed the project
    but when i turned it on, only the servo is working
    but the dc motor isn't
    the video shows that 1.2v eneloop batteries work
    but i'm using 1.5v batteries.
    can someone please help me finish the project?????

  4. And why is this guy not responding on people that have problems. Like the others only my right wheel spins nothing else, sometimes the servo makes sound. I used the library to add the motor and the ping. Nothing works good

  5. Sir,
    My robot turns at a very small angle and only to the left.
    It takes about 3 minutes for it to avoid the obstacle and move to a free space.
    plz help.

  6. French retired I discover electronics. this tutorial is really great.
    can we have the sheme of connection of the LCD screen as well as modofications for code Encore thank you

  7. Good project.
    Sir I Need A little bit Help from your Side.
    Will You give me the Schematic Diagram of this project.
    Thanks you Sir.!!!!!!!!!!!!!!!!!!!!!!!

  8. The motor shield sends only 1,7 V at the right motor and it doesn't work. Meanwhile the left motor receives about 3,2 volts and runs normally. Is it a shields problem or simply there 's not enough power? I put 4x AA new batteries and the multimeter shows 5,9V. The servo and the sensor work just fine.

  9. Hello, first of all this is a great video. I would like to ask about a problem of mine. For some reason sometimes it does not go straight forward. IT seems that a motors starts some ms before the other motor, Any ideas how to solve this? When i place it to move forward to seems to have a drag left or right…it does go straight.

  10. As I am mechanical hobby technician, this video was very helpful for me and IP:) (Idiotic Proof), but I would need to connect more powerful el. motors, like for 12 or even 18 V DC for some grass mover, etc. Can you help with some details how to realieze it? Many thanks

  11. A nice project, but when I made it everything was working properly only thing was that one motor was not running Pls help

  12. I have watched several of your vids…they are top notch.

    IDEA==>could you take this same platform and make a vid on using a remote control?

  13. A question. The motors I'm using may require a higher voltage than I'm using. I'm assuming that's why I can't get my code to run correctly. The sensor seems to work just fine on a test rig but does nothing on the robot. I copied and pasted the code directly from the website and connected everything correctly as in the video, and it doesn't seem to work. What might I be doing wrong? Thanks for reading. 🙂

  14. Great video, helped me alot. I made a robot for a school project along with a detailed 'scription' about robots in todays society.
    Is it okay if i put your video with the sources?
    Thanks!

    PS: One question, why did you solder the wires to the motor shield when you could've put them in the arduino first to let you keep using the motor shield?

  15. I use everything as per shown but my L293D produce beeping noise. I am using absolutely new 9V battery . Please help me

  16. Arduino: 1.6.10 (Windows 7), Board: "Arduino/Genuino Uno"

    Sketch uses 3,830 bytes (11%) of program storage space. Maximum is 32,256 bytes.
    Global variables use 89 bytes (4%) of dynamic memory, leaving 1,959 bytes for local variables. Maximum is 2,048 bytes.
    C:Program FilesArduinohardwaretoolsavr/bin/avrdude -CC:Program FilesArduinohardwaretoolsavr/etc/avrdude.conf -v -patmega328p -carduino -PCOM10 -b115200 -D -Uflash:w:C:UsersadminAppDataLocalTempbuild68c293821abaf1b41f3093fa58d7e362.tmp/pd2.ino.hex:i
    An error occurred while uploading the sketch

    This report would have more information with
    "Show verbose output during compilation"
    option enabled in File -> Preferences.
    this type of error occuring can you help me please

  17. Thanks for the video. I found the kit did not really work out of the box and did a number of modifications. Here are my build instructions and modified code. http://www.gertweb.com/SmartCar.html

  18. Hi.. build the same robot, but i didn't with the arduino mega 2560…. I see on the video after connecting the motor shield to the arduino, the motor shield turns on after connecting power to it, but im my case it doesn't turn on (led is off after connecting power to my motor shield.) any one can help me..!

  19. This code seemed to work for me. Just change the Trig And Echo pins to math your setup.
    //including the libraries

    #include <AFMotor.h>

    #include <NewPing.h>

    #include <Servo.h>

    //defining pins and variables

    #define TRIG_PIN A5

    #define ECHO_PIN A2

    #define MAX_DISTANCE 200

    #define MAX_SPEED 200 // sets speed of DC motors

    #define MAX_SPEED_OFFSET 20

    //defining motors,servo,sensor

    NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);

    AF_DCMotor motor1(1, MOTOR12_8KHZ);

    AF_DCMotor motor2(2, MOTOR12_8KHZ);

    Servo myservo;

    //defining global variables

    boolean goesForward=false;

    int distance = 100;

    int speedSet = 0;

    void setup() {

    myservo.attach(10);

    myservo.write(90);

    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<=25)

    {

    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();

    }

    //reseting the variable after the operations

    distance = readPing();

    }

    int lookRight()

    {

    myservo.write(180);

    delay(500);

    int distance = readPing();

    delay(100);

    myservo.write(90);

    return distance;

    }

    int lookLeft()

    {

    myservo.write(0);

    delay(500);

    int distance = readPing();

    delay(100);

    myservo.write(90);

    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)

    {

    goesForward=true;

    motor1.run(FORWARD);

    motor2.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+MAX_SPEED_OFFSET);

    delay(5);

    }

    }

    }

    void moveBackward() {

    goesForward=false;

    motor1.run(BACKWARD);

    motor2.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+MAX_SPEED_OFFSET);

    delay(5);

    }

    }

    void turnRight() {

    motor1.run(FORWARD);

    motor2.run(BACKWARD);

    delay(300);

    motor1.run(FORWARD);

    motor2.run(FORWARD);

    }

    void turnLeft() {

    motor1.run(BACKWARD);

    motor2.run(FORWARD);

    delay(300);

    motor1.run(FORWARD);

    motor2.run(FORWARD);

    }

  20. I want to attach a harrow behind it that will need stepper motor for its angular movement. how can I go about it?

  21. I want to attach a harrow behind it that will need stepper motor for its angular movement. how can I go about it?

  22. Well ..I've got a problem with the Super Sonic Sensor.
    When I run the New Ping Example it would just show me some squares …or occasionally some letters when i gently press its "eyes".
    However when i run the code that you gave us in the super sonic sensor tutorial (the one without any library) it works just fine. So I guess the problem would be at the New Ping library ? As the sensor works fine when i'm not using it. The problem is , I guess i will need the New Ping library in order to make this robot work ( for lookright() function and lookleft(function))

  23. where will i upload the code to?, do i upload it to the arduino uno board, or just click upload and it will wirelessely be awake

  24. I hope you find this video useful. If you want to support the channel see how here: http://educ8s.tv/support-educ8s-tv/

  25. Please upload this code if one of your motors is not working:

    http://www.mediafire.com/download/dql27gnuljjpurr/Obstacle_Avoiding_Robot_Code.ino

  26. Thank you! Good explanation and nice pics. The video helped a lot ! I would like to suggest a few edits please.
    1- The wiring for the Ultrasound Sensor needs to be corrected. The trigger and Echo pins in the diagram and in the code are inverted. The code has Trigger pin in A4, while the diagram has it in A5.
    2- If you have issues with motor not running, may be you want to remove the input MOTOR12_1KHZ from lines 7&8. See updated code below. What I found is that for most of the battery operated motors, the MOTOR12_1KHZ will create an error and a humming noise in the motor.

    AF_DCMotor motor1(1); // Removed MOTOR12_1KHZ
    AF_DCMotor motor2(3); // Removed MOTOR12_1KHZ

  27. hello sir, can you be able to limit the distance of the sensor to let say 30cm then the robot will detect obtacle thus it will move away

  28. Heyy There! Can anyone help me …My Ultrasonic sensors is not working . It cannot detect any obstacle? Plz Anyone…Its urgent

  29. I connected 9v to the motor shield but it's not working.
    But when I give supply through serial port it works perfect .
    Why is it so???

  30. Dear, I'm looking for a smaller ultrason sensor but I cann't find it on the internet. Please smaller then the HC04, the HC04 is to large for my project. Thanks a lot for your video's and tips etc… Hope someone can help me with a link to look at a smaller ultrason sensor.

  31. Hi,
    Excellent video.
    Is it possible to implement the project so that the robot is autonomous and create routes through mapping through a device (laptop, tablet, smartphone….)?…. How would it be?
    Thank you

  32. i complete to upload code to aurduino but when i remove the cable from aurduino the robot doesn't work what should i do now?

  33. Arduino: 1.8.9 (Windows 10), Board: "Arduino/Genuino Uno"

    C:UsersHPDocumentsArduinosketch_aug17asketch_aug17a.ino: In function 'void setup()':

    sketch_aug17a:18:3: error: 'distance' was not declared in this scope

    distance = readPing();

    ^

    sketch_aug17a:18:23: error: 'readPing' was not declared in this scope

    distance = readPing();

    ^

    C:UsersHPDocumentsArduinosketch_aug17asketch_aug17a.ino: In function 'void loop()':

    sketch_aug17a:32:5: error: 'distance' was not declared in this scope

    if(distance<=15)

    ^

    sketch_aug17a:34:12: error: 'moveStop' was not declared in this scope

    moveStop();

    ^

    sketch_aug17a:36:16: error: 'moveBackward' was not declared in this scope

    moveBackward();

    ^

    sketch_aug17a:40:25: error: 'lookRight' was not declared in this scope

    distanceR = lookRight();

    ^

    sketch_aug17a:42:24: error: 'lookLeft' was not declared in this scope

    distanceL = lookLeft();

    ^

    sketch_aug17a:47:15: error: 'turnRight' was not declared in this scope

    turnRight();

    ^

    sketch_aug17a:51:14: error: 'turnLeft' was not declared in this scope

    turnLeft();

    ^

    sketch_aug17a:55:15: error: 'moveForward' was not declared in this scope

    moveForward();

    ^

    sketch_aug17a:57:4: error: 'distance' was not declared in this scope

    { distance = readnewping.h();

    ^

    sketch_aug17a:57:15: error: 'readnewping' was not declared in this scope

    { distance = readnewping.h();

    ^

    sketch_aug17a:58:1: error: expected '}' at end of input

    }

    ^

    exit status 1

    'distance' was not declared in this scope

    This report would have more information with

    "Show verbose output during compilation"

    option enabled in File -> Preferences.

    not working

  34. error code:

    Arduino: 1.8.10 (Windows 8.1), Board: "Arduino Uno WiFi"

    ArduinoRobot:7:10: error: AFMotor.h: No such file or directory

    #include <AFMotor.h>

    ^~~~~~~~~~~

    compilation terminated.

    exit status 1

    AFMotor.h: No such file or directory

    Invalid library found in C:UserssrinivasDocumentsArduinolibrariesForward_robot_motion: no headers files (.h) found in C:UserssrinivasDocumentsArduinolibrariesForward_robot_motion

    Invalid library found in C:UserssrinivasDocumentsArduinolibrariesForward_robot_motion: no headers files (.h) found in C:UserssrinivasDocumentsArduinolibrariesForward_robot_motion

    This report would have more information with

    "Show verbose output during compilation"

    option enabled in File -> Preferences.

Leave a Reply

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