Hi genius
I'm going to build (assemble) ultrasonic obstacle avoiding robot and I have Arduino Uno - Atmel Atmega 328-PU board and Arduino Compatible H-Bridge Motor Driver board (photo's attached) and Ultrasonic Module HC-SR04 Arduino. I'm looking for a way to attach cable from motor,ultrasonic module and within uno and motor control board and also suitable program/code for it...
Hi genius
I'm going to build (assemble) ultrasonic obstacle avoiding robot and I have Arduino Uno - Atmel Atmega 328-PU board and Arduino Compatible H-Bridge Motor Driver board (photo's attached) and Ultrasonic Module HC-SR04 Arduino. I'm looking for a way to attach cable from motor,ultrasonic module and within uno and motor control board and also suitable program/code for it...
1st off pictures are of little value. Give us links to data sheet or the products page… I don't plan on hunting them down. You bought the stuff!
No body is going to wright your code for you… you would never learn. However, I will give you some examples.
Analog read example… chances are your sensor is analog… now way to tell with out data sheet.
/*
AnalogReadSerial
Reads an analog input on pin 0, prints the result to the serial monitor.
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
This example code is in the public domain.
*/
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(700); // delay in between reads for stability
}
Thats it for now until you post the data sheets or product page. No point wasting my time giving you code that might/might not apply.
I'm bought bought items separately,not the whole project.so I haven't any data sheet or code that's why I'm trying to find the way to connect components and code
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.
first, learn how to program the UNO, if you don't already know how.
pick one of the subsystems, and learn to program that.
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,
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.
I have all the connectors
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
now I'm looking for how to combine those 2 codes....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.
wrap your code using the "#" icon on the edit screen,
code goes here
go research the concepts of "modular programming", so your program isn't one long jumble of code.
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.
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;
}
as you say I have connect motor control's pins then where's to connect sensor?
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
Sorry I did not notice. I put the code together a little to fast. 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.
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.