Introduction: Arduino - Obstacle Avoiding Robot 4WD

About: Maker 101; Beginner and intermediate level Maker projects! You can find projects such as "How to" and "DIY" on programmable boards such as Arduino, ESP8266, ESP32 and Raspberry Pi on this channel. The projects…

Arduino Tutorial Obstacle Avoiding Robot 4WD - For beginners

In this tutorial, you will make obstacle avoiding robot.

Step 1: Video Tutorial

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.

Step 2: Hardware Required

  • 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

Step 3: Create of the Chassis

  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

Step 4: About the Motor Shield

  • 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.


Step 5: Connections

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

Step 6: Copy the Code and Install the Arduino Library

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
  • 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

Step 7: About the Code

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


  • 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.


Step 8: If It Helps, Please Subscribe

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

Arduino Contest 2016

Participated in the
Arduino Contest 2016