3

I am somewhat familiar with Arduino and am trying to use two ultrasonic HC-SR04 modules to measure the direction of water flow inside a pipe. The sensors are kept as in the image below. The direction is determined by emitting a signal from one sensor and receiving it in the other, noting down the time and repeating with the other set(See image below). This is called an ultrasonic time of flight flowmeter.

We then use the obtained values to determine the direction by seeing which one is greater. Further details on how it works can be found here: https://www.youtube.com/watch?v=rqJ9snaqYiY

I used the below code to collect the data to determine direction for this but it doesn't seem to be working, as only the first value is displayed and the second is always 0.

long firstduration = 0;
long secondduration = 0;

void setup() {
  // put your setup code here, to run once:
pinMode(13, OUTPUT);
pinMode(12, INPUT);
pinMode(11, INPUT);
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
digitalWrite(13,LOW);
delayMicroseconds(2);
digitalWrite(13,HIGH);
delayMicroseconds(10);
digitalWrite(13,LOW);
firstduration = pulseIn(12,HIGH);
secondduration = pulseIn(11, HIGH);
Serial.print(firstduration);
Serial.print(", ");
Serial.println(secondduration);
}

Updated code:

long firstduration;
long secondduration;

void setup() {
  // put your setup code here, to run once:
pinMode(10, OUTPUT);
pinMode(13, OUTPUT);
pinMode(12, INPUT);
pinMode(11, INPUT);
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
digitalWrite(13,LOW);
delayMicroseconds(2);
digitalWrite(13,HIGH);
delayMicroseconds(10);
digitalWrite(13,LOW);
firstduration = pulseIn(12,HIGH);

digitalWrite(10,LOW);
delayMicroseconds(2);
digitalWrite(10,HIGH);
delayMicroseconds(10);
digitalWrite(10,LOW);
secondduration = pulseIn(11,HIGH);

Serial.print(firstduration);
Serial.print(", ");
Serial.println(secondduration);
}

What would the issue be?

I am using an Arduino Uno board.

I have ruled out wiring issues...

Thanks!

4
  • 1
    To which sensors trigger is pin 13 connected? The one with the echo pin 12 or 11?
    – chrisl
    Commented Dec 3, 2021 at 16:56
  • pin 13 is used to trigger both sensors at the same time. Commented Dec 4, 2021 at 2:47
  • @chrisl I changed the code like you said in your answer but it didn't work... Commented Dec 4, 2021 at 6:21
  • The updated code has been shown above. Commented Dec 4, 2021 at 6:22

1 Answer 1

3

I don't think this will work with HC-SR04 sensors. They are not just ultrasonic transducers. They incorporate extra electronics for the sole purpose of measuring the time difference between the sended and received signal. They just don't provide the needed control on the ultrasonic transducers. To change their function to suite you you will need to fully understand their circuitry (I surely don't). Or build your own support circuitry. Additionally you need to think about how to transmit the ultrasonic wave into the fluid. It isn't enough to just tag a standard transducer to the pipe. The small transducers on the HC-SR04 sensors are made for transmitting through air, so the actual transducer is hidden behind the grill inside the capsule. I thin you need to at least bring the transducer itswlf directly to the pipe, if not directly to the water. You will need to buy fizting ultrasonic transducers.

Also you have a problem due to the workings of pulseIn().

If you want to read a HIGH pulse it will wait for the pin to go LOW, then to go HIGH again, and then counting the time until it goes LOW again. This means it blocks from the moment it is called until the pulse is finished (or the 1s timeout hits).

I will assume that you trigger the sensor which has its echo pin on pin 12, though a similar problem arises when you trigger the other sensor.

With the HC-SR04 the HIGH pulse will start at a specific time after the trigger pulse. Lets assume the pulse starts at the same time on both sensors (despite the described problems above). Directly after the trigger pulse you call pulseIn() on the sending sensor. It will block until it receives the each. Since this needs about twice as long as the other sensor to receive the signal (signal needs to go to the second sensor and then back to its source) the end of the echo pulse from the receiving sensor will be inside this time, thus putting its echo pin low again. The sending snsor receives its echo, the pulseIn() function ends. Now you call pulseIn() on the receiving sensor. But its pulse already ended, so the function waits and waits for the pulse to start and finally reaches its timeout. In this case it returns zero.

You would need to check the pins in a non-blocking way. Maybe even an Input Capture Interrupt would be fitting (depending on the requirements on the resolution).

3
  • Thanks @chrisl . So should I update the code to check first sensor one and then trigger sensor two and check it? Commented Dec 4, 2021 at 2:49
  • And I also need to change the sensors used so that the transmitter diaphragm touches the pipe itself right? Commented Dec 4, 2021 at 2:50
  • Actually you don't want to trigger both sensors as they are. When they both only receive their respective echo they will have the same value (as in both cases the sound wave needs to go one time with the flow and the second time against it). You would need to split up each sensor so that the receiver of each sensor sits on the other side of the pipe than the sender. The you can do these two measurements (one with thw flow, one against it)
    – chrisl
    Commented Dec 4, 2021 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.