Hello! I'm currently working on turning an ultrasonic sensor into an audio sensor by using only its echo output pin. I have made some code that I want to use to see if the echo sensor can receive and measure soundwaves. Basically, I'm making a decibel meter out of it. Thanks for the help!
Here's my code:
// defines arduino pins numbers
const int echoPin = 11;
// defines variables
long output;
void setup()
{
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
output = digitalWrite(echoPin, HIGH);
Serial.print("Output: ");
Serial.print(output);
Serial.println("");
}
When I run this code, it gives me an error message essentially saying that I've returned a value in a void loop, the exact message is
Arduino: 1.8.13 (Windows 10), Board: "Arduino Uno"
C:\Users\macat\Documents\Arduino\attemptultrasonicdecibelreader\attemptultrasonicdecibelreader.ino: In function 'void loop()':
attemptultrasonicdecibelreader:17:38: error: void value not ignored as it ought to be
duration = digitalWrite(echoPin, HIGH);
^
exit status 1
void value not ignored as it ought to be
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
What should I fix in my code? I've used other code where I could read and write in a void loop. What is the issue exactly?
Thanks for helping!
1 Like
I am guessing it is because you haven't defined duration. Could be wrong though.
EDIT: Having another look, you have defined output but used duration.
The digitalWrite() function does not return a value. Perhaps you meant to use digitalRead().
If you are attempting to use the HC-SR04 as a microphone, it won't work. The HC-SR04 has its own microprocessor, which controls the transmit and receive functions in a fixed sequence. The transducers used in the HC-SR04 work well only in the ultrasonic frequency range.
You can buy microphone modules for audio, like this one, which has the amplifier that you will also need, built in.
My friend, buy a microphone sensor.
As @jremington said, HC-SR04 has it's own control circuit that is designed specifically to transmit and receive pulses in specific frequency (40khz iirc).
windoze_killa:
I am guessing it is because you haven't defined duration. Could be wrong though.
EDIT: Having another look, you have defined output but used duration.
Ah, that's because I overlooked that variable, I was changing "duration" to "output" to avoid confusion, but it seems I made it worse.
Sorry about that!
Hello again, guys! I see some replies that note that the sensor itself has a microprocessor, and thus this project is not feasible. I'm sure that is the case, given that all/most of you have more experience with Arduino than I. Does this mean that the sensor itself is too complex to configure using just an Arduino? To expand on what I have done though is that I only wired the ground, power, and echo pins to the Arduino. I know the whole working process of ultrasound, but with the time I spent today, I was unable to find anything online that discussed specifically the firmware or circuitry of the sensor itself. All I know about the one I'm using is that it has an emitter and an echo sensor/receiver if that's what it's called. I honestly would've bought the audio sensor in the first place, but I'm trying to see how far I can push myself working with this, as my uncle discourages buying more parts (as of now).
I only thought it may have been possible since, in order to measure distance, you have to measure atmospheric pressure, soundwave intensity, etc. I seem to have forgotten, however, that the sensor in question measures using a different method, using the time elapsed, along with other variables.
Again thank you all so much for the help, whether it was helping me understand, or saving me a headache. 
Ignoring that it may not be possible for other reasons you have to remember that most ultra sonic sensors are tuned to a particular frequency generally to exclude other frequencies. Most of them operate at about 40KHz which is way above audio so it is very unlikely you will pick up anything.
I would try a microphone as suggested.
PS. If you want to have fun in a parking lot make your self a ........no better not.
jremington:
The digitalWrite() function does not return a value. Perhaps you meant to use digitalRead().
If you are attempting to use the HC-SR04 as a microphone, it won't work. The HC-SR04 has its own microprocessor, which controls the transmit and receive functions in a fixed sequence. The transducers used in the HC-SR04 work well only in the ultrasonic frequency range.
You can buy microphone modules for audio, like this one, which has the amplifier that you will also need, built in.
That's what I thought too. I get the same issue using the digitalRead command. To be a little bit more clear on what exactly I'm trying to achieve, I'm actually attempting to make a decibel meter. I saw that the sensor emits ultrasonic soundwaves to receive when they reflect back. As of now, I'm uncertain whether if the sensor measures the intensity of the soundwaves (which is what I'm looking for). Thank you so much for this reply! It helped me understand things a little bit better.
windoze_killa:
Ignoring that it may not be possible for other reasons you have to remember that most ultra sonic sensors are tuned to a particular frequency generally to exclude other frequencies. Most of them operate at about 40KHz which is way above audio so it is very unlikely you will pick up anything.
I would try a microphone as suggested.
PS. If you want to have fun in a parking lot make your self a ........no better not.
I guess that should be the case. It's funny because I must've forgotten this, despite that this would be absolutely necessary in order for an ultrasonic sensor to work properly, due to so many waves of different frequencies being emitted by other environmental variables. I wonder how it ignores all other frequencies? Noise cancellation? Perhaps a microprocessor controlling all of that chaos? Thank you so much for the info!
The little aluminum cylinders on the HC-SR04 contain piezo elements that act as both microphones and speakers, specifically tuned to 40 kHz. That way there is little to no confusion from audible sounds.
In the HC-SR04 case one is specifically used as the sender, the other as a receiver, but they are actually interchangeable. Some other types of ultrasonic rangers use a single piezo element for both transmit and receive functions.
The HC-SR04 is taken apart and discussed in this video.