Obstacle avoiding Robot

Hi!!
I am new to arduino and so i started to learn by trying out a simple obstacle avoiding Robot which goes straight when the distance between the obstacle and the bot is greater than 10 cm and turns left when the distance of the obstacle is less than 10 cm. The Robot i made works fine when connected to my computer but when i power it with an external power source only one wheel spins and nothing other than that happens. I have used L293D IC and HC-SR04 to make my Robot. I have also attached my code. please help me.

# define trigpin A0
# define echopin A1

void setup() {
  
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(trigpin,OUTPUT);
  pinMode(echopin,INPUT);

}

void loop() 
{
  int distance,duration;
  digitalWrite(trigpin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigpin, LOW);
  duration= pulseIn(echopin, HIGH);
  distance= (duration/2)/ 29.1;
  delay(500);
  while(distance > 10)
  {
    FORWARD();
    digitalWrite(trigpin, HIGH);
    delayMicroseconds(1000);
    digitalWrite(trigpin, LOW);
    duration= pulseIn(echopin, HIGH);
    distance= (duration/2)/ 29.1;
    delay(500);
  }
  LEFT();
}

void FORWARD()
{
  digitalWrite(7, HIGH);
  digitalWrite(6, LOW);
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
};
void LEFT()
{
  digitalWrite(7, HIGH);
  digitalWrite(6, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
};

RoboCar-US.ino (1.02 KB)

Hi,

Can you please post a copy of your sketch, using code tags?
Please use code tags.. See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf?

Also what are you using to supply the arduino and the motors?

Tom.... :slight_smile:

/1000 microsecond is a second

Nope.

I am really sorry about the 1000 microsecond part. I didn't notice.

I am using a 9v battery to power my Arduino Mega 2560 and a 12v battery to power the motor.

# define trigpin A0
# define echopin A1

void setup() {
  
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(trigpin,OUTPUT);
  pinMode(echopin,INPUT);

}

void loop() 
{
  int distance,duration;
  digitalWrite(trigpin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigpin, LOW);
  duration= pulseIn(echopin, HIGH);
  distance= (duration/2)/ 29.1;
  delay(500);
  while(distance > 10)
  {
    FORWARD();
    digitalWrite(trigpin, HIGH);
    delayMicroseconds(1000);
    digitalWrite(trigpin, LOW);
    duration= pulseIn(echopin, HIGH);
    distance= (duration/2)/ 29.1;
    delay(500);
  }
  LEFT();
}

void FORWARD()
{
  digitalWrite(7, HIGH);
  digitalWrite(6, LOW);
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
};
void LEFT()
{
  digitalWrite(7, HIGH);
  digitalWrite(6, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
};

I have also attached the circuit. sorry, if the circuit looks a little amateur.

Hi,
Thanks for the schematic, good.
The 9V battery, is it one of those small batteries you use in a smoke detector?
If so then it will not be able to power your mega and SR-04 for long, you will need a bigger capacitiy.

As you say it works OK on the USB supply, the 9V battery is the weak link.

Tom..... :slight_smile:
PS A drawing hint. Because its a pain to draw little skips over non connecting wires, if you draw a dot where crossing wires connect, then you can just draw crossing wires without dots to show that they do not connect.

But i have already tried connecting the Arduino with a 12V battery and that too did not work.

Hi,

can you put on your circuit how you connected the 12V battery to the arduino and the driver?

Tom..... :slight_smile:

I have attached the circuit. The thing is i had only one 12v battery so first i tried using the same battery to power up both my Arduino and motor but that didnt work then i tried using a 12 v battery for my arduino and a 9v battery for the motor and even that didnt work. Though my motor is a 12v motor there should atleast be some spinning but as usual only 1 wheel spinned in a fast phase.