#define TRIG_PIN 3
#define ECHO_PIN 4
void setup() {
Serial.begin(115200); // Increased baud rate for faster serial communication
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
void loop() {
unsigned long startTime = micros();
unsigned long currentTime;
for (int i = 0; i < 2500; ++i) { // Perform 2500 measurements within 5 seconds for an approximate 500 Hz sampling rate
currentTime = micros() - startTime;
if (currentTime >= 5000000) // Break if 5 seconds have elapsed
break;
float voltage = measureVoltage();
Serial.print("Time: ");
Serial.print((float)currentTime / 1000000.0, 3); // Convert microseconds to seconds
Serial.print("s, Voltage: ");
Serial.print(voltage);
Serial.println("V");
while ((micros() - startTime) < (i + 1) * 2000) {} // Wait until next sampling instant
}
delay(1000); // Delay for 1 second before restarting
}
float measureVoltage() {
// Send a short pulse to trigger the sensor
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Measure the duration of the echo pulse
unsigned long pulseDuration = pulseIn(ECHO_PIN, HIGH);
// Calculate voltage (replace with your scaling factor)
float voltage = pulseDuration * 0.000343; // Speed of sound in air (m/s)
return voltage;
}
Explain how this is to measure an external frequency. It appears you are using an ultrasonic distance sensor.
Yes im using HCSR-04 this setup is to test capabilities of the sensor to detect changes in flow.So the data must have high sampling frequency to record the changes.
Is this ChatGPT generated code, by any chance?
Hi, @daniel_mecha
Welcome to the forum.
Thanks for using code tags. ![]()
Are you trying to make an ultrasonic flow meter?
Tom...
![]()
I do the basic coding myself and when trying to put 500 Hz ifrequency it shows error.so i ask chatgpt to show me the solution
yes
Not possible with the HC-SR04. It has an on board microprocessor that cannot be modified. All you can do is replace that MCU with another, programmed to do what you want.
The circuit of the HC-SR04 board has been reverse engineered, and you can read about it here:
HC-SR04 | David Pilling
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.