Arduino - Obstacle Avoiding Robot 4WD

by MertArduino in Circuits > Arduino

68683 Views, 141 Favorites, 0 Comments

Arduino - Obstacle Avoiding Robot 4WD

img.jpg

Arduino Tutorial Obstacle Avoiding Robot 4WD - For beginners

In this tutorial, you will make obstacle avoiding robot.

Video Tutorial

Arduino Project Tutorial 11 : Obstacle Avoiding Robot 4WD

This tutorial involves building a 4WD robot with an ultrasonic sensor that can detect nearby objects and change its direction to avoid these objects. The ultrasonic sensor will be attached to a servo motor which is constantly scanning left and right looking for objects in its way.

Hardware Required

vlcsnap-2016-12-05-23h52m33s164.png

  • Arduino Board
  • Motor Shield (Adafruit)
  • Ultrasonic Sensor (HC-SR04)
  • Mini Servo Motor (SG90)
  • DC Motor x4
  • Wheel x4
  • Chassis plate
  • Holder for HC-SR04
  • Battery 9V x2
  • Battery Buckle x2
  • Wires
  • Button
  • Glue Gun

Create of the Chassis

vlcsnap-2016-12-05-23h52m47s188.png
vlcsnap-2016-12-05-23h52m59s140.png
vlcsnap-2016-12-05-23h53m28s571.png
vlcsnap-2016-12-05-23h53m47s389.png
vlcsnap-2016-12-05-23h55m00s540.png
vlcsnap-2016-12-05-23h55m10s986.png
vlcsnap-2016-12-05-23h55m26s798.png
vlcsnap-2016-12-05-23h55m38s797.png
  1. The DC motors attach to the chassis
  2. The Arduino attach to the chassis
  3. The Motor Shield attach to the Arduino.
  4. The Servo motor attach to the chassis
  5. The HC-SR04 holder attach to Servo
  6. The HC-SR04 sensor attach to Servo

About the Motor Shield

vlcsnap-2016-12-05-23h54m05s233.png
vlcsnap-2016-12-05-23h54m29s379.png
vlcsnap-2016-12-05-23h54m37s987.png
vlcsnap-2016-12-05-23h54m46s303.png
vlcsnap-2016-12-05-23h54m11s119.png

  • The motor controllers on this shield are designed to run from 4.5V to 25V.
  • There are two places you can get your motor 'high voltage supply' from. One is the DC jack on the Arduino board and the other is the 2-terminal block on the shield that is labeled EXT_PWR.
  • To connect a motor, simply solder two wires to the terminals and then connect them to either the M1, M2, M3, or M4.
  • Hobby servos are the easiest way to get going with motor control. They have a 3-pin 0.1" female header connection with +5V, ground and signal inputs. The motor shield simply brings out the 16bit PWM output lines to two 3-pin headers so that its easy to plug in and go.
  • Power for the Servos comes from the Arduino's on-board 5V regulator, powered directly from the USB or DC power jack on the Arduino.

Reference

https://learn.adafruit.com/adafruit-motor-shield/overview

Connections

vlcsnap-2016-12-05-23h55m49s981.png
vlcsnap-2016-12-05-23h56m06s361.png
vlcsnap-2016-12-05-23h56m48s378.png
vlcsnap-2016-12-05-23h57m03s218.png
vlcsnap-2016-12-05-23h57m10s410.png
vlcsnap-2016-12-05-23h57m51s337.png
vlcsnap-2016-12-05-23h58m09s555.png
vlcsnap-2016-12-05-23h58m33s554.png
vlcsnap-2016-12-05-23h58m44s297.png
vlcsnap-2016-12-05-23h59m06s197.png
vlcsnap-2016-12-05-23h59m13s413.png
vlcsnap-2016-12-05-23h59m39s811.png
vlcsnap-2016-12-05-23h59m51s032.png
vlcsnap-2016-12-06-00h00m05s818.png

1. DC Motors connect to the Motor Shield

2. Left Motors connect to M1 and M2 (If the motors run backwards, change the (+) and (-) connections)

3. Right Motors connect to M4 and M3

4. The Servo motor connect to the Motor Shield

  • The Servo1 input is connected to the Arduino Digital10 input
  • The Servo2 input is connected to the Arduino Digital9 input
  • Power for the Servos comes from the Arduino's on-board 5V regulator, powered directly from the USB or DC power jack on the Arduino

5. The HC-SR04 Sensor connections

  • The HC-SR04 Sensor VCC connect to the Arduino +5V
  • The HC-SR04 Sensor GND connect to the Arduino Ground
  • The HC-SR04 Sensor Trig connect to the Arduino Analog 0
  • The HC-SR04 Sensor Echo connect to the Arduino Analog 1

6. Battery and on / off button connection

  • The (+) from the battery is connect to a leg of the button
  • Connect a cable to the other legs of the button. This cable is for Motor Shield (+)

7. 2 seperate DC power supplies for the Arduino and motors

  • If you would like to have 2 seperate DC power supplies for the Arduino and motors. Plug in the supply for the Arduino into the DC jack, and connect the motor supply to the PWR_EXT block. Make sure the jumper is removed from the motor shield.

8. Attach the wheels

Copy the Code and Install the Arduino Library

vlcsnap-2016-12-06-00h00m20s603.png
vlcsnap-2016-12-06-00h00m48s216.png
vlcsnap-2016-12-06-00h01m00s241.png
vlcsnap-2016-12-06-00h01m12s438.png
vlcsnap-2016-12-06-00h01m25s560.png
vlcsnap-2016-12-06-00h01m47s056.png
vlcsnap-2016-12-06-00h01m54s907.png
vlcsnap-2016-12-06-00h02m12s608.png

1. Open the new Arduino IDE page

2. Delete everything on the page

3. Get the code in video description

4. Paste empty Arduino IDE page

5. First Install the Arduino Library

6. The AF_Motor Arduino library

  • Before you can use the Motor shield, you must install the AF_Motor Arduino library.
  • Close the Arduino IDE
  • Download the library https://github.com/adafruit/Adafruit-Motor-Shield-library
  • Uncompress the ZIP file onto your desktop
  • Check that inside AFMotor is AFMotor.cpp and AFMotor.h files
  • Place the AFMotor folder into your arduinosketchfolder/libraries folder. For Windows, this will probably be something like MY Documents/Arduino/libraries for Mac it will be something likeDocuments/arduino/libraries. If this is the first time you are installing a library, you'll need to create the libraries folder. Make sure to call it libraries exactly, no caps, no other name.
  • Rename the uncompressed folder AFMotor

7. The NewPing Arduino Library

8. Restart the IDE

9. Now you can see the libraries you have installed

10. When you verify, you will not see any errors

About the Code

vlcsnap-2016-12-06-00h02m33s972.png
vlcsnap-2016-12-06-00h02m45s606.png
vlcsnap-2016-12-06-00h02m54s782.png
vlcsnap-2016-12-06-00h03m01s701.png

1. Using DC Motors with the Motor Shield

  • The motor shield can drive up to 4 DC motors bi-directionally. That means they can be driven forwards and backwards.
  • To connect a motor, simply solder two wires to the terminals and then connect them to either the M1, M2, M3, or M4.
  • Make sure you #include AFMotor.h
  • Create the AF_DCMotor object with AF_DCMotor(motor#, frequency), to setup the motor H-bridge and latches. The constructor takes two arguments.
  • The first is which port the motor is connected to, 1, 2, 3 or 4. frequency is how fast the speed controlling signal is.
  • For motors 1 and 2 you can choose MOTOR12_64KHZ, MOTOR12_8KHZ, MOTOR12_2KHZ, orMOTOR12_1KHZ. A high speed like 64KHz wont be audible but a low speed like 1KHz will use less power. Motors 3 & 4 are only possible to run at 1KHz and will ignore any setting given.
  • Then you can set the speed of the motor using setSpeed(speed) where the speed ranges from 0 (stopped) to 255 (full speed). You can set the speed whenever you want.
  • To run the motor, call run(direction) where direction is FORWARD, BACKWARD or RELEASE. Of course, the Arduino doesn't actually know if the motor is 'forward' or 'backward', so if you want to change which way it thinks is forward, simply swap the two wires from the motor to the shield.

2. Using the HC-SR04 with the NewPing Library

  • NewPing sonar(trigger_pin, echo_pin [, max_cm_distance]);

Example;

  • NewPing sonar(12, 11, 200);

This initializes NewPing to use pin 12 for trigger output, pin 11 for echo input, with a maximum ping distance of 200cm. max_cm_distance is optional [default = 500cm]. If connecting using a single pin, specify the same pin for both trigger_pin and echo_pin as the same pin is doing both functions.

Reference

https://learn.adafruit.com/adafruit-motor-shield/overview

http://playground.arduino.cc/Code/NewPing

If It Helps, Please Subscribe

vlcsnap-2016-12-06-00h03m13s353.png

First of all, I would like to thank you for reading this guide ! I hope it helps you.

If you want to support me, you can subscribe my channel and watch my videos.

Visit My Youtube Channel - Mert Arduino Tutorial & Projects