I have been trying to write a sketch using an HC-SR04 with an Arduino Uno. My objective is to use the HC-SR04 with a buzzer.
I want the audible sounds to increase with speed the closer I get to an object and decrease the further away from the object; to cover the entire range of the sensor in inches.
I have attached a sketch that I have been working with. It does verify but does not give the result as above. The buzzer sounds continuously as soon as I apply power to the board. I have tried making changes to the sketch but to no avail.
Could someone look the sketch over and see if there are problems with it,……..please.
I have found conflicting information concerning libraries for the HC-SR04. When is a library needed, and when is it not needed. I would appreciate any help. I’m still trying to learn to code but realize that it is an ongoing process.
Much thanks,
Here is the sketch:
/* HC-SR04 Sonar Sketch */ #include<NewPing.h> int trigPin = 8; int echoPin = 9; int buzzerPin = 10; int scalingfactor = 10; int inches = 0; unsigned long maxtime = 1000; unsigned long i = 1; unsigned long time2 = 0; unsigned long time1 = 0; void setup() { pinMode (trigPin, OUTPUT); pinMode (echoPin, INPUT); pinMode (buzzerPin, OUTPUT); } v void logdistance() { time1 = (millis); digitalWrite (trigPin, HIGH); delayMicroseconds (10); digitalWrite (trigPin, LOW); duration = pulseIn(echoPin, HIGH); inches = duration / 148; time2 = millis(); maxtime = scalingfactor * inches; } void delayandcheckdistance(); { i = 1 ; while (1) { if ( i = % 60 == 0) { logdistance (); } delay (1); ++1 ; if ( i >= maxtime) { break ; } } }
Hi.
There are two different ways to get the distance from the ultrasonic sensor:
- using the NewPing library, or
- without using the library and do the calculations ourselves (that’s what you’re doing in your code).
This tutorial explains the different ways to get the distance: https://randomnerdtutorials.com/complete-guide-for-ultrasonic-sensor-hc-sr04/
In your sketch, although you’re including the NewPing library, you’re not using it. So, you can remove the line that includes the library.
There are also a few other problems with your sketch:
- There are several variables that you’re declaring, but they are not used anywhere in the code. For example: time1 and time2. Additionally, you are defining the buzzerPin, but you’re not controlling that pin anywhere in the code. That means, that once power is applied to the buzzer it will sound continuously.
- You create two functions in your code: logdistance() and delayandcheckdistance(), however these functions are not called anywhere in the code. That means that even though they are defined, they will not run.
- Your sketch doesn’t include a void loop() function. Is something missing in the code you’ve pasted?
To make the created functions run, in the void loop(), you just need to write their name to call them, for example
void loop(){ logdistance(); delayandcheckdistance(); delay(10); }
Did you paste the complete code?
One of the easiest ways to control a buzzer is using the tone library: https://www.arduino.cc/reference/en/language/functions/advanced-io/tone/
Have you experimented the code I’ve sent you before? https://gist.github.com/RuiSantosdotme/9ad7a2cf1fe4d6c24c7892880fa3def6
Let me know if this helps, and how you’re doing.
Regards,
Sara