4WD Obstacle Avoiding Robot - Arduino Smart Robot Car

Published

Introduction: 4WD Obstacle Avoiding Robot - Arduino Smart Robot Car

About: I`m 12 and I`m from Ukraine

Hi everyone. This my new project 4WD Obstacle Avoiding Smart Robot Car with Arduino Uno, Servo & HC-SR04.

This is a simple design robot you can easily build it.

Step 1: Requirements:

Project requirements:

Arduino uno R3

Ultrasonic Sensor HC-SR04

Servo Motor SG-90

Chassis 4WD with DC Motor and Wheel x4

9v Battery

Step 2: Wiring Diagram

Make Connections as in Wiring Diagram image above.

Step 3: Upload Robot Code

#include

#define TRIG 8

#define ECHO 9

#define IN_11 0 // L298N #1 in 1 motor Right Ahead

#define IN_12 1 // L298N #1 in 2 motor Right Ahead

#define IN_13 2 // L298N #1 in 3 motor Right Back

#define IN_14 3 // L298N #1 in 4 motor Right Back

#define IN_21 4 // L298N #2 in 1 motor Left Ahead

#define IN_22 5 // L298N #2 in 2 motor Left Ahead

#define IN_23 6 // L298N #2 in 3 motor Left Back

#define IN_24 7 // L298N #2 in 4 motor Left Back

Servo servo;

int angle = 90;

int lookLeft = 0;

int lookRight = 0;

int lookAhead = 0;

int timeEcho;

void setup()

{

servo.attach(10);

pinMode(TRIG, OUTPUT);

pinMode(ECHO, INPUT);

pinMode(IN_11, OUTPUT);

pinMode(IN_12, OUTPUT);

pinMode(IN_13, OUTPUT);

pinMode(IN_14, OUTPUT);

pinMode(IN_21, OUTPUT);

pinMode(IN_22, OUTPUT);

pinMode(IN_23, OUTPUT);

pinMode(IN_24, OUTPUT);

}

void goAhead(){

digitalWrite(IN_11, HIGH); digitalWrite(IN_12, LOW);

digitalWrite(IN_13, LOW); digitalWrite(IN_14, HIGH);

digitalWrite(IN_21, LOW); digitalWrite(IN_22, HIGH);

digitalWrite(IN_23, HIGH); digitalWrite(IN_24, LOW); }

void goBack(){

digitalWrite(IN_11, LOW); digitalWrite(IN_12, HIGH);

digitalWrite(IN_13, HIGH); digitalWrite(IN_14, LOW);

digitalWrite(IN_21, HIGH); digitalWrite(IN_22, LOW);

digitalWrite(IN_23, LOW); digitalWrite(IN_24, HIGH); }

void goRight(){

digitalWrite(IN_11, LOW); digitalWrite(IN_12, HIGH);

digitalWrite(IN_13, HIGH); digitalWrite(IN_14, LOW);

digitalWrite(IN_21, LOW); digitalWrite(IN_22, HIGH);

digitalWrite(IN_23, HIGH); digitalWrite(IN_24, LOW);

}

void goLeft(){

digitalWrite(IN_11, HIGH); digitalWrite(IN_12, LOW);

digitalWrite(IN_13, LOW); digitalWrite(IN_14, HIGH);

digitalWrite(IN_21, HIGH); digitalWrite(IN_22, LOW);

digitalWrite(IN_23, LOW); digitalWrite(IN_24, HIGH); }

void stopRobot(){

digitalWrite(IN_11, LOW); digitalWrite(IN_12, LOW);

digitalWrite(IN_13, LOW); digitalWrite(IN_14, LOW);

digitalWrite(IN_21, LOW); digitalWrite(IN_22, LOW);

digitalWrite(IN_23, LOW); digitalWrite(IN_24, LOW); }

void distanceLeft(){ digitalWrite(TRIG, HIGH); delayMicroseconds(10); digitalWrite(TRIG, LOW); timeEcho = pulseIn(ECHO, HIGH); lookLeft = timeEcho/58;

}

void distanceAhead(){ digitalWrite(TRIG, HIGH); delayMicroseconds(10); digitalWrite(TRIG, LOW); timeEcho = pulseIn(ECHO, HIGH); lookAhead = timeEcho/58;

}

void distanceRight(){ digitalWrite(TRIG, HIGH); delayMicroseconds(10); digitalWrite(TRIG, LOW); timeEcho = pulseIn(ECHO, HIGH); lookRight = timeEcho/58; }

void loop(){

distanceAhead();

if(lookAhead<30){ stopRobot(); delay(1000); goBack(); delay(200); stopRobot(); delay(1000);

for(angle=90;angle>=10;angle--){ servo.write(angle); delay(5); }

distanceRight();

delay(100);

for(angle=10;angle<=170;angle++){ servo.write(angle); delay(5); }

distanceLeft();

delay(100);

for(angle=170;angle>=90;angle--){ servo.write(angle); delay(5); }

if(lookLeft < lookRight){ goRight(); delay(250); stopRobot(); }

else{

goLeft();

delay(250);

stopRobot();

}

}

else{ goAhead();

}

}

Step 4: Finish

That it once you upload code you are ready to go connect the robot with battery and play. I hope that you are all like my project

Thank you all!

Share

    Recommendations

    • Woodworking Contest

      Woodworking Contest
    • Microcontroller Contest

      Microcontroller Contest
    • Make it Move Contest

      Make it Move Contest
    user

    We have a be nice policy.
    Please be positive and constructive.

    Tips

    Questions

    7 Comments

    Wow! Nicely done! What programming language did you use for the coding.

    2 replies

    Hi! Thanks.

    Arduino IDE.

    The Arduino IDE supports languages C & C++

    OK, Thanks! Keep up the great work!