See the schematics in the attached file that i made
and use this code
//*
HC-SR04 Ping distance sensor:
VCC to arduino 5v
GND to arduino GND
Echo to Arduino pin 7
Trig to Arduino pin 8
Created by Mudassir Hussain
*/
#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define LEDPin 13 // Onboard LED
int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}
void loop() {
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;
Serial.print("Distance");
Serial.println(distance);
//Delay 50ms before next reading.
delay(50);
}
Connect VCC and GND of both the sensors to same Arduino
Please Add Karma if this helped you.
Merry Christmas
mudassir9999:
Haha, I've a milestone of 100 Karma points
But actually asking for them? Sort of defeats the purpose, IMO.
People will give you Karma points if they feel that you deserve them. You shouldn't have to ask for them.
You don't get any prizes, you know.
No need for apologies. My opinion isn't necessarily the same as others'.
But I do personally think it's just better to leave people to think of it themselves.
We don't want to mimic YouTube etc.
Yes, This is because of Timing.
The sonic waves emitted by the Transmitter are reflected by an object and received back in the Receiver, The system calculate the TIMING of each wave "How much does that wave took to come back to the receiver" and gives you the OUTPUT.But in your case the Sound wave is not reflecting to any object but directly coming into the receiver.
Keep the two modules side by side and see, I'm sure it will give you right values with good precision.
the modules & frequency are Equal and Wiring is correct,
I just looked that attached picture, Can you tell me Where is that Green and Yellow wire are connected to.? In the picture it seems both of them are connected to ECHO pins of those two modules.
mudassir9999:
I just looked that attached picture, Can you tell me Where is that Green and Yellow wire are connected to.? In the picture it seems both of them are connected to ECHO pins of those two modules.
I think you might mean the "trig" pins. On my modules, at least, the "Trig" pin is next to the Vcc pin.
I agree, though, that it looks like the same pin on both modules is connected, rather than the "Trig" pin on one and the "Echo" pin on the other.
And Tom raises a very good point - if they're both pointed in the same direction, what's the benefit of using two modules? I imagined them pointing toward each other, so that one sent a ping and the other recieved it to indicate the distance between them. The way they're currently oriented, one sensor would suffice.