Hello everyone,
I am trying to measure the distance between two ultrasonic sensors by following this project: Measure Distance.
I did everything as in the tutorial, however it is not working. The displayed distance is always 0.
I checked whether the sensors work by using a different script that only uses one sensor to measure distance of the surrounding objects. Using that script, the sensors work.
I'm trying to troubleshoot but I've run out of ideas.
I know that it is difficult to troubleshoot this but:
does anyone know what could be wrong? Any ideas of what to check?
does it look like there's any error in the posted project?
My end goal is to calculate the distance between two points. Is there a better way of doing it?
Post YOUR code, using code tags, and a hand drawn wiring diagram of YOUR circuit and a photo of how you wired it, as described in "How to use this forum".
It would silly to assume that you did everything correctly.
For the transmitter (the one with both sender and receiver uncovered) I used this:
// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
// defines variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
delay(2);
}
For the receiver, the one with the SENDER COVERED and RECEIVER UNCOVERED I used this
const int trigPin = 9;
const int echoPin = 10;
// defines variables
long duration;
int distance, Pdistance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
Pdistance=distance;
Calc();
distance= duration*0.034;
if (Pdistance==distance || Pdistance==distance+1 || Pdistance==distance-1 )
{
Serial.print("Measured Distance: ");
Serial.println(distance/2);
}
//Serial.print("Distance: ");
//Serial.println(distance/2);
delay(500);
}
void Calc()
{
duration=0;
Trigger_US();
while (digitalRead(echoPin)==HIGH);
delay(2);
Trigger_US();
duration = pulseIn(echoPin, HIGH);
}
void Trigger_US()
{
// Fake trigger the US sensor
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
}
I will send pictures and schematics as soon as possible.
That is the code I used. I copied it from that page and pasted in the Arduino IDE, then sent it to the
respective Arduinos (to be more clear: The code I pasted here is directly copied from the Arduino IDE).
Perhaps I don't understand the receiver synchronization scheme linked from the original post, but I don't think it works.
Anyway, here's a link to a thread using a cheap radio module to synchronize the HC-SR04 receiver node with the transmitter node to do ultrasonic one way ranging: Communication Between 2 Ultrasonic Sensors
Transmitter: tries to trigger the sensor as fast as possible - it should come to some 50,000 times a second 12 us worth of delayMicroseconds() time plus overhead for a total of roughly 20 us per run of loop()).
Receiver: that code is just plain nonsensical, with the best chance of that if statement being true is if distance == 0 thus only ever printing zeros, no matter whether it actually measured something.
There is a use and way to use one ultrasonic module to transmit and a second module to detect.
You wire the reception sensor to the transmission module.
What you are doing is physically moving the receiver away from the transmitter.
The module has a separate microcontroller. A PIC
That pulses the transmitter the starts timing while lilistening for the receiver to signal that the pulse was received