# Sound localization using two mics, Arduino Uno on MATLAB

I have been trying to achieve this for a long time using the ITD method and have gone through various articles on the net.

I want to know if its possible to actually achieve sound localization using an Arduino Uno and two MEMS mics with a high gain (https://www.sparkfun.com/products/9868).

I have been trying this on MATLAB using the a=arduino() command and readVoltage command but the main issue I face is that for a particular sound (clapping for instance) the time difference between the two mics is not always the same which results in different direction predictions.

We managed to increase the low resolution of the Arduino by removing unnecessary operations. Now we are able to get an array of size 750 for 10 secs giving a resolution of 0.01 secs. I am not sure if that is enough.

I basically have 3 arrays, one storing the sampling times, and the other two storing the amplitudes of the two analog inputs at that particular time in the first array.

Now I look for a peak in one and then the corresponding near peak in the second one and then compute the time difference. After this I can compute the angle using the speed of sound and the distance between the mics.

Please let me know how I can make this work or where I am going wrong.

• Calculate the minimum distance given your setup. What do you get? And what parameter determines this and the increment/resolution? Also check your memory requirements. Remember that Uno has a total of 2K SRAM (and that must hold stack, heap, global variables). May 6 '18 at 9:18
• How far does sound travel (at normal room temperature and air pressure) in 0.01 sec and/or the time to perform an analogRead()? Software Engineering is also about "doing the numbers". May 6 '18 at 11:13
• How about the distance between the microphones? It is the delta between the source and the microphones you want to measure to determine the angle? May 6 '18 at 14:28
• Here is some info that might help you with the engineering: ese.wustl.edu/ContentFiles/Research/UndergraduateResearch/… Please remember that the Arduino has a max sample rate of approx. 9 K samples per second when collecting all samples on the Arduino before processing. May 6 '18 at 14:41
• I'm voting to close this question as off-topic because the limitations of an Arduino mean that it is not a useful part of a practical solution to the problem. Further, an MCU board is entirely unnecessary here; instead of using an Arduino, you should just route your two microphones to the PC's stereo microphone inputs, and surely Matlab has some capability or plugin that can capture audio from there. If your PC doesn't have stereo mic inputs, get a suitable solution to that problem like a stereo USB audio ADC (beware the cheap ones are mono, and you can't use two for this). May 6 '18 at 16:40

Let's assume a microphone distance of 0.5m (which may be just small enough for you). Sound travels with about 300m/s in normal conditions. Let's also say, that you want at least a precision in the angle measurement of 15°.

As stated in the project Mikael linked, it is important that the distance to the sound source is big enough (so that the waves can be seen as planar). Then you have the following situation: We are interested in the difference in distance, that the waves have to travel to the two microphones. It can be calculated by `delta s = d * sin(alpha)`. With `15°` we get `delta s = 0.13m`. The time difference is calculated by `delta t = delta s/v`, where `v` is the speed of sound. We get `delta t = 0.43ms` which equates to `2.32kHz` sampling frequency. Since we use two microphones that must be sampled sequentially, the sampling frequency must be doubled. In the end we have a sampling frequency of `4.64kHz`. So your sampling time of `0.01s` wouldn't be enough for that.

The largest time difference in this setup (with the sound source in line with the microphones) would be `1.6ms`, so your sampling time would be too big even for this.

Regarding the transmission with a baudrate of say 115200 baud this shouldn't be a problem.

But the actual speed of your setup highly depends on your code, which you haven't shown. Maybe this forum post can help you going further.

• Yes, but using an Arduino to sample audio on behalf of a PC only introduces extra complexity while drastically reducing the quality and amount of information compared to the internal sound card. May 6 '18 at 16:45
• @chrisl Thanks for the detailed well explained answer. I'll try the procedure in the link. Please tell me if using a better micro controller (like- the NRF52 series sparkfun.com/products/13990) help in getting a better resolution. May 6 '18 at 18:50
• also is there a way to speeden up readVoltage on Matlab somehow to get this working maybe to a less precision of 30-90 degrees and d=1-1.5m. May 6 '18 at 18:52
• I don't know anything about the matlab functions. But Chris is right, that it will be easier to achieve with a direct connection from microphone to PC. May 6 '18 at 20:31

You should use the Arduino interrupt function. This will get it to 4 micros. You cannot use delay on Arduino counting in micros that is needed. You can have ca 10 cm between mics doing as follows:

You may use accelerometer instead of mic. Serial read, don't work, too slow. Use ISR..together with Arduino clock is 16 MHz. Gives a error in +- 10 micros. Given that it's possible to get the angle within 0.5 degrees if using Sine/Cosine. But, the trigg appear at different places on the waves sinus, giving you extra errors at low freq.

Don't give up. This works:

1. Attach interrupt (ISR) on 2 pins. Make all variable used in ISR volatile.
2. Use internal clock with micros() to stop at interrupt, flag a variable true if rising/falling/change.
3. Use the time difference from trigs for calculations.
4. Calculations do in loop, not ISR calculate using radians.
5. Don't try to read or write anything while in ISR or before calculations, that mess up the clock!
6. Use 11500 baudrate.
7. If in loop or ISR = true then disable interrupt by state variable.
• I fixed a bunch of things in this post. Please fix more and avoid texting shorthand. Jan 12 at 22:37