Introduction: Arduino Ground Rover With Adafruit Motor Shield V1 and Servo Sonar Sensor

About: Project S.E.R.G.E. (Sensors, Electronics, Robotics, Graphics, Education). Exploring the world of microcontrollers using the Arduino and Raspberry Pi platforms.

This is the Arduino Ground Rover. It uses the Adafruit Motor Shield V1, mini Servo, and a HC-SR04 Sonar Sensor. This design is meant to be a baseline project that can be modified for future use to incorporate various other sensors, modifications, and upgrades. I hope you enjoy building this! ***This is a work in progress.***

Supplies

Parts/Bill of Materials

  1. 1x Arduino UNO R3 microcontroller (or similar clone).
  2. Arduino IDE (software download from Arduino.cc)
  3. USB Cable to upload code
  4. 1x Adafruit Motor Shield V1 (or similar clone)
  5. 1x HC-SR04 Ultrasonic Sonar Sensor
  6. 1x 9g Micro Servo w/ nylon control arm
  7. 1x 400 tie-point breadboard
  8. 1x Battery Holder Case, 6x1.5V (AA battery) w/ DC 5.5x2.1mm Male Connector, ON/Off Switch
  9. 2x Robot Plastic Tire Wheel with DC 3-6V Gear Motor
  10. 1x 2 inch Caster Wheel
  11. 6x AA batteries (1.5V)
  12. Various Zip Ties (mainly 4-inch, 8-inch)
  13. 8x 4-inch zip ties
  14. 2x 8-inch zip ties
  15. Various Breadboard Jumper Wires
  16. 12x 5-inch Male-to-Female jumper wires
  17. 6x U-shape flat connecting line jumper wires
  18. Robot Car Chassis kit, or Access to 3D printer for printing Arduino Rover Body and Sonar Holder.
  19. Some Robot Car Chassis kits come with the caster wheel, robot tires with DC gear motors, and battery holder.

Tools for Assembly

  1. Pliers
  2. Small Flat-head or Philips Screwdriver

Step 1: Print Rover Body and Sonar Sensor

If you purchased a Robot Car Chassis kit, then you can use that as the base for your rover. Follow the instructions that came with them. If you do not want to purchase a kit, then you can follow the steps below.

  1. Download the attached STL files for the Arduino Rover Body, and HC-SR04 Sonar Sensor Holder.
  2. Print them using an available 3D printer.
  3. The following was used in order to print:
  4. Creality CR-10 3D printer
  5. Ultimaker Cura Printing Software
  6. 1.75mm PLA Filament
  7. Orient the STL files in the 3D printing software so that the largest flat surface is facing down towards the printing plate.
  8. The Arduino Rover Body will take approximately 6-7hrs to print. I did not use any supports for printing.
  9. The Sonar Sensor Holder will take approximately 30mins to print. I printed with supports.

Step 2: Assemble the Sonar Sensor Holder

If you do not have access to a 3D printer for printing the Sonar Sensor holder, you can try zip-tying the servo nylon control bar to the sonar sensor, you can also try using a Hot Glue Gun to glue the control bar to the sensor, or use super-glue to glue it together. This will take some trial & error.

If you do have access to a 3D printer, and are able to print the Sonar Sensor Holder, then proceed with the steps below.

  1. Grab the nylon control bar from the micro servo, and insert it into the designated slot on the 3D print.
  2. Use two 4-inch zip ties to secure the control bar. Once it is tightened enough, snip/cut the excess from the zip ties.
  3. Insert the HC-SR04 Sonar Sensor, and use two small jumper wire connecting lines to keep it in place.

Place this piece off to the side, and we will add it to the Rover later on towards the end. Go on to the next step.

Step 3: Assemble the Rover Body Wheel Base

Now you will start putting together the main body of the rover.

  1. Place the flat surface of the rover body down on your working surface.
  2. Add the two DC Gear Motors without the wheels attached.
  3. Use two small (4-inch) zip ties per wheel.
  4. Insert one zip tie (#1) into the top hole that lines up with the motor.
  5. Tuck the same remaining zip tie (#1) back into the bottom hole.
  6. Grab the second zip tie (#2), and insert it into the locking mechanism of the top zip tie (#1). Only go about halfway. You need the remaining ends.
  7. Now, insert the end of the bottom zip tie (#1) into the locking mechanism of the second zip tie (#2).
  8. Tighten the zip ties to ensure they are secure, but don't over-tighten as to break the 3D print or the plastic housing on the motor.
  9. Snip the excess zip tie material from each.
  10. Repeat the steps above for the other motor.

Step 4: Assemble the Rover Body Power Source

Now we will add the power source for the rover.

  1. Grab the battery holder case, open it, and add the 6x 1.5V (AA) batteries. Make sure the switch is in the OFF position.
  2. Place the cover on the battery holder case.
  3. Orient the rover body so that the round hole for the caster wheel is at the top, and the square micro servo holder & wheel motors are at the bottom.
  4. Insert the battery holder with the DC plug, and ON/OFF switch into the slot. It should fit snug.
  5. Make sure that the orientation of the battery holder has the DC plug and ON/OFF switch pointing to the left.
  6. Insert the large (8-inch) zip tie from the top into the slot hole of the support keeping the battery in place down through the bottom slot hole.
  7. Grab another large (8-inch) zip tie, and insert the locking mechanism. Push up to the 3D print support, but ensure it remains slightly loose. We don't want to break the support by over-tightening it.
  8. Snip the excess zip tie material.
  9. Flip the entire rover body over. We are now ready to start adding the other electronic components.

Step 5: Assemble/Position the Arduino, Breadboard, & Motor Shield

Now we will Assemble/Position the Arduino, Breadboard, & Motor Shield.

  1. Orient the rover body so that the round hole for the caster wheel is on top, and the square hole for the micro servo is on the bottom.
  2. Grab the breadboard, and remove the adhesive backing.
  3. Place the breadboard about 0.5 inches (13mm) from the bottom edge of the rover body. The alphabetical labels should be on the left and right, and the numerical labels should be on the top and bottom.
  4. Next, grab the Arduino UNO, and place it so that the holes on the 3D printed rover body line up with the 4 holes on the Arduino body. The DC power plug, and USB port should be facing towards the right.
  5. Grab two small (4-inch) zip ties. Insert into the hole from underneath on the side that has the battery holder. Secure them tightly. Snip the excess material.
  6. Grab the Adafruit Motor Shield, and stack it on top on the Arduino UNO. The pins only stack in one direction. make sure they are stacked correctly.

Step 6: Connect Micro Servo, Sonar Sensor, & DC Motors

Here we install the Servo, Sonar Sensor, and connect the DC motors.

  1. Insert the Micro Servo into the square holder on the 3D printed rover body. It does not matter which way it is oriented.
  2. Connect the servo cable to the "SER1" connection on the Motor Shield. The lightest colored wire is for "S" (signal/data), the darkest colored wire is for "-" (negative/ground), the middle wire is for "+" (positive/5V).
  3. Now lets move on to the DC motors. Connect the 2 jumper wires from the DC motors to the motor shield using a small screwdriver. We will check the spin of the DC motors later on. For now lets make sure they are connected.
  4. One DC motor will connect to the two "M1" ports, and the other will connect to the two "M3" ports.
  5. Now, on to the Sonar Sensor. Grab 4 Female-to-Male jumper wires, and connect the female ends to the "GND,ECHO,TRIG, VCC" male pins on the sonar sensor.
  6. Now, grab 4 flat jumper wires, and place them around the middle of the breadboard (~Rows 14-17, columns E-F) in order to extend the rows.
  7. Place the completed Sonar Sensor and holder onto the micro servo. Ensure that the nylon control bar lines up with the black lines on the servo.
  8. Connect the male ends of the jumper wires from the Sonar Sensor to the breadboard. Column J: GND-Row17, ECHO-Row16, TRIG-Row15, VCC-Row14.
  9. Grab 4 more Female-to-Male jumper wires, and connect them on the breadboard to Column A, Rows14-17.
  10. Connect Row 17 jumper wire to "GND" on the motor shield (middle row, first pin from the right).
  11. Connect Row 16 jumper wire to "A5" on the motor shield (bottom row, first pin from the right).
  12. Connect Row 15 jumper wire to "A4" on the motor shield (bottom row, second pin from the right).
  13. Connect Row 14 jumper wire to "+5" on the motor shield (top row, first pin from the right).

You have now completed your Arduino Ground Rover. The only things missing are the wheels, but we don't want to connect those until we are sure the micro servo, and the DC motors are spinning/moving in the right directions.

Step 7: Calibrate Micro Servo and DC Motors Using Arduino Code

Now we will start playing with the Arduino code to calibrate the Micro Servo and DC motors.

  1. If you have not done so already, dowload the Arduino IDE software from Arduino website. I prefer the Legacy IDE, but it is up to you.
  2. Once the software has been downloaded, and installed, open the "Library Manager" and download the "Servo" library from Arduino, and download the "Adafruit Motor Shield Library" from Adafruit Version 1.0.1.
  3. Now let's look at the Servo "Sweep" example. In the Arduino IDE, go to File->Examples->Examples from Custom Libraries, Servo->Sweep.
  4. On line 18 of the code, change "myservo.attach(9);" to "myservo.attach(10);
  5. Connect the USB cable to your computer, and to the Arduino board.
  6. Upload the code.
  7. Observe the movement of the Sonar Holder. If the sonar does not point from 0 degrees to 180 degrees, then unplug the USB cable and adjust the Sonar holder accordingly. Plug the cable back in, and check to see if it lines up correctly. Keep doing this until you are satisfied with the alignment. Tip: Grab a fine tip black pen or marker, and draw a line on the micro servo rotating head at the 0 degree and 180 degree marks. Then if you have to remove the sonar holder, you know exactly how to align it in the future.
  8. Now let's look at the Motor Shield "MotorTest" example. In the Arduino IDE, go to File->Examples->Examples from Custom Libraries, Adafruit Motor Shield library->MotorTest.
  9. On line 7 of the code, change "AF_DCMotor motor(4);" to "AF_DCMotor motor(1);"
  10. Connect the USB cable to your computer, and to the Arduino board.
  11. Upload the code.
  12. Orient the rover body so that the Micro Servo square holder is at the top, and the round caster wheel holder is at the bottom. This means "M1" motor 1 will be on your left, and "M3" motor 3 will be on your right.
  13. If the wires from the DC motor are connected correctly, you should see the left motor (M1) go forwards, then go backwards, then stop, then repeat the whole process again forever in a loop. If they are spinning in the opposite direction, then swap the jumper wires going into M1. If you are satisfied with how it moves then unplug the USB cable, and test the other motor.
  14. On line 7 of the code, change "AF_DCMotor motor(1);" to "AF_DCMotor motor(3);"
  15. Connect the USB cable to your computer, and to the Arduino board.
  16. Upload the code.
  17. If the wires from the DC motor are connected correctly, you should see the right motor go forwards, then go backwards, then stop, then repeat the whole process again forever in a loop. If they are spinning in the opposite direction, then swap the jumper wires going into M1. If you are satisfied with how it moves then unplug the USB cable, and you are done with this step.

Step 8: Uploading the Ground Rover Arduino Code

Here is the final step. We will now upload the Ground Rover code that combines the Arduino UNO, Motor Shield, Servo, Motor, and Sonar Sensor. This is still a work in progress on my end and has some glitches. I will be updating the code in the future, but in the mean time feel free to play around, and make modifications to the code as you see fit. This code is a combination of multiple sources from all over the internet. If you find something that works easier, better, or faster please let me know. We are all in this together, and when we share knowledge then the whole collective benefits from that progress. Now on to the last few steps.

  1. Open the Arduino IDE.
  2. Open the "Library Manager" and download the "HCSR04" library from Martin Sosic v2.0.0.
  3. Download the attached Arduino Code file, "GroundRoverAFMotorShieldV1HCSR04Servo.ino", and open it in the Arduino IDE.
  4. It will ask you to place this in a folder in the Arduino directory. Go ahead and do that.
  5. Make sure you can still view the code in the Arduino IDE.
  6. Connect the USB cable to your computer, and to the Arduino board.
  7. Upload the code, and once it finishes uploading then disconnect the USB cable from the Rover.
  8. Now for the moment of truth!!!
  9. Pick up the rover, and make sure that the battery holder switch is in the OFF position.
  10. Connect the DC power plug from the battery holder to the Arduino UNO board's power jack.
  11. Add the wheels to the left and right DC motors.
  12. Add the caster wheel to the rear round hole on the rover body.
  13. Find an open spot on the ground with at least 5ft of clearance in every direction. This works best on flat surfaces, not carpet. It is very difficult for the rover to move on carpeted surfaces.
  14. Place the rover on the floor, and flip the battery holder switch to the ON position.
  15. There is a 5 second delay before the rover starts moving. This should give you enough time to get out of the way.
  16. Enjoy the show!!!

This is by no means a perfect product, but it is a great introduction to robotic systems and platforms. Keep tweaking and adjusting the code, change the parameters and see what happens. Use the Code examples from Servo "Sweep", and Adafruit Motor Shield library "MotorTest" to make your own code, and test out different scenarios.

Step 9: GroundRoverAFMotorShieldV1HCSR04Servo Code

// Detecting Collisions with an Ultrasonic Distance Sensor on a Servo using the Adafruit Motor
// Shield v1
// This is a combination of various code sources, and is still a work in progress.
#include <AFMotor.h>
#include <HCSR04.h>
#include <Servo.h>

// set up instances of each motor
AF_DCMotor motorL(1);
AF_DCMotor motorR(3);


// set up ultrasonic sensor
UltraSonicDistanceSensor HCSR04(A4, A5); // trig - LEFT, echo - RIGHT

// set up Servo
Servo myservo;

boolean crash = false;

void checkDistance() {
 float distance;
 distance = HCSR04.measureDistanceCm();
  if (distance < 8) // crash distance is 10 cm or less
 {
   crash = true;
 }
}

void goBackward(int speed, int duration) {
 motorL.setSpeed(speed);
 motorR.setSpeed(speed);

 motorL.run(BACKWARD);
 motorR.run(BACKWARD);

 delay(duration);
 motorL.run(RELEASE);
 motorR.run(RELEASE);

}

void goForward(int duration, int speed)
{
 long a, b;
 boolean noMove = true;
 a = millis();
 do
 {
   checkDistance();
   if (crash == false) {
     motorL.setSpeed(speed);
     motorR.setSpeed(speed);

     motorL.run(FORWARD);
     motorR.run(FORWARD);
   }
   if (crash == true) {
     motorL.run(RELEASE);
     motorR.run(RELEASE);
     goBackward(120, 1000);
     myservo.write(45);
     checkDistance();
     delay(2000);
     myservo.write(90);
     motorL.setSpeed(speed);
     motorR.setSpeed(speed);
     motorL.run(FORWARD);
     motorR.run(BACKWARD);
     delay(500);
     crash = false;
   }

   b = millis() - a;
   if (b >= duration)
   {
     noMove = false;
   }
 }
 while (noMove != false);
 // stop motors
 motorL.run(RELEASE);
 motorR.run(RELEASE);
}

void setup() {
 myservo.attach(10);
 myservo.write(90);
 Serial.begin(9600);
 delay(5000);
}

void loop() {
 goForward(1000, 150);
 Serial.println(HCSR04.measureDistanceCm());
 delay(10);
}