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.
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);
}
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.
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!!!!
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 ?
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 ?
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
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.