Locating Where a Sound is Coming From

For my masters year, half the marks came from one module, the masters project. Being a team effort, we were in a group of three. Putting our heads together, and taking ideas from lecturers, we made a list of potential projects. We knew for one that I wanted to be making hardware, and the other two wanted to use/learn machine learning and maybe FPGA’s. After much deliberation we decided to make a project that listened for a sound, and using time difference of arrival worked out where the sound came from. This post is mostly about the hardware and circuitry designed for the project.

The final board for our masters project. Contains four amplifier sections for the microphones and a micro controller with USB interface.

With a world with a big focus on safety in public places, we thought it would be a good product for the security industry, potentially with links to smart cities. Imagine a shopping center, somewhere with lots of security already. They tend to have lots of security cameras, alarm systems and a dedicated guard. This isn’t uncommon in big public places/attractions, especially in the UK. Sports stadiums, train stations and museums are always looking for new ways to protect themselves and isolate problems. The example that inspired us was the horrendous shooting in Las Vegas at a concert in October 2017, just as we were picking projects. The main problem was that the security services did not know where the shooter was, meaning it took longer to get to him. If they had a system like we envisaged, the microphones would pick up the sound and triangulate it. The location could then be sent to relevant authorities to use.

The front page of the New York times just days after the Las Vegas shooting

To start with we needed microphones. We didn’t need to reinvent the wheel, and microphones can be easily bought off the shelf. For ease we used standard stage microphones, that had 3-pin XLR outputs. Although we had been warned that they would not work they had a good omnidirectional pattern, and had lots of good documentation. One issue with them is the output is balanced, which means it needs to go through a pre-amp. To get an idea of what a balanced signal is, imagine a ground connection and two signals. The two signals are the same, but one is inverted. This means when it travels down the cable it is much less susceptible to interference. This is part of the reason we liked using stage rated equipment, as sound engineers have already worked out issues with transporting sound signals long distances through noisy situations. We concluded from research that the signals could reach over 100m, which was the number we were aiming for.

One of the pre-amplifier sections used on the board, using four operational amplifiers.

Once the signal got to the box it needed to be converted to a signal that could be read by an ADC. To do this we used an INA217, a pre-amp designed for basically this purpose. An instrument amplifier, it measures the difference between the signals and amplifies them, outputting a voltage with reference to ground. The signal from the microphone is tiny, in the tens of milivolts range, so it needed some dramatic amplification to get it near the 5V ADC. The INA217 did a good job but we put a second stage amplifier to give it the extra push, as very large gains can sometimes be bad for a number of reasons. We used an OP07D but if we were to do it again we would get a rail-to-rail to get better results. This amp had a pot as one of the gain resistors so that we could easily trim the gain depending on test. Finally, the signal at this point sat between -2.5V and +2.5V so we needed to shift it up so it was between 0 and 5V. This was done with a simple shift circuit and an amplifier. We used another OP07D to make buying easier.

Me manufacturing the PCB, at this point I was inspecting to see how well it had been soldered.

From here the signal gets read by the 12 bit ADC in an STM32 microcontroller. It then streams the data via the USB to a PC where MATLAB picks it up. This is where my knowledge is a bit lacking as I did not make it. In essence MATLAB uses a machine learning algorithm that had listened to over 1000 gunshots, screams and explosions. It has categorized them, and used a number of features to notice the difference. Then when playing a new sound of one of these things (not heard by it before) it categorizes it and outputs it to the user. It also used a selection of sounds from the background to know when there is not one of these events happening, else there will false negatives.

One of our set ups to get a useful output and test the amplifiers were working properly.

All in all the project did actually work. It detected gunshots and screams being played into the microphone, and the triangulation algorithm worked, just not in real time. We managed to win the best masters project, mainly because we had good quality hardware, a partially working system and a good business case behind it. There is a lot of scope of where this project could go, and many things that could be improved, but we were happy with how it came out. I may be able to use some of the circuitry on other projects, who knows. If you are interested in more of the project, maybe some more detail about the hardware or manufacture, comment or message on Twitter. Thanks for reading.

A good example of how much difference there is between the microphones when a big sound was made. Minute distances made a big time difference.

Considerations When Making a Current Shunt Sensor

For battery powered projects, current consumption is a really important consideration when designing the circuitry. While designing my final year project I spent a huge amount of time researching how to put together a simple current sensor. Considering most applications for me are DC, fairly low current and low voltage, the most obvious design is to make a current shunt. The basic idea of a current shunt is that you put a very low value resistor between the circuit you want to measure and ground, and measure the voltage across it. When one side is of the shunt resitor is ground it is low side, there are also high side versions but are mlre complex. As the resistor has a small resistance, there will be a low voltage drop (usually mV) across it meaning it shouldn’t affect the load circuitry. The voltage will also be proportional to the current running through it, meaning if you measure it, and do the right maths you can get a consistent and reliable current reading. This post is about how to get that tiny voltage into 1’s and 0’s, while thinking about the considerations that have to be made about the design to make it accurate and reliable in the environments you want.

Final Year Project
My final year project needed current sensors on the motors as well as monitoring the drain on the battery.

The first thing that needs to be decided is the shunt resistor itself. A shunt resistor is basically a low value resistor, with very tight and known tolerance usually with a fairly high power rating. It can be used in AC and DC circuitry, with the concept behind it being that as a current flows  though it, a voltage is induced across it. The voltage can then be measured and using a simple calculation (based on ohms law) converted into a value for current. The value of the resistor depends on what it is measuring and what is measuring it. Start with what is measuring it. If you are like me, it is likely that it will be read by an ADC, probably on a 5V or 3V3 microcontroller. The voltage across the resistor is going to be amplified between 10 and 100 times (we will get to why in a moment) so pick a maximum voltage within that range. I tend to go with 100mV maximum voltage drop, which for a 5V ADC would require an amplification of 50. Then, take the maximum value of current you want to be able to measure. You can then use ohms law to figure the resistance you need. For example if I wanted to measure 1A, the resistor would be 100mV/1A = 100 mohm. Now we know the resistor value, use the power equation to work out the power eating we want. For this example we would need P = I V = 1 x 0.1 = 100mW. This is the minimum power rating you need, I personally would get a 250mW or even a 500mW just to keep the temperature of the circuit down.

The simple equation to work out what sizesunt resistor to use. Credit: Texas Instruments 

Now we have a voltage that will be somewhere between 0 and 100mV with reference to ground. We want this value to be scaled up to 0 to 5V. To do this we are going to use an operational amplifier. There are plenty out there, and most people have their favourites and I’m not here to convince you otherwise. I tend to use an op amp that I am using somewhere else in the circuit to make life easier. There are a few things you do need from an op amp in this circuit though, it needs to be rail to rail, and have a low input offset voltage. Offset voltage in an op amp is the voltage difference on the inputs, and even though they are tiny differences they can have a big effect because we are amplifying small voltages, and any noise or offset will be amplified too. The op amp needs to be in a simple non inverting configuration. The equations needed to design this are in most first year textbooks and there are plenty of calculators online. I have set a gain of 50 in my calculation, which is in the fairly common range. The output of the amplifier can then go straight straight into an ADC to be measured.

The basic layout of a current shunt sensor showing where the shunt resistors and gain resistors go in the circuit. Credit: Texas Instruments 
The first version of my current sense test circuit, using an OP170 made by TI.

Now let’s look at a few places where errors can come into a design like this. There are two types of errors that occur in a circuit like this, gain error and offset error. A gain error is one where the output error gets further away from the ideal output as the current gets higher. An offset error is one that has the same amount of error whatever the input, just like an offset. The only common source of offset error in a circuit like this is from the offset error in the op amp discussed previously, solved with an improved choice of amplifier. The gain errors are usually due to a difference in resistance from the ideal. Many things can cause this, one is the tolerance of the resistor used, we want to use a precision resistor of 1% or less tolerance. Another cause could be temperature changes in the resistor itself, it may be next to a large MOSFET or other hot component, or could have too low of a power rating making it heat up, wither way a change in temperature means change in resistance. Layout can also be an issue, if tracks are too thin or too long they can add extra unwanted resistance.

Great graphs showing the difference between gain an offset error. Credit: Texas Instruments.

If you want to add a bit of fancyness into the project, or really need to measure down to low currents, you need to tackle the zero-current error. The problem is that when using an op amp, even a rail to rail one, it never quite reaches the power rails. Even the best ones can only get within 100mV or so of the power rails, this is known as saturation. Solving this involves moving the power rails slightly so the saturation point is less than ground. If you have a negative voltage rail you can use that but home projects tend to be single supply, so we need another power source. This can be made using a voltage inverter (a type of charge pump). Usually only needing an external capacitor to work, they are cheap and easy to integrate into a project. I used a LTC1983, which creates a negative 5V rail, but there are plenty of others such as the LM7705. Research what fits your circuit and cost point, and just attach the negative output to the negative supply rail of the op amp.

 A great graph showing how the zero current error occurs, and what it would look like if you tested it. Credit: Texas Instruments.

Most issues with error can be fixed during the hardware design phase. You can pick better op amps, such as ones designed to combat offset voltage. Some amplifiers have internal calibration procedures, and some such as chopper stabilizers are specifically designed to correct these problems. You can also use a potentiometer instead of a power resistor, but they are more susceptible to temperature and can be knocked. Another way is to fix issues in software by creating a calibration procedure. Using a calibrated precision current source and a multimeter, measure the reading of the ADC and compare the value to the reading from the instruments. You should get an offset and gain value that can then be used to calibrate the sensor.

A simple set up that I used to calibrate an early sensor, with a big power resistor as the load and a variable power supply to change the current. Marked down to put into calibration.

I would suggest trying out a few of these sensors in future projects, they don’t cost too much, and can be a valuable addition to a design. Especially for power sensitive devices, or smart sensors, this could be a better solution than an off the shelf or breakout board solution. If you want to hear more about my current sensor designs, and how well the testing and calibration went then comment or tweet at me. I already have some documentation that I may release at some point.

Follow @TheIndieG
Tweet to @TheIndieG