How to make wall/obstacle avoiding robot with arduino uno atmega 328

That's actually a pretty good kit for the price, includes all the "components" you need to build a basic robot. What's missing is some of the "glue" components, namely the wires to connect things together (see below). All the pieces are common, standard items, so you can easily find software and libraries for everything. It's very typical with CN companies that they will sell you things very cheaply, but provide little or no support whatsoever beyond that.

Like Drew said, you do one thing at a time, get it working in isolation from everything else, and then gradually bring the different pieces together. After a couple of iterations on this, you'll have learned a lot.

  1. first, learn how to program the UNO, if you don't already know how.
  2. pick one of the subsystems, and learn to program that.
  3. go on to the next subsystem, etc.

If you do a google search on "arduino" and the part, you'll find links to follow up, eg the library for the HC-SR04,

http://forum.arduino.cc/index.php/topic,37712.0.html

Also, to connect things to the Arduino board is a bigger problem than the software. You can get jumpers like this, with male on one end and female on the other, to stick into the Arduino female headers, but the little round pins are really crappy and marginally reliable,

What I would do instead is insert male headers into the Arduino female headers, and use the following type of female-female jumper. MUCH more reliable, and won't fall out as easily.

Good luck, :-).

I have all the connectors :slight_smile:
I'm check ultrasonic sensor by this code-

and motors with
int ENA=5;//connected to arduino's port 5 (output pwm)
int IN1=2;//connected to arduino's port 2
int IN2=3;//connected to arduino's port 3
int ENB=6;//connected to arduino's port 5 (output pwm)
int IN3=4;//connected to arduino's port 4
int IN4=7;//connected to arduino's port 7
void setup ()
{
pinMode(ENA,OUTPUT);//output
pinMode(ENB,OUTPUT);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
digitalWrite(ENA,LOW);
digitalWrite(ENB,LOW);//stop driving
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);//setting motorA's direction
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);//setting motorB's direction
}
void loop ()
{
analogWrite (ENA,255);//start driving motorA
analogWrite (ENB,255);//start driving motorB
}

both are working well and I'm very happy :smiley:

now I'm looking for how to combine those 2 codes....I think now you can directly help me :slight_smile:

I think now you can directly help me

Code A does something that you like. Code B does something that you like. The combined program, Code C, has some requirements. They are?

If you UNDERSTOOD the code, you'd know that combining them is both easy and hard. Getting all the code into one sketch is easy. Making the parts do something useful is hard.

Knowing what they should do is more than half the battle.

manju, three things:

  1. wrap your code using the "#" icon on the edit screen,
code goes here
  1. go research the concepts of "modular programming", so your program isn't one long jumble of code.

  2. at your point in this project, the best thing you can probably do is go do a search on similar projects, and adapt code from them for your own use. This is how everybody does it at the beginning, :-).

That code should make your robot turn when something gets closer then 10 inches. Try it and see if it works. Also please do what oric_dan said.

  1. wrap your code using the "#" icon on the edit screen,
 int ENA=5;//connected to arduino's port 5 (output pwm)
int IN1=2;//connected to arduino's port 2
int IN2=3;//connected to arduino's port 3
int ENB=6;//connected to arduino's port 5 (output pwm)
int IN3=4;//connected to arduino's port 4
int IN4=7;//connected to arduino's port 7

const int trigPin = 13;
const int echoPin = 12;
 
void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}
 
void loop()
{

 analogWrite (ENA,200);//start driving motorA
  analogWrite (ENB,200);//start driving motorB

  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, inches, cm;
 
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
 
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
 
  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  
if ( inches < 10) {
  
  
  analogWrite (ENA,255);//start driving motorA
  analogWrite (ENB,1);//start driving motorB
  
  delay (500);
  
}



}
 
long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}
 
long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

Yeah, item #4 was supposed to be the following, but I forgot to include it.

  1. "if...else" is the single most important construct for this sort of thing.

dear drew davis, :slight_smile:

as you say I have connect motor control's pins then where's to connect sensor? :frowning:

in your code
int ENA=5;//connected to arduino's port 5 (output pwm)
int IN1=2;//connected to arduino's port 2
int IN2=3;//connected to arduino's port 3
int ENB=6;//connected to arduino's port 5 (output pwm)
int IN3=4;//connected to arduino's port 4
int IN4=7;//connected to arduino's port 7

const int trigPin = 2;
const int echoPin = 4;

as I highlight in red is it ok when I connect ENA & ENB to digital port 5

in sensors connection ( highlight in blue ) where's to connect them motor control board's pins are almost there in 2 and 4 or do I want to connect sensor's trig & echo wires to analog's 2 & 4

please explain me where's to connect wires

I'm add pic how I connected motor control's wires to arduino

the-arduino-uno-atmega328p-pu-2011-version - Copy.jpg

Sorry I did not notice. I put the code together a little to fast. :slight_smile: I will update my post soon.

I'm a little confused. That is how your code was. I just copied it in. Does it work like that...

 int ENA=5;//connected to arduino's port 5 (output pwm)
int IN1=2;//connected to arduino's port 2
int IN2=3;//connected to arduino's port 3
int ENB=6;//connected to arduino's port 5 (output pwm)

Where it says port 5 the 2nd time is just a copy typo, pin 6 is actually being assigned.

Also, you cannot use pins 2,4 for the sonar, as they are already assigned as IN1,IN3. You can use 12,13 as shown earlier.

Also, since you're trying to do both sensors and motors in one initial sketch, put the robot up on a book,etc, so the wheels can spin freely during program development.

thanks on advice :slight_smile: waiting for modified code :slight_smile:

I'm try the code in robot. sensor is working (sending distance to serial monitor) well but motors are on same position without turning :confused:

As I originally suggested, the best thing to do is learn how to operate the motors separately from the sensors, and the sensors separately from the motors, and after you do that, only then combine the two together.

HELP ME PLEASE ! HELP ME PLEASE ! HELP ME PLEASE !

What is the problem now?

sensors are working :expressionless: and send details to serial monitor.but motors are not working I tried it by even removing sensor but it wasn't work :frowning:

but motors are not working

All we know so far is where one end of each of 4 wires go. Where is the rest of the schematic? Where is the code?

You need to do some stuff yourself.

this is the code I used

int ENA=5;//connected to arduino's port 5 (output pwm)
int IN1=2;//connected to arduino's port 2
int IN2=3;//connected to arduino's port 3
int ENB=6;//connected to arduino's port 5 (output pwm)
int IN3=4;//connected to arduino's port 4
int IN4=7;//connected to arduino's port 7

const int trigPin = 13;
const int echoPin = 12;

void setup() {
// initialize serial communication:
Serial.begin(9600);
}

void loop()
{

analogWrite (ENA,200);//start driving motorA
analogWrite (ENB,200);//start driving motorB

// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;

// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);

// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();

if ( inches < 10) {

analogWrite (ENA,255);//start driving motorA
analogWrite (ENB,1);//start driving motorB

delay (500);

}

}

long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}

this is the code I used

Wonderful. So, now what we know is that you STILL can't post code correctly. Still NOTHING about where the other end of the wires go.

All I can say at this point is good luck.

manju123:
he he don't say so =( =( I'm new to arduino and I like to go throug but I don't know how to do it and no time to learn on this project because it want to be finish before end of this month

Yes, we all have this trouble, don't we. No time to learn how to do our own projects, so better to find someone else to do them for us.

You're off to a VERY BAD start here.

You didn't listen to anything I said previously, so I'll just point you back to read those earlier posts, reply #6 and #9. I'm done here, got other things to do, so good luck.