Arduino Robot – An easy DIY project

Home/Arduino Projects/Arduino Robot – An easy DIY project

Arduino Robot – An easy DIY project

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 Arduino Robot that we are going to build today is moving around and it can detect obstacles and avoid them. It uses an 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!

How to build an Arduino Robot

The parts needed in order to build this Arduino Robot are these:

——————–
WHERE TO BUY
——————–

Robot Chassis Kit: http://bit.ly/RobotChassisB

Arduino Uno: http://bit.ly/Cheap_Uno

Motor Shield: http://bit.ly/MotorShield

SG90 Servo: http://bit.ly/SG90Servo

Ultrasonic Sensor: http://bit.ly/HCSR04B

Switch: http://bit.ly/Switch10PCS

100nf Capacitors: http://bit.ly/CeramicCapacitors

300μF Capacitors: http://bit.ly/ElectrolyticCapacitors

Wires: http://bit.ly/Wires3InOne

Full disclosure: All of the links above are affiliate links. I get a small percentage of each sale they generate. Thank you for your support!

The cost of the this Arduino Robot is around 25$.

First we have to build the Arduino 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 Supersonic 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.

——————–
LIBRARIES
——————–

Motor Shield Library: https://github.com/adafruit/Adafruit-Motor-Shield-library

New Ping Library: https://bitbucket.org/teckel12/arduino-new-ping/wiki/Home#!download-install

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.

——————–

CODE OF THE PROJECT
——————–

Download

 

 

 

SUBSCRIBE ON YOUTUBE

——————–

Never miss a video: Subscribe to educ8s.tv

2017-03-30T12:03:38+00:00

24 Comments

  1. adarsh September 21, 2016 at 12:49 pm

    Thk u very much..ur tutorial is really good…i have seen all ur vedio and subscribed…but Can u plz send me a link where I can download all ur codes of the project’s at a time….i had downloaded it earlier but I lost it ..now again I have get a codes by going through each vedio info again…plz send to my email or send me an link to download all the codes at a time…thk you

  2. Rick September 28, 2016 at 8:25 am

    Hi, your code is not working properly. do you have a fix for it? Seems to be having a problem with this line:

    NewPing sonar (TRIG_PIN, ECHO_PIN, MAX_DISTANCE);

    Thanks

    • Nick September 28, 2016 at 8:33 am

      Have you downloaded the NewPing library?

      • Marian October 10, 2016 at 5:18 pm

        Hi!
        Please help me with the code. I get a set of errors when trying to upload.
        Arduino: 1.5.4 (Windows 7), Board: “Arduino Uno”

        ArduinoRobot:19: error: ‘AF_DCMotor’ does not name a type
        ArduinoRobot:20: error: ‘AF_DCMotor’ does not name a type
        ArduinoRobot.ino: In function ‘void moveStop()’:
        ArduinoRobot:109: error: ‘motor1’ was not declared in this scope
        ArduinoRobot:109: error: ‘RELEASE’ was not declared in this scope
        ArduinoRobot:110: error: ‘motor2’ was not declared in this scope
        ArduinoRobot.ino: In function ‘void moveForward()’:
        ArduinoRobot:118: error: ‘motor1’ was not declared in this scope
        ArduinoRobot:118: error: ‘FORWARD’ was not declared in this scope
        ArduinoRobot:119: error: ‘motor2’ was not declared in this scope
        ArduinoRobot.ino: In function ‘void moveBackward()’:
        ArduinoRobot:131: error: ‘motor1’ was not declared in this scope
        ArduinoRobot:131: error: ‘BACKWARD’ was not declared in this scope
        ArduinoRobot:132: error: ‘motor2’ was not declared in this scope
        ArduinoRobot.ino: In function ‘void turnRight()’:
        ArduinoRobot:142: error: ‘motor1’ was not declared in this scope
        ArduinoRobot:142: error: ‘FORWARD’ was not declared in this scope
        ArduinoRobot:143: error: ‘motor2’ was not declared in this scope
        ArduinoRobot:143: error: ‘BACKWARD’ was not declared in this scope
        ArduinoRobot.ino: In function ‘void turnLeft()’:
        ArduinoRobot:150: error: ‘motor1’ was not declared in this scope
        ArduinoRobot:150: error: ‘BACKWARD’ was not declared in this scope
        ArduinoRobot:151: error: ‘motor2’ was not declared in this scope
        ArduinoRobot:151: error: ‘FORWARD’ was not declared in this scope

        This report would have more information with
        “Show verbose output during compilation”
        enabled in File > Preferences.

        I mention that I downloaded the code from your link on the website:
        http://educ8s.tv/arduino-robot-easy-diy-project/

        Many thanks!!!

        • Nick October 12, 2016 at 6:33 pm

          Have you installed the Motor library?

        • nihal keetha November 28, 2016 at 2:08 pm

          hey you install the motor shield library. i am sure you will get it and after installing the library make sure you drag the library folder to document/Arduino/library..

  3. Rick September 28, 2016 at 8:44 am

    Hey, never mind. Your code works great. Needed to restart arduino after installing the ping library. Great video. Thanks.

  4. Rahul November 10, 2016 at 6:23 pm

    Thank you very much

  5. Carlo November 26, 2016 at 12:08 pm

    Hi, i don’t understand where to sold the 300μF capacitor……Thank you very much

  6. Carlo November 26, 2016 at 12:56 pm

    Sorry….. i resolved….Great project, . I would like to congratulate you on your excellent work!!!

    • Biagio November 30, 2016 at 9:30 pm

      I need a little help… Where do I have to connect the 300μF capacitor??? Thanks! Great tutorial! 🙂

  7. Shubham singh December 2, 2016 at 2:39 pm

    Sir the robot is not moving its head and only rotating right side only please help me

    • Luke February 20, 2017 at 4:49 pm

      Did you sort this if so how?

  8. sanzida islam February 5, 2017 at 7:20 pm

    thank you so much.your video helped a lot.🌾☺

  9. Jeta February 8, 2017 at 5:34 pm

    Hi, I was wondering how I could modify the code to include IR sensors for line following? Also how would I attach these to the current body of the robot
    Thank you

  10. Terry Ryan February 16, 2017 at 9:19 pm

    Love the project. It’s been a big help to me. I modified an old Trilobot (that my son found in the garbage) and all basic functions work fine. But, I have a question. In your code there is a class called “moveForward”. See below. In that class you refer to a Boolean variable called “goesForward”. If this is necessary, please explain why. I have adapted the overall code to suit the latest version of the Adafruit Motorshield. In my case, is “goesForward” still required?
    Have fun.
    Thanks,
    Terry from Toronto

    *************************************************
    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);
    }
    }
    }
    *********************************************

  11. Nordiea March 2, 2017 at 2:07 am

    Good night, I have just completed mine, the robot is not moving, i have checked the code and everything, I suspect that it is the batteries, I will get some better one and let you know the outcome.
    Thanks

  12. Abhijeet Dash March 21, 2017 at 3:03 pm

    Sir can we make a robo follow us using this technique
    I m thinking of a an emmiter which the user will carry and a ultrasound sensor which will be on the bot by measuring distance it can also follow the emmiter

  13. Chandramohan April 4, 2017 at 8:14 am

    Hi . Im doing this project uaing servo motor instead of dc motor. Do you have coding for that . Thank you

  14. Danny Miro May 1, 2017 at 12:42 am

    Can you help me with my case,,, my robot is 4wd and i don’t know how to modify the code for that…

  15. Md.Sirajul Islam May 3, 2017 at 6:15 pm

    Hi,
    your video is very easier to build obstacle robot, in this regard I have build everything and finally verify your code but there is error coming which is “redefinition void set up()” for this I could not upload it. So would you please help me by correcting this. (parts are arduino uno,adafruit motor shield,HC-Sr04 and SG90).
    Thanks

    #include
    #include
    #include

    #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);
    AF_DCMotor motor2(3, MOTOR12_1KHZ);

    Servo myservo;

    boolean goesForward=false;
    int distance = 100;
    int speedSet = 0;

    void setup() {

    myservo.attach(9);
    myservo.write(115);
    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=distanceL)
    {
    turnRight();
    moveStop();
    }else
    {
    turnLeft();
    moveStop();
    }
    }else
    {
    moveForward();
    }
    distance = readPing();
    }

    int lookRight()
    {
    myservo.write(50);
    delay(500);
    int distance = readPing();
    delay(100);
    myservo.write(115);
    return distance;
    }

    int lookLeft()
    {
    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)
    {
    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);
    }

  16. Prasanna K September 3, 2017 at 4:19 am

    Great write up. I’m looking to use this for a 4 wheel robot…any suggestions please ?

  17. Noval September 7, 2017 at 11:05 am

    Can I use Arduino Uno DIP ? Coz in your video you’re using Arduino Uno SMD.

    • Nick September 8, 2017 at 8:24 am

      Yes, it is exactly the same.

Comments are closed.

Close