HC-SR04 to sense water flow

i have mounted this module on a 1.5 inch dia pvc pipe but the readings come out to 0.4 inches distance with the code below.
I am expecting the sound waves to bounce off the lower wall of the pipe giving me 1.5 inches .
Why am I getting 0.4 inch distance ?

/*********
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp32-hc-sr04-ultrasonic-arduino/
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.
  
  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*********/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_ADDR   0x3C
Adafruit_SSD1306 display(128, 64, &Wire, -1); 
const int trigPin = 5;
const int echoPin = 18;

//define sound speed in cm/uS
#define SOUND_SPEED 0.034
#define CM_TO_INCH 0.393701

long duration;
float distanceCm;
float distanceInch;

void setup() {
  Serial.begin(115200); // Starts the serial communication
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(2);
  display.setCursor(0,0);
  display.display();
}

void loop() {
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH)/1000;
  
  // Calculate the distance
  distanceCm = duration * SOUND_SPEED/2;
  
  // Convert to inches
  distanceInch = distanceCm * CM_TO_INCH;
  
  // Prints the distance in the Serial Monitor
  //Serial.print("Distance (cm): ");
  //Serial.println(distanceCm);
  Serial.print("Distance (inch): ");
  Serial.println(distanceInch);
  //Serial.print("Duration (ms): ");
  Serial.println(duration);
  display.clearDisplay();
  display.setCursor(0,0);
  display.print(duration);
  display.display();
  delay(1000);
}

Have you looked at the minimum sensing distance in the data sheet? IIRC it's 4cm in air. At such a close range it can't measure linearly anyway because the transmitter and receiver are several cm apart.

1 Like

Not so. This line returns the travel time in milliseconds because you divided it by 1000. Why do you do it? This sacrifices resolution.

I am using the following code to sense distance but when I add display output to it the results not only change in values but also become more spread .
how is the sending the output to display is effecting the sensor ?

/*********
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp32-hc-sr04-ultrasonic-arduino/
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.
  
  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*********/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_ADDR   0x3C
Adafruit_SSD1306 display(128, 64, &Wire, -1); 
const int trigPin = 5;
const int echoPin = 18;

//define sound speed in cm/uS
#define SOUND_SPEED 0.034
#define CM_TO_INCH 0.393701

long duration;
float distanceCm;
float distanceInch;

void setup() {
  Serial.begin(115200); // Starts the serial communication
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(2);
  display.setCursor(0,0);
  display.display();
}

void loop() {
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH)/1000;
  
  // Calculate the distance
  distanceCm = duration * SOUND_SPEED/2;
  
  // Convert to inches
  distanceInch = distanceCm * CM_TO_INCH;
  
  // Prints the distance in the Serial Monitor
  //Serial.print("Distance (cm): ");
  //Serial.println(distanceCm);
  Serial.print("Distance (inch): ");
  Serial.println(distanceInch);
  //Serial.print("Duration (ms): ");
  Serial.println(duration);
  display.clearDisplay();
  display.setCursor(0,0);
  display.print(duration);
  display.display();
  delay(1000);
}

duplicate post
https://forum.arduino.cc/t/hc-sr04-to-sense-water-flow/1151037

What Arduino board? That information should be included in the OP as a matter of course. The how to get the most from the forum post.

Pin 18 on an Uno or Nano is the I2C SDA (A4) pin. The display uses SDA. Suggest that you pick a different pin for echo if that is the case.

1 Like

Threads merged.

i havent looked at the data sheet but if i leave the sensors open then it detects the movement from 10 feet away fine.
problem arises when I mount it on the PVC pipe.

I was told to open different post for different topic. and since there were two issues at hand i had opened two topics :

1- sensor showing different values when display is engaged
2- sensor not showing proper distance when mounted on PVC pipe.

are we going to address both the issues in one thread?

This is ESP32 board with Arduino code from Rui Santos.

The sensors work fine IF :
1- the display output is commented out .
2- the sensors are not mounted on the PVC pipe.

Okay, but I thought you were looking at the other end of the distance scale, like a few centimeters. What you are saying has no bearing at all on the minimum distance behaviour. I'm having trouble imagining your thought process in this regard.

The only real significance, is that it shows that the sensor isn't broken.

Rather than say, I haven't looked at the data sheet, why don't you do that!!!!

1 Like

You can't troubleshoot both of these at the same time. Pick one, make it work, and then attack the other one.

1 Like

ok lets focus on the item 2 since I can live without having to display the values on an OLED.

2: The sensor module if mounted on PVC is not giving me 1.5 inch distance but rather 0.4 inches distance.

so first question is : do the sound waves pass the top wall (contact point between sensor and PVC pipe ) and then reflect off the base wall or are they getting bounced off the top wall ?

in either case 0.4 inch reading is wrong.

yes i did read it now and I am well in limits of my application. it can read 4m to 2cm .

https://cdn.sparkfun.com/datasheets/Sensors/Proximity/HCSR04.pdf

Sound waves are reflected from any boundary where there is a change in the speed of sound at the boundary. Air-PVC and PVC-water are two such boundaries.

so first question is : do the sound waves pass the top wall (contact point between sensor and PVC pipe ) and then reflect off the base wall or are they getting bounced off the top wall ?

We can't see any of these walls.

Please post a photo of the sensor installation.

1 Like

Trying to measure flow by measuring water depth in a round pipe is going to be difficult, volume will vary with the sine of depth. Without a photo or drawing it's going to be hard to help.

I'm suspicious of the 2cm minimum distance specification. That is less than the distance between transmitter and receiver transducers. Therefore there must be triangulation on the path between them, and that assumes a perfect target with not a lot of multiple reflections. That is what I think of when a "pipe" is mentioned.

I agree, this can not move towards a solution without detailed photographs and/or detailed (i.e. multi dimensional and made to scale) drawings.

how can I post a picture ? i am not seeing any option for that .

Also i am not trying to measure the flow rate , just want to measure if water is flowing or not.
please see this video about this topic so I know its possible

Drag it into the post editor window.

so I know its possible

The method in the video is almost certainly not possible with the HC-SR04. You need to get the signal through the pipe into the water, but around 99% of the HC-SR04 signal will simply be reflected from the outer surface of the pipe.