Arduino L293D Obstacle Avoiding Robot

by Robert 7320 in Circuits > Arduino

17073 Views, 170 Favorites, 0 Comments

Arduino L293D Obstacle Avoiding Robot

20160122_091229[1].jpg

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.

Wiring

l293d.jpg
20160122_091300[1].jpg

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

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>

Touble Shooting

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