Arduino L293D Obstacle Avoiding Robot

14,626

170

26

About: Robots are better than People

Intro: Arduino L293D Obstacle Avoiding Robot

What you need to make it is.

1. Arduino uno.

2. Chassis kit of some sort.

3. L293D Motor driving chip.

4. power supple i used 0.5 mah li-po battery at 7.4v

5. lots of wires

6. ultra sonic distance sensor

7. potentiometer

when you put the code in the robot it will go forward, when the ultra sonic sensor senses something it will reverse and turn left.

Step 1: Wiring

i made this robot with a thin peace of wood 11cm x 11cm

Step 2: Code

int pwm_speed
//trig of ultrasonic sensor
int trig = 12;
//echo of ultrasonic sensor
int echo = 13; 
//int red = 2;
//int blue = 4;
//int green = 7;
int potpin = A0;
void setup() {
//pins for motor controller
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(6, OUTPUT);//pinMode(red,OUTPUT);
//pinMode(blue,OUTPUT);
//pinMode(green,OUTPUT);//set trig as output and echo as input for ultrasonic sensor
pinMode(trig, OUTPUT);
pinMode(echo,INPUT);
}void loop() {
pwm_speed = analogRead(potpin);
pwm_speed = map(pwm_speed, 0, 1028, 0, 225);scan();int duration = pulseIn(echo, HIGH);
int distance = duration / 29 / 2;if(distance > 15){
   forward(100);
}else if(distance < 15){
  backward(700);
  left(500);
}}// function for driving straight
void forward(int delay_time){
  analogWrite(11, pwm_speed);
  analogWrite(10, 0);
  
  analogWrite(9, 0);
  analogWrite(6, pwm_speed);
  delay(delay_time);
}//function for reversing
void backward(int delay_time){
  analogWrite(11, 0);
  analogWrite(10, pwm_speed);
  
  analogWrite(9, pwm_speed);
  analogWrite(6, 0);
  delay(delay_time);
}//function for turning left
void left(int delay_time){
  analogWrite(11, 0);
  analogWrite(10, pwm_speed);
  
  analogWrite(9, 0);
  analogWrite(6, pwm_speed);
  delay(delay_time);
}//function for turning right
void right(int delay_time){
  analogWrite(11, pwm_speed);
  analogWrite(10, 0);
  
  analogWrite(9, pwm_speed);
  analogWrite(6, 0);
  delay(delay_time);
}//function for stopping motors
void motors_stop(int delay_time){
  
  analogWrite(11, 0);
  analogWrite(10, 0);
  
  analogWrite(9, 0);
  analogWrite(6, 0);
  delay(delay_time);
}
void scan(){
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(5);
digitalWrite(trig, LOW);
}</p>

Step 3: Touble Shooting

If the robot goes the wrong way swap the motor wires around.

Share

    Recommendations

    • Furniture Contest 2018

      Furniture Contest 2018
    • Metalworking Contest

      Metalworking Contest
    • Tiny Home Contest

      Tiny Home Contest

    26 Discussions

    0
    None
    pravi1

    1 year ago

    Hey Robert the code that you have given i the instruction have some errors. pls rectify the errors and post it. Thank you.

    Screen Shot 2016-12-28 at 3.15.42 PM.png
    0
    None
    Robert 7320

    2 years ago

    if you use this code it should work but you'll need to change the motor driver wires to pin 6 int 1 pin 9 int 2 pin 10 int 3 pin 11 int 4 int on the motor driver board and you can use a RGB led with this code and the pot for speed is A0 so the same

    int trig = 12;

    int echo = 13;

    int red = 2;

    int blue = 4;

    int green = 7;

    int val=200;

    int potpin = A0;

    void setup() {

    //pins for motor controller

    pinMode(11, OUTPUT);

    pinMode(10, OUTPUT);

    pinMode(9, OUTPUT);

    pinMode(6, OUTPUT);

    pinMode(red,OUTPUT);

    pinMode(blue,OUTPUT);

    pinMode(green,OUTPUT);

    //set trig as output and echo as input for ultrasonic sensor

    pinMode(trig, OUTPUT);

    pinMode(echo,INPUT);

    }

    void loop() {

    val = analogRead(potpin);

    val = map(val, 0, 1028, 0, 225);

    digitalWrite(trig, LOW);

    delayMicroseconds(2);

    digitalWrite(trig, HIGH);

    delayMicroseconds(5);

    digitalWrite(trig, LOW);

    int duration = pulseIn(echo, HIGH);

    int distance = duration / 29 / 2;

    if(distance > 15){

    digitalWrite(blue,0);

    digitalWrite(red,1);

    forward(50);

    }else if(distance < 15){

    digitalWrite(red,0);

    digitalWrite(blue,1);

    backward(600);

    digitalWrite(red,1);

    digitalWrite(green,0);

    left(700);

    digitalWrite(green,1);

    }

    }

    // function for driving straight

    void forward(int delay_time){

    analogWrite(11, val);

    digitalWrite(10, LOW);

    analogWrite(9, val);

    digitalWrite(6, LOW);

    delay(delay_time);

    }

    //function for reversing

    void backward(int delay_time){

    digitalWrite(11, LOW);

    analogWrite(10, val);

    digitalWrite(9, LOW);

    analogWrite(6, val);

    delay(delay_time);

    }

    //function for turning left

    void left(int delay_time){

    analogWrite(11, val);

    digitalWrite(10, LOW);

    digitalWrite(9, LOW);

    analogWrite(6, val);

    delay(delay_time);

    }

    //function for turning right

    void right(int delay_time){

    digitalWrite(11, LOW);

    analogWrite(10, val);

    analogWrite(9, val);

    digitalWrite(6, LOW);

    delay(delay_time);

    }

    //function for stopping motors

    void motors_stop(int delay_time){

    digitalWrite(11, LOW);

    digitalWrite(10, LOW);

    digitalWrite(9,LOW);

    digitalWrite(6, LOW);

    delay(delay_time);

    }

    0
    None
    jimilnshah

    2 years ago

    this codes are having error can u have another codes

    0
    None
    maria_AI

    2 years ago

    problem with the code :(

    sketch_may08a.ino:5:16: error: expected unqualified-id before '<' token

    sketch_may08a.ino:10:8: error: expected constructor, destructor, or type conversion before '(' token

    sketch_may08a.ino:11:8: error: expected constructor, destructor, or type conversion before '(' token

    sketch_may08a.ino:12:8: error: expected constructor, destructor, or type conversion before '(' token

    0
    None
    hiponiaE

    2 years ago

    Hello about the L293D motor IC what are the other replacement for it.? I mean, there is no available stock of the said IC and im going to use it im project arduino and bluetooth car. So, is there other value of IC as replacement for it.?
    Any help would be greatly appreciated, thank you.

    1 reply
    0
    None
    keithfiorehiponiaE

    Reply 2 years ago

    Try this from Sparkfun, the SN754410 Quad Half H-Bridge. https://www.sparkfun.com/products/315

    0
    None
    Alexkouvel

    2 years ago

    We have a problem. We can't find the name of the supersonic sensor . Can you give us name or can we use another one?

    1 reply
    0
    None
    keithfioreAlexkouvel

    Reply 2 years ago

    If you Google HC-SR04, you will find an ample list of retailers/supply - especially eBay

    0
    None
    Exop

    2 years ago

    Great project! I have to ask if it is possible to use these sensors underwater? If you waterproof them of course.

    0
    None
    alexkat

    2 years ago

    we can use another type of distance sensor?or the same ...for example sharp distance sensor?/

    0
    None
    AbD3Robert 7320

    Reply 2 years ago

    Please tell me about the code change if I want to stop it, not to divert its path

    0
    None
    AbD3

    2 years ago

    i don't wanna divert the path.. I just want to stop it when any obstacles detect.. So what should be changed in the code.. and

    what is the unit of distance in : }else if(distance < 15){

    0
    None
    Robert 7320

    2 years ago

    If you want a cheap chassis kit go on ebay and seach arduino smart car kit or you can make your own with cardboard ,wood or plastis.