Introduction: Obstacle Avoiding Robot


Darwin here,

This is for my Artificial Intelligence project in my school and it's my first Arduino project and I would like to share this project to you. In this tutorial making a Robot is not that hard you can make your own by following this Steps.

NOTE! To be updated for my future projects and have inquires. Please contact me thru this following social media account. I'm willing to help.

Subscribe to my Youtube channel here:

Like my Facebook:

Follow me on twitter: _____________________________________________________________

Step 1: Building the Chasis

Picture of Building the Chasis

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

Step 2: Connecting the Motor and Motorshield

Picture of Connecting the Motor and Motorshield

Solder the thick black and red wires at the motors.

Attach the front wheel. After attached the rear wheels according to the instructions leaflet that comes with kit.

The next step is to attach the switch to the battery holder. Cut the battery holder black wire and solder the one end to separate side of the switch. Our switch is ready. Then attach it to the battery holder using the screw that comes with the chassis kit.

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.

Move on to the motor shield. This shield provides power to the motors and the servo motor and makes our project 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 attach the Arduino Uno and the Battery holder to the chassis. With a double sided tape also attach the supersonic sensor to the Servo motor and both of them to the robot chassis. I used a hot glue gun to attach the servo motor to the chassis.

Step 3: Configuring the Ultrasonic Sensor and Connection to Servo Motor

Picture of Configuring the Ultrasonic Sensor and Connection to Servo Motor

In configuring the supersonic sensor we need to this steps.

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.

Lastly we connect the servo motor to the servo_2 slot. The darker color on the right end side red in the middle and orange in the left side order.

Step 4: Codes

Picture of Codes

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 here:

Motor Shield Library:

New Ping Library:


#include #include


AF_DCMotor leftMotor(4, MOTOR12_8KHZ); AF_DCMotor rightMotor(3, MOTOR12_8KHZ); Servo neckControllerServoMotor;

int pos = 0; int maxDist = 0; int maxAngle = 0; int maxRight = 0; int maxLeft = 0; int maxFront = 0; int course = 0; int curDist = 0; String motorSet = ""; int speedSet = 0;

void setup() { neckControllerServoMotor.attach(10); neckControllerServoMotor.write(90); delay(2000); checkPath(); motorSet = "FORWARD"; neckControllerServoMotor.write(90); moveForward(); }

void loop() { checkForward(); checkPath(); }

void checkPath() { int curLeft = 0; int curFront = 0; int curRight = 0; int curDist = 0; neckControllerServoMotor.write(144); delay(120); for(pos = 144; pos >= 36; pos-=18) { neckControllerServoMotor.write(pos); delay(90); checkForward(); curDist = readPing(); if (curDist < COLL_DIST) { checkCourse(); break; } if (curDist < TURN_DIST) { changePath(); } if (curDist > curDist) {maxAngle = pos;} if (pos > 90 && curDist > curLeft) { curLeft = curDist;} if (pos == 90 && curDist > curFront) {curFront = curDist;} if (pos < 90 && curDist > curRight) {curRight = curDist;} } maxLeft = curLeft; maxRight = curRight; maxFront = curFront; }

void setCourse() { if (maxAngle < 90) {turnRight();} if (maxAngle > 90) {turnLeft();} maxLeft = 0; maxRight = 0; maxFront = 0; }

void checkCourse() { moveBackward(); delay(500); moveStop(); setCourse(); }

void changePath() { if (pos < 90) {lookLeft();} if (pos > 90) {lookRight();} }

int readPing() { delay(70); unsigned int uS =; int cm = uS/US_ROUNDTRIP_CM; return cm; }

void checkForward() { if (motorSet=="FORWARD") {;; } }

void checkBackward() { if (motorSet=="BACKWARD") {;; } }

void moveStop() {;;}

void moveForward() { motorSet = "FORWARD";;; for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) { leftMotor.setSpeed(speedSet+MOTORS_CALIBRATION_OFFSET); rightMotor.setSpeed(speedSet); delay(5); } }

void moveBackward() { motorSet = "BACKWARD";;; for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) { leftMotor.setSpeed(speedSet+MOTORS_CALIBRATION_OFFSET); rightMotor.setSpeed(speedSet); delay(5); } }

void turnRight() { motorSet = "RIGHT";;; delay(400); motorSet = "FORWARD";;; }

void turnLeft() { motorSet = "LEFT";;; delay(400); motorSet = "FORWARD";;; }

void lookRight() {; delay(400);;} void lookLeft() {; delay(400);;}

Step 5: Schematic Diagram

Picture of Schematic Diagram


YigitElectroMan (author)2017-09-24

Thanks for sharing. Btw I thing the link to the newping library is wrong

Swansong (author)2017-04-10

Thanks for sharing :)

Your welcome :) Mabuhay!

About This Instructable




Bio: Mabuhay! I'm a Filipino maker that build Artificial Intelligence and Robotic projects. I'm also a tester and reviewer of latest technology.
More by Darwin Dela Cruz:Portable Monitoring System Using Raspberry Pi (2 USB Camera)Obstacle Avoiding Robot
Add instructable to: