I'm currently working on a project involving two HC-SR04 ultrasonic sensors and an Arduino Mega2560 board. My goal is to have one HC-SR04 actively measuring distance while the other one, connected only to the echo pin, picks up the initial sonar pings.
However, I've encountered an issue with the setup where the echo pin of the passive HC-SR04 always shows a reading of 0. I've double-checked my connections and tried troubleshooting the code, but haven't been able to resolve the issue.
Here's a snippet of my code:
const int echoPin = 9;
float duration, distance;
void setup() {
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
// Active sonar is being fed with GND and VCC and has seperate module that calculates the distance
Serial.println(digitalRead(echoPin));
}
Also, keep in mind that the active sonar is fed with GND and VCC separately, and it performs the distance measuring independently. It works fine, which is why it's not mentioned in the code above.
I would greatly appreciate any insights or suggestions on what might be causing this problem and how to fix it. Has anyone else attempted a similar setup with HC-SR04 sensors?
How could the passive unit detect a ping when it hasn't been triggered? I think you need to connect the passive unit's trigger pin to the active unit's trigger pin and either disconnect the passive unit's "speaker" or muffle it down with foam rubber or something similar so it cannot issue an interfering ping itself.
Another potential problem:
Can the Mega listen with pulseIn() on 2 pins at the same time?
You might use a second Arduino to listen for the ping and send to the Mega via interrupt, but how would the Mega determine which echo came from which sensor?
I doubt you can circumvent the normal operation of the microprocessor built in to the HC-SR04.
One option is to remove the TX transducer from the "passive" board, and trigger both boards with a parallel connection. Listen for the return pulse detection only from the "passive" board.
So basically what I'm trying to do is to take 2 separate HC-SR04 modules that are independent of one another - one active and the other passive, while trying to make the passive sonar module able to detect active sonar pings. I thought it would be possible because the sonar ping signatures are the same for both sonars. Therefore, the passive sonar would be able to interpret the active sonar ping and make it think it is its own. This way, I can achieve an effective way of knowning when the passive module is being pinged.
Another potential problem:
Can the Mega listen with pulseIn() on 2 pins at the same time?
You might use a second Arduino to listen for the ping and send to the Mega via interrupt, but how would the Mega determine which echo came from which sensor?
So I came around this problem by using the passive sonar with the original HC-SR04 module (using pulseIn). And the active one, is an independent module with separate Microcontroller that outputs range through analog voltage.
I doubt you can circumvent the normal operation of the microprocessor built in to the HC-SR04.
One option is to remove the TX transducer from the "passive" board, and trigger both boards with a parallel connection. Listen for the return pulse detection only from the "passive" board.
How could the passive unit detect a ping when it hasn't been triggered? I think you need to connect the passive unit's trigger pin to the active unit's trigger pin and either disconnect the passive unit's "speaker" or muffle it down with foam rubber or something similar so it cannot issue an interfering ping itself.
Does anyone know of a method to circumvent the original design of the internal microcontroller within a sonar module? I've attempted to locate datasheets online to gain insight into its internal workings, but I haven't found anything like that. How about disconnecting/muffling the speaker while sending repeatedly the trigger signal through the TRIG pin.
Do you think it'll work?
I am sure you have closely studied the documentation on the HC-SR04, so do you remember that multiple sonic pulses are sent out. Is it 6 or 8? Can't remember. Do you have a plan for the passive unit to do something with all the pulses?
Complete, reverse-engineered analysis of the HC-SR04 board circuitry and functionality is presented on line at HC-SR04 | David Pilling
Alright thank you, that's exactly what I've been looking for.
I am sure you have closely studied the documentation on the HC-SR04, so do you remember that multiple sonic pulses are sent out. Is it 6 or 8? Can't remember. Do you have a plan for the passive unit to do something with all the pulses?
Yes, A burst of 8 x 40 kHz pulses
So, according to the datasheets of the sonar module that @jremington presented, the receiver transducer is connected to a quad op-amp circuit called LM324, labeled as U2 in the module. Pin number 1 in the LM324 IC is the output pin in the comparator amplifier, which yields a binary output whenever it detects its own sound-wave. This occurs after passing through the three amplifiers: U2D, U2C, and U2B, which function as Amplifier, Bandpass filter, and another Amplifier, respectively. Thus, theoretically speaking, it is possible to connect Pin number 1 in the LM324 module to an Arduino and manually recognize the eight burst pulses separated by the
predetermined delay between each peak.
Or alternatively, connecting pin number 10 in the Chinese EM78P153S to the Arduino to pickup the signal
Why is that a problem if I'm parsing the data from the quad op-amp chip?
Correct me if I'm wrong, but for my understanding pin number 1 in the LM324 module would give me the a binary output whenever the receiver transducer is receiving loud enough sound wave in the transmitter frequency (40KHz), I assume it's possible to wire it up to a microcontroller and parse the ping manually. I've also connected an oscilloscope to pin number 1 and saw the following pattern:
Assuming there is a zero delay from the time you send the trigger pulse and when the actual burst is sent, then all you have to do is start a timer when you send the trigger and stop it when you get the signal from the opamp pin 1. You could use interrupts
Assuming there is a zero delay from the time you send the trigger pulse and when the actual burst is sent, then all you have to do is start a timer when you send the trigger and stop it when you get the signal from the opamp pin 1. You could use interrupts
You are correct, although keep in mind that my project requires the 2 sonar module to be independent. The passive sonar never knows when the active one triggers the pulse, so the trigger pin in the passive sonar is not used.
No, the passive sonar is meant to detect pulses from the separate active sonar, so they are connected to 2 different microcontrollers. My vision was building a module that's capable of detecting and warning of incoming sonar pings from the HC-SR04 module.
Quick update, the plan worked as expected, I de-soldered the transmitter transducer from the passive sonar and hooked pin number 1 of the opamp U2 chip to the oscilloscope. Finally, I was able to detect the pings from the active module
I would have thought that you would be better off taking your signal from the collector of the transistor, rather than the output of the op-amp.
The signal there is more likely to be logic level.
The code that you showed in post #1 is no good for doing what you want to do.
It takes just over 3ms to print '0' or '1' plus carriage return and line feed to the serial monitor at a baud rate of 9600.
You are likely to miss the reflection whilst you are doing your printing.