Arduino Ground Rover With Adafruit Motor Shield V1 and Servo Sonar Sensor
by ProjectSerge in Circuits > Arduino
1016 Views, 2 Favorites, 0 Comments
Arduino Ground Rover With Adafruit Motor Shield V1 and Servo Sonar Sensor
![IMG_0073.JPG](/proxy/?url=https://content.instructables.com/FMP/NKV8/LIVQ302G/FMPNKV8LIVQ302G.jpg&filename=IMG_0073.JPG)
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
![6-cell Battery Holder Case OnOffSwitch DC Plug.jpg](/proxy/?url=https://content.instructables.com/FQD/YHH0/LIX5IQ1O/FQDYHH0LIX5IQ1O.jpg&filename=6-cell Battery Holder Case OnOffSwitch DC Plug.jpg)
![9G Micro Servo.jpg](/proxy/?url=https://content.instructables.com/F0P/T00A/LIX5IQ1P/F0PT00ALIX5IQ1P.jpg&filename=9G Micro Servo.jpg)
![400 Tie-point Breadboard.jpg](/proxy/?url=https://content.instructables.com/F5G/Z81X/LIX5IQ1Q/F5GZ81XLIX5IQ1Q.jpg&filename=400 Tie-point Breadboard.jpg)
![Adafruit Motor Shield V1.jpg](/proxy/?url=https://content.instructables.com/FQL/J9Q7/LIX5IQ1R/FQLJ9Q7LIX5IQ1R.jpg&filename=Adafruit Motor Shield V1.jpg)
![Arduino Ground Rover.JPG](/proxy/?url=https://content.instructables.com/FMZ/A8XC/LIX5IQ1S/FMZA8XCLIX5IQ1S.jpg&filename=Arduino Ground Rover.JPG)
![Arduino UNO Clone.jpg](/proxy/?url=https://content.instructables.com/FZV/G3MW/LIX5IQ22/FZVG3MWLIX5IQ22.jpg&filename=Arduino UNO Clone.jpg)
![HC-SR04 Sonar Sensor.jpg](/proxy/?url=https://content.instructables.com/FWG/C5KK/LIX5IQ23/FWGC5KKLIX5IQ23.jpg&filename=HC-SR04 Sonar Sensor.jpg)
![Jumper Wires.jpg](/proxy/?url=https://content.instructables.com/FC4/HZNB/LIX5IQ24/FC4HZNBLIX5IQ24.jpg&filename=Jumper Wires.jpg)
![Pliers and Screwdrivers.jpg](/proxy/?url=https://content.instructables.com/FIZ/7CVR/LIX5IQ25/FIZ7CVRLIX5IQ25.jpg&filename=Pliers and Screwdrivers.jpg)
![Robot Wheels and DC Gear Motors.jpg](/proxy/?url=https://content.instructables.com/FJK/EG3H/LIX5IQ2F/FJKEG3HLIX5IQ2F.jpg&filename=Robot Wheels and DC Gear Motors.jpg)
![Rover Body and Sonar Sensor.jpg](/proxy/?url=https://content.instructables.com/F2T/98L7/LIX5IQ2H/F2T98L7LIX5IQ2H.jpg&filename=Rover Body and Sonar Sensor.jpg)
![USB-A to USB-B Cable.jpg](/proxy/?url=https://content.instructables.com/FP2/0F7A/LIX5IQ32/FP20F7ALIX5IQ32.jpg&filename=USB-A to USB-B Cable.jpg)
![Zip Ties.jpg](/proxy/?url=https://content.instructables.com/F2O/ZYDC/LIX5IQ33/F2OZYDCLIX5IQ33.jpg&filename=Zip Ties.jpg)
Parts/Bill of Materials
- 1x Arduino UNO R3 microcontroller (or similar clone).
- Arduino IDE (software download from Arduino.cc)
- USB Cable to upload code
- 1x Adafruit Motor Shield V1 (or similar clone)
- 1x HC-SR04 Ultrasonic Sonar Sensor
- 1x 9g Micro Servo w/ nylon control arm
- 1x 400 tie-point breadboard
- 1x Battery Holder Case, 6x1.5V (AA battery) w/ DC 5.5x2.1mm Male Connector, ON/Off Switch
- 2x Robot Plastic Tire Wheel with DC 3-6V Gear Motor
- 1x 2 inch Caster Wheel
- 6x AA batteries (1.5V)
- Various Zip Ties (mainly 4-inch, 8-inch)
- 8x 4-inch zip ties
- 2x 8-inch zip ties
- Various Breadboard Jumper Wires
- 12x 5-inch Male-to-Female jumper wires
- 6x U-shape flat connecting line jumper wires
- Robot Car Chassis kit, or Access to 3D printer for printing Arduino Rover Body and Sonar Holder.
- Some Robot Car Chassis kits come with the caster wheel, robot tires with DC gear motors, and battery holder.
Tools for Assembly
- Pliers
- Small Flat-head or Philips Screwdriver
Print Rover Body and Sonar Sensor
![Ultimaker Cura Software Arduino Robot Car.png](/proxy/?url=https://content.instructables.com/FWR/J3D4/LIVQ3O60/FWRJ3D4LIVQ3O60.png&filename=Ultimaker Cura Software Arduino Robot Car.png)
![Ultimaker Cura Software Sonar Holder.png](/proxy/?url=https://content.instructables.com/FAM/5T0Y/LIVQ3O61/FAM5T0YLIVQ3O61.png&filename=Ultimaker Cura Software Sonar Holder.png)
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.
- Download the attached STL files for the Arduino Rover Body, and HC-SR04 Sonar Sensor Holder.
- Print them using an available 3D printer.
- The following was used in order to print:
- Creality CR-10 3D printer
- Ultimaker Cura Printing Software
- 1.75mm PLA Filament
- Orient the STL files in the 3D printing software so that the largest flat surface is facing down towards the printing plate.
- The Arduino Rover Body will take approximately 6-7hrs to print. I did not use any supports for printing.
- The Sonar Sensor Holder will take approximately 30mins to print. I printed with supports.
Assemble the Sonar Sensor Holder
![Sonar Holder.jpg](/proxy/?url=https://content.instructables.com/FO3/Y8X7/LIX5IQ2R/FO3Y8X7LIX5IQ2R.jpg&filename=Sonar Holder.jpg)
![Sonar with Nylon Control Bar.jpg](/proxy/?url=https://content.instructables.com/FXX/ATJH/LIX5IQ2S/FXXATJHLIX5IQ2S.jpg&filename=Sonar with Nylon Control Bar.jpg)
![Sonar Holder 1.JPG](/proxy/?url=https://content.instructables.com/F9P/9X2O/LIX5IQZI/F9P9X2OLIX5IQZI.jpg&filename=Sonar Holder 1.JPG)
![Sonar Holder 2.JPG](/proxy/?url=https://content.instructables.com/FVG/7L8E/LIX5IQZJ/FVG7L8ELIX5IQZJ.jpg&filename=Sonar Holder 2.JPG)
![Sonar Holder 3.JPG](/proxy/?url=https://content.instructables.com/FGA/X0S2/LIX5IQZK/FGAX0S2LIX5IQZK.jpg&filename=Sonar Holder 3.JPG)
![Sonar Holder 4.JPG](/proxy/?url=https://content.instructables.com/FJF/VK74/LIX5IQZL/FJFVK74LIX5IQZL.jpg&filename=Sonar Holder 4.JPG)
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.
- Grab the nylon control bar from the micro servo, and insert it into the designated slot on the 3D print.
- Use two 4-inch zip ties to secure the control bar. Once it is tightened enough, snip/cut the excess from the zip ties.
- 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.
Assemble the Rover Body Wheel Base
![IMG_0097.jpg](/proxy/?url=https://content.instructables.com/FWW/ZYZ6/LIX5ITQY/FWWZYZ6LIX5ITQY.jpg&filename=IMG_0097.jpg)
![IMG_0099.jpg](/proxy/?url=https://content.instructables.com/FFB/CWHZ/LIX5ITQZ/FFBCWHZLIX5ITQZ.jpg&filename=IMG_0099.jpg)
![IMG_0100.jpg](/proxy/?url=https://content.instructables.com/F5E/85U6/LIX5ITR0/F5E85U6LIX5ITR0.jpg&filename=IMG_0100.jpg)
![IMG_0101.jpg](/proxy/?url=https://content.instructables.com/F5D/ZF0Q/LIX5ITR1/F5DZF0QLIX5ITR1.jpg&filename=IMG_0101.jpg)
![IMG_0102.jpg](/proxy/?url=https://content.instructables.com/FEI/JR70/LIX5ITR2/FEIJR70LIX5ITR2.jpg&filename=IMG_0102.jpg)
![IMG_0103.jpg](/proxy/?url=https://content.instructables.com/F5J/TELV/LIX5ITR3/F5JTELVLIX5ITR3.jpg&filename=IMG_0103.jpg)
![IMG_0104.jpg](/proxy/?url=https://content.instructables.com/FU6/UG47/LIX5ITR4/FU6UG47LIX5ITR4.jpg&filename=IMG_0104.jpg)
Now you will start putting together the main body of the rover.
- Place the flat surface of the rover body down on your working surface.
- Add the two DC Gear Motors without the wheels attached.
- Use two small (4-inch) zip ties per wheel.
- Insert one zip tie (#1) into the top hole that lines up with the motor.
- Tuck the same remaining zip tie (#1) back into the bottom hole.
- 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.
- Now, insert the end of the bottom zip tie (#1) into the locking mechanism of the second zip tie (#2).
- 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.
- Snip the excess zip tie material from each.
- Repeat the steps above for the other motor.
Assemble the Rover Body Power Source
![IMG_0106.jpg](/proxy/?url=https://content.instructables.com/F3R/SAO1/LIX5IXZO/F3RSAO1LIX5IXZO.jpg&filename=IMG_0106.jpg)
![IMG_0107.jpg](/proxy/?url=https://content.instructables.com/FH8/33IA/LIX5IXZP/FH833IALIX5IXZP.jpg&filename=IMG_0107.jpg)
![IMG_0108.jpg](/proxy/?url=https://content.instructables.com/F93/CKMB/LIX5IXZQ/F93CKMBLIX5IXZQ.jpg&filename=IMG_0108.jpg)
![IMG_0109.jpg](/proxy/?url=https://content.instructables.com/FWH/ATAK/LIX5IXZR/FWHATAKLIX5IXZR.jpg&filename=IMG_0109.jpg)
![IMG_0110.jpg](/proxy/?url=https://content.instructables.com/FSO/2NU7/LIX5IXZS/FSO2NU7LIX5IXZS.jpg&filename=IMG_0110.jpg)
![IMG_0111.jpg](/proxy/?url=https://content.instructables.com/FSR/SYBJ/LIX5IXZT/FSRSYBJLIX5IXZT.jpg&filename=IMG_0111.jpg)
Now we will add the power source for the rover.
- Grab the battery holder case, open it, and add the 6x 1.5V (AA) batteries. Make sure the switch is in the OFF position.
- Place the cover on the battery holder case.
- 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.
- Insert the battery holder with the DC plug, and ON/OFF switch into the slot. It should fit snug.
- Make sure that the orientation of the battery holder has the DC plug and ON/OFF switch pointing to the left.
- 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.
- 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.
- Snip the excess zip tie material.
- Flip the entire rover body over. We are now ready to start adding the other electronic components.
Assemble/Position the Arduino, Breadboard, & Motor Shield
![IMG_0112.jpg](/proxy/?url=https://content.instructables.com/FK7/N8W5/LIX5J2OX/FK7N8W5LIX5J2OX.jpg&filename=IMG_0112.jpg)
![IMG_0113.jpg](/proxy/?url=https://content.instructables.com/FV7/E87Q/LIX5J2OY/FV7E87QLIX5J2OY.jpg&filename=IMG_0113.jpg)
![IMG_0114.jpg](/proxy/?url=https://content.instructables.com/FJP/ETM4/LIX5J2OZ/FJPETM4LIX5J2OZ.jpg&filename=IMG_0114.jpg)
![IMG_0115.jpg](/proxy/?url=https://content.instructables.com/FD8/7VD3/LIX5J2P0/FD87VD3LIX5J2P0.jpg&filename=IMG_0115.jpg)
![IMG_0116.jpg](/proxy/?url=https://content.instructables.com/FTZ/G3T1/LIX5J2P1/FTZG3T1LIX5J2P1.jpg&filename=IMG_0116.jpg)
![IMG_0117.jpg](/proxy/?url=https://content.instructables.com/FLF/FSLZ/LIX5J2P2/FLFFSLZLIX5J2P2.jpg&filename=IMG_0117.jpg)
![IMG_0118.jpg](/proxy/?url=https://content.instructables.com/F4X/A57Z/LIX5J2P3/F4XA57ZLIX5J2P3.jpg&filename=IMG_0118.jpg)
![IMG_0119.jpg](/proxy/?url=https://content.instructables.com/F2E/PRDB/LIX5J2P4/F2EPRDBLIX5J2P4.jpg&filename=IMG_0119.jpg)
Now we will Assemble/Position the Arduino, Breadboard, & Motor Shield.
- 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.
- Grab the breadboard, and remove the adhesive backing.
- 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.
- 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.
- 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.
- 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.
Connect Micro Servo, Sonar Sensor, & DC Motors
![IMG_0120.JPG](/proxy/?url=https://content.instructables.com/FD2/F661/LIX5JDEE/FD2F661LIX5JDEE.jpg&filename=IMG_0120.JPG)
![IMG_0121.JPG](/proxy/?url=https://content.instructables.com/FVU/RN2K/LIX5JDEL/FVURN2KLIX5JDEL.jpg&filename=IMG_0121.JPG)
![IMG_0122.JPG](/proxy/?url=https://content.instructables.com/FMM/XJV9/LIX5JDEO/FMMXJV9LIX5JDEO.jpg&filename=IMG_0122.JPG)
![IMG_0123.JPG](/proxy/?url=https://content.instructables.com/F30/ADF1/LIX5JDEP/F30ADF1LIX5JDEP.jpg&filename=IMG_0123.JPG)
![IMG_0124.JPG](/proxy/?url=https://content.instructables.com/F96/KO1X/LIX5JDEQ/F96KO1XLIX5JDEQ.jpg&filename=IMG_0124.JPG)
![IMG_0125.JPG](/proxy/?url=https://content.instructables.com/FX9/KEY1/LIX5JDET/FX9KEY1LIX5JDET.jpg&filename=IMG_0125.JPG)
![IMG_0126.JPG](/proxy/?url=https://content.instructables.com/FQA/3E7D/LIX5JDEU/FQA3E7DLIX5JDEU.jpg&filename=IMG_0126.JPG)
![IMG_0127.JPG](/proxy/?url=https://content.instructables.com/FLH/IY9O/LIX5JDEV/FLHIY9OLIX5JDEV.jpg&filename=IMG_0127.JPG)
![IMG_0128.JPG](/proxy/?url=https://content.instructables.com/FGW/RO7Y/LIX5JDEW/FGWRO7YLIX5JDEW.jpg&filename=IMG_0128.JPG)
![IMG_0129.JPG](/proxy/?url=https://content.instructables.com/FWB/UPSC/LIX5JDEX/FWBUPSCLIX5JDEX.jpg&filename=IMG_0129.JPG)
![IMG_0130.JPG](/proxy/?url=https://content.instructables.com/FWD/L7PQ/LIX5JDEY/FWDL7PQLIX5JDEY.jpg&filename=IMG_0130.JPG)
![IMG_0131.JPG](/proxy/?url=https://content.instructables.com/FWT/0654/LIX5JDEZ/FWT0654LIX5JDEZ.jpg&filename=IMG_0131.JPG)
![IMG_0132.JPG](/proxy/?url=https://content.instructables.com/FCT/QNNA/LIX5JDF0/FCTQNNALIX5JDF0.jpg&filename=IMG_0132.JPG)
![IMG_0133.JPG](/proxy/?url=https://content.instructables.com/F12/76RX/LIX5JDF1/F1276RXLIX5JDF1.jpg&filename=IMG_0133.JPG)
Here we install the Servo, Sonar Sensor, and connect the DC motors.
- Insert the Micro Servo into the square holder on the 3D printed rover body. It does not matter which way it is oriented.
- 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).
- 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.
- One DC motor will connect to the two "M1" ports, and the other will connect to the two "M3" ports.
- 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.
- 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.
- 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.
- 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.
- Grab 4 more Female-to-Male jumper wires, and connect them on the breadboard to Column A, Rows14-17.
- Connect Row 17 jumper wire to "GND" on the motor shield (middle row, first pin from the right).
- Connect Row 16 jumper wire to "A5" on the motor shield (bottom row, first pin from the right).
- Connect Row 15 jumper wire to "A4" on the motor shield (bottom row, second pin from the right).
- 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.
Calibrate Micro Servo and DC Motors Using Arduino Code
![1 Arduino IDE Library Mgr Servo.png](/proxy/?url=https://content.instructables.com/FCZ/N88A/LIX5JSMT/FCZN88ALIX5JSMT.png&filename=1 Arduino IDE Library Mgr Servo.png)
![2 Arduino IDE Library Mgr Adafruit Motor Shield V1.png](/proxy/?url=https://content.instructables.com/F98/17R5/LIX5JSMU/F9817R5LIX5JSMU.png&filename=2 Arduino IDE Library Mgr Adafruit Motor Shield V1.png)
![3 Arduino IDE Servo Sweep Code.png](/proxy/?url=https://content.instructables.com/FO3/AR7V/LIX5JSMV/FO3AR7VLIX5JSMV.png&filename=3 Arduino IDE Servo Sweep Code.png)
![4 Arduino IDE Motor Shield Code.png](/proxy/?url=https://content.instructables.com/FAH/N2LB/LIX5JSMX/FAHN2LBLIX5JSMX.png&filename=4 Arduino IDE Motor Shield Code.png)
![IMG_0134.jpg](/proxy/?url=https://content.instructables.com/FZC/WQZH/LIX5JSMY/FZCWQZHLIX5JSMY.jpg&filename=IMG_0134.jpg)
![IMG_0135.jpg](/proxy/?url=https://content.instructables.com/FN6/GCHD/LIX5JSMZ/FN6GCHDLIX5JSMZ.jpg&filename=IMG_0135.jpg)
![IMG_0136.jpg](/proxy/?url=https://content.instructables.com/F2M/G5UY/LIX5JSN0/F2MG5UYLIX5JSN0.jpg&filename=IMG_0136.jpg)
Now we will start playing with the Arduino code to calibrate the Micro Servo and DC motors.
- 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.
- 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.
- Now let's look at the Servo "Sweep" example. In the Arduino IDE, go to File->Examples->Examples from Custom Libraries, Servo->Sweep.
- On line 18 of the code, change "myservo.attach(9);" to "myservo.attach(10);
- Connect the USB cable to your computer, and to the Arduino board.
- Upload the code.
- 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.
- 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.
- On line 7 of the code, change "AF_DCMotor motor(4);" to "AF_DCMotor motor(1);"
- Connect the USB cable to your computer, and to the Arduino board.
- Upload the code.
- 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.
- 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.
- On line 7 of the code, change "AF_DCMotor motor(1);" to "AF_DCMotor motor(3);"
- Connect the USB cable to your computer, and to the Arduino board.
- Upload the code.
- 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.
Uploading the Ground Rover Arduino Code
![1 Final Arduino Code and Movement.png](/proxy/?url=https://content.instructables.com/F1V/95Q5/LIX5KAV2/F1V95Q5LIX5KAV2.png&filename=1 Final Arduino Code and Movement.png)
![IMG_0137.jpg](/proxy/?url=https://content.instructables.com/FXM/AOQZ/LIX5KAV3/FXMAOQZLIX5KAV3.jpg&filename=IMG_0137.jpg)
![IMG_0138.jpg](/proxy/?url=https://content.instructables.com/FST/OHY7/LIX5KAV4/FSTOHY7LIX5KAV4.jpg&filename=IMG_0138.jpg)
![IMG_0139.jpg](/proxy/?url=https://content.instructables.com/FHH/79VL/LIX5KAV5/FHH79VLLIX5KAV5.jpg&filename=IMG_0139.jpg)
![IMG_0140.jpg](/proxy/?url=https://content.instructables.com/FFK/HWUF/LIX5KAV6/FFKHWUFLIX5KAV6.jpg&filename=IMG_0140.jpg)
![IMG_0141.JPG](/proxy/?url=https://content.instructables.com/FJS/0227/LIX5KAV7/FJS0227LIX5KAV7.jpg&filename=IMG_0141.JPG)
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.
- Open the Arduino IDE.
- Open the "Library Manager" and download the "HCSR04" library from Martin Sosic v2.0.0.
- Download the attached Arduino Code file, "GroundRoverAFMotorShieldV1HCSR04Servo.ino", and open it in the Arduino IDE.
- It will ask you to place this in a folder in the Arduino directory. Go ahead and do that.
- Make sure you can still view the code in the Arduino IDE.
- Connect the USB cable to your computer, and to the Arduino board.
- Upload the code, and once it finishes uploading then disconnect the USB cable from the Rover.
- Now for the moment of truth!!!
- Pick up the rover, and make sure that the battery holder switch is in the OFF position.
- Connect the DC power plug from the battery holder to the Arduino UNO board's power jack.
- Add the wheels to the left and right DC motors.
- Add the caster wheel to the rear round hole on the rover body.
- 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.
- Place the rover on the floor, and flip the battery holder switch to the ON position.
- There is a 5 second delay before the rover starts moving. This should give you enough time to get out of the way.
- 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.
GroundRoverAFMotorShieldV1HCSR04Servo Code
![1 Arduino Code.png](/proxy/?url=https://content.instructables.com/F84/DBDM/LIX5KB1T/F84DBDMLIX5KB1T.png&filename=1 Arduino Code.png)
![2 Arduino Code.png](/proxy/?url=https://content.instructables.com/F36/NTRL/LIX5KB1X/F36NTRLLIX5KB1X.png&filename=2 Arduino Code.png)
![3 Arduino Code.png](/proxy/?url=https://content.instructables.com/F9Y/DZQZ/LIX5KB1Y/F9YDZQZLIX5KB1Y.png&filename=3 Arduino Code.png)
// 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);
}