Spark Logic

$10 O-Scope revisited

ahull

Over the last few days I’ve been playing with Ray’s STM32 Oscilloscope idea.

Image

Some preliminary results are available here..

https://github.com/pingumacpenguin/STM3 … ontruction

My take on the Scope Sketch is here..

https://github.com/pingumacpenguin/STM32-O-Scope

Its a fun little project and well worth building just for the entertainment value. Currently bandwidth is a little limited, but with a little extra effort it would be possible to make a fully functional 2MHz Scope for next to nothing. Ideal for probing i2c, audio and other low speed signals.

EDIT: We managed to squeeze the maximum speed out of the ADC, read further through the thread for details. The github code includes all of the enhancements made of the course of this thread.

Replies20

ahull Mon Oct 19, 2015 7:55 pm

bianchifan wrote:Nice little project, unfortunately it still fails compiling.
After cleaning up all faults mentioned above many errors on Time1.h are remaining:
error: ‘dt_MAX_STRING_LEN’ was not declared in this scope
error: ‘uint8_t’ was not declared in this scope
error: expected ‘,’ or ‘;’ before ‘{‘ token
…
and so on..

Win7/64, Arduino IDE 1.6.5

bianchifan Tue Oct 20, 2015 8:19 am

I used PaulStoffregen’s lib and renamed Time.h to Time1.h analog I read some posts above.
Without renaming I get the well known faults in RTClock:
wether ‘mktime’ nor ‘gmtime’ were “declared in this scope”.

Just even I tried your attached lib..same procedure…

There are 3 more different time.h in …\tools\arm-none-eabi-gcc\…

mrburnette Tue Oct 20, 2015 12:57 pm

bianchifan wrote:I used PaulStoffregen’s lib and renamed Time.h to Time1.h analog I read some posts above.
Without renaming I get the well known faults in RTClock:
wether ‘mktime’ nor ‘gmtime’ were “declared in this scope”.

Just even I tried your attached lib..same procedure…

There are 3 more different time.h in …\tools\arm-none-eabi-gcc\…

bianchifan Wed Oct 21, 2015 9:02 am

I found my stupid fault, I forgot renaming in DateStrings.cpp :(

Nevertheless I gave this a try
mrburnette wrote:
#include “./library.h”

robca Sun Nov 22, 2015 12:09 am

I know it’s the wrong thread for my question, but the most knowledge of fast ADC is here, so I thought I’d try here before creating a new thread.

I’m trying to build a sound locator using 4 microphones. The idea is to monitor 4 microphones for a trigger, once a trigger (any microphone) is above the threshold, the system will sample 4 microphones (ideally at high sample rates, above 100kHz per channel). Then the system will cross correlate the 4 signals, calculate the different times the sound has reached the microphones, and use the 3 TDOA values (the first microphone to receive a signal is set as 0 reference time) to calculate the sound location using multilateration. I think I have the code for the cross correlation and multilateration, now I need to get good samples

I see that the $10 scope uses 2 channels interleaved sampling directly to memory using DMA.

Do you have any suggestion on how to sample 4 channels (ideally with DMA transfer), ideally ensuring that all 4 channels are in sync (or worst case ch1 and ch2 in sync, ch3 and ch4 in sync, with a known delay)? Sound location is based on TDOA (time difference of arrival of the sound), so keeping the samples in sync is pretty critical

8bit sampling, btw, is all I need, no need to go all the way to 12

Any suggestion (including C code for similar boards) is welcome. I quickly scanned the STM app note for ADC, and I quickly got lost in all the various modes supported… I hope you could give me a starting point (otherwise I’l dig into the app note)

Vassilis Sun Nov 22, 2015 6:43 am

If I understood correct, you have for example a big square pad with 1 microphone on each corner (4 total) and you want to find the position of an object that flights over that square pad and produces a specific sound (a drone for example).

Is that right?

RogerClark Sun Nov 22, 2015 7:03 am

I have a feeling that unless the sound that the OP wants to detect is something like a click, or something else very short duration, that the most difficult problem may be the signal processing.

If they are listening for the sound of a drone, its a very complicated sound with the 4 propellers all going at different speeds all the time, and I’m not sure how you could even use a digital signal processor to determine the position, as the patter is repetitive.
So even if you did some slide comparison of some circular buffers to produce a digital equivalent of a difference pattern, I have a feeling that its going to be hard to compute, and I’m not sure that a 72Mhz processor will have enough speed to do it

However It would depend on what sample rate was used and how often the buffers needed to be compared.

Also ram would be a big problem, as you could only allocate a few k per channel unless you switch to using a larger processor like a F103V or F103Z which have more ram (or perhaps use an extenal SPI ram device – which would be slower to access)

However, perhaps things are simpler than I imagine.

robca Sun Nov 22, 2015 10:07 am

Thanks for the comments.

Vassilis: yes, the mics are placed in a square or rectangle (since that makes the math easier, multilateration can be done with any arbitrary location, as demonstrated daily by GPS, which works on the same principle), but I’m detecting a knock. It’s relatively easy to detect impulsive sounds and cross correlate their times of arrival, very hard to detect more complex noises and determine their correlation. Roger is absolutely right. If you wanted to detect a drone, the way to do it would be to have an ultrasonic emitter on the drone, ultrasonic receivers on the ground, and have the drone emit periodic ultrasonic impulses.

The interesting thing is that the math works even when the sound is outside of the “microphone rectangle”. Realistically, it’s limited only by the microphone sensitivity. With sensitive microphones and an impulsive sound well above the noise floor, you could detect things at any distance. Speed of sound changes with humidity and temperature, so there’s also a thermohygrometer to help correct for that

Roger: yes, memory is indeed a concern, that’s why I will need to balance the amount of available memory (good thing STM32F103VET6 boards are only ~$10 and have 64kb, in case I need more), sampling rate and microphone array size. The interesting thing about the math is that it only depends on the distance between microphones, not the distance of the sound (since I trigger conversion only when the mic closest to the sound receives it, and since sound travels at around ~343 m/sec, there’s only so many samples needed to detect sound as it travels from the triggered microphone to the farthest). The formula (if I have it right) is that the minimum number of samples needed is the maximum distance between microphones in meters (the diagonal of the rectangle) divided by speed of sound, times the sampling rate. Even for a microphone array as big as 6 by 4 meters, the maximum sound delay between microphones is less than a 1/40 sec, allowing me to sample 4 microphones at a theoretical maximum of ~500-550kHz (with 64kb RAM, leaving enough for the system and other tasks). For 100Khz and a 4x6m array, a Maple Mini with 20kb RAM should be enough

The more I can increase the sampling rate, the more I can decrease the microphone array size. The way the math works, you can detect location with a smaller array if you can sample faster (i.e. if you can reliably detect smaller and smaller TDOA). The more precisely you can measure TDOA, the more precisely you can locate a sound. You can either increase the size of the microphone array (to separate the sound arrival more), or sample faster (to get better resolution and ability to cross correlate signals). If I could manage 100-200 kHz per channel (with good timing precision) at 8 bits, I could make precise measurements with a smaller array

The sounds I want to detect are far enough from each other that any STM32 is fast enough (an AVR Arduino would be, if I could sample fast enough) can sample, cross correlate, calculate location and send the results to either an SD card or via Bluetooth/wifi, and have still plenty of time listening for the next sound. With fast ADC and a 72MHz processor, you could locate a sound multiple times a second, I think. Not the sound of a drone, granted, but any impulsive sound.

So, we are left with the problem that started the original question ;): any idea on how I could sample 4 channels (8 bit) as fast as possible, and as consistently as possible, ideally using DMA?

RogerClark Sun Nov 22, 2015 10:19 am

@robca

I think you’ll need to wait for Andy who wrote the scope or possibly @victorpv (who’s done a lot of DMA stuff) to reply to the DMA question

I think limitation may be the number of DMA channels and I don’t know whether there are enough free channels to handle 4 ADC inputs.
I think the larger chips e.g the VET may have more DMA channels, as they are quite different MCU’s to the F103C series.

BTW. I presume as you are using 4 mics that you are doing 3D position location ?

robca Sun Nov 22, 2015 12:05 pm

After reading a few app notes and samples, and considering I want to use a cheap board (i.e. limited to 2 ADCs), I’m pretty sure that I will have to use Dual Simultaneous Mode, taking two reads (one per ADC… something like mic1 and mic3 on ADC1, mic2 and mic4 on ADC2). That wold mean that mic3 and mic4 will have a small “shift” in time compared to mic1 and mic2 (one conversion cycle), but that would be easy to correct in the cross correlation portion. As long as each pair of mics is in sync, and the time shift is known, it’s easy to add the shift in the TDOA calculation. Having 4 ADCs would clearly simplify things, but with high enough rates it won’t matter

An alternative approach (but I will only do as a second version or if the first one doesn’t work), would be to have an external trigger (i.e. use an op-amp comparator on the microphones) and two F103 boards. The external trigger would start conversion at the same time on the two F103s, each sampling only 2 channels. Then one of the F103 would send the timing to the other, and the “master” F103 would calculate the position… but I hope I don’t have to go there.

I’m actually using 4 mics on a plane, and only get a 2D location. It’s true that with 4 mics (in the proper configuration) I could detect 3D positions, but the math gets horribly complex very fast and I got lost… For a 2D fix you only need 3 microphones, but the math for a 2D fix with 4 microphones is much simpler. Given that converting 3 or 4 analog signals takes exactly the same time, I decided to start with a 2D fix and see what happens, then “graduate” to 3D fixes with the same 4 mics (just in a different layout)

What I’m struggling to understand from the sample code I found and the O-Scope code, is how the DMA transfer moves 8 bits samples, given that the ADC is 12 bits and DMA usually is for 16 bits… Part of my problem is that I’m completely new to the STM32 (and I haven’t read all the docs yet :oops:), part that I’m not familiar with the STM32duino project, so finding things like “where is ADC1 defined so that I can understand ADC1->regs->CR1 |= ADC_CR1_FASTINT” is still slow going (and, yeah, that part I found :))

So any clarification on how DMA might work with Dual Simultaneous Mode would be a great help (ironically, I can find samples to use DMA with just about any ADC mode, save the one I need: Dual Simultaneous Mode)

Once I get over this hump, I hope to have some sample code to share (and will start the right thread… until then, this one is where most of the DMA/ADC knowledge seems to be). Hope Andy and the others won’t mind too much the thread hijack

ahull Sun Nov 22, 2015 12:25 pm

@roca, this sounds like a pretty interesting problem. I would suggest it is interesting enough to have a topic in its own right.

If we knew the profile of the sounds we are trying to defect, we could possibly simplify the problem by adding some analog (or digital) filtering to get a more defined waveform. Filtering will probably be essential to avoid false triggering, and help isolate the signal from the noise.

If the sound is sharp, and there is little background noise, it might be worth experimenting with the sort of simple circuit used in clap detectors. Something like this. If the four mic inputs are fed to four identical circuits, then in theory at least, they should all produce a clean signal in sequence depending on the time taken from the source to the mic. The problem then becomes one of measuring the time between all of the inputs changing, and triangulating the source based on those times. Rapid ADC conversion may therefore not even be necessary.

If we do go down the ADC route, then we need to model something similar to the analog circuit in software (i.e. some sort of integrating trigger)

There are several existing examples of more complex systems on the web, for example this patent.

If however you decide to do some signal processing (noise filtering and integration of the digitised waveforms as a bare minimum I would think), then you will need to figure out what component of the incoming sound you are looking for.

You are also limited on the STM32F103 to two simultaneous ADC conversions at any time, so you will need to sample Mic1, Mic2, then Mic3,Mic4 and account for the delay between the two different sample times in software. This should be possible, perhaps by interpolating the mid point between consecutive sample sets of M1,M2 and likewise consecutive sample sets of M3,M4. You will also need to oversample, and probably apply some statistical cleanup to the samples (more than simple averaging perhaps) in order to avoid false triggering and remove noise. Read up on digital and analog oscilloscope triggering to get some ideas.

One of the projects I have never quite managed to get round to with the STM32F103 is an acoustic speed camera. I live next to a busy main road, where the traffic passes the house at a notional 30mph. During the night (when all of the traffic cops are tucked up in bed), I often hear vehicles pass the house at excessive speed.. and it would be interesting to try to determine that speed from the sound of the vehicle alone. I suspect this should be possible, but I have not had any of that very expensive and rare magic missing ingredient (spare time) to pursue the matter. Your post might be just what I need to get me integrating the doppler shift of the local nocturnal speed freaks.

RogerClark Sun Nov 22, 2015 8:22 pm

Andy.

Re:Speed camera

What about performing a FFT on the sound and then using the Doppler principal, monitor rate of change of pitch.

And / or, have 2 mics a few meters apart and compare pitch to determine when the car passes each mic ( but perhaps amplitude comparison is all that is necessary)

robca Tue Nov 24, 2015 5:07 pm

To avoid randomizing this thread more than necessary, I posted my question here http://www.stm32duino.com/viewtopic.php?f=3&t=757

I did a lot more digging, and I’m at a dead end (I don’t think I can do what I wanted to)

stanleyseow Sun Dec 06, 2015 2:26 pm

I hv the “red” Mini STM32 and managed to get the TFT I ILI9341 running without any issues…

When I tried to compile the STM32-O-Scope, I get the errors below :-

Only changes I made was :-
#undef TOUCH_SCREEN_AVAILABLE
and using the Time.h from // Time library – https://github.com/PaulStoffregen/Time

STM32-O-Scope.ino: In function 'void setup()':
STM32-O-Scope.ino:257:12: error: 'setADCs' was not declared in this scope
STM32-O-Scope.ino:262:34: error: 'setCurrentTime' was not declared in this scope
STM32-O-Scope.ino:263:34: error: 'serialCurrentTime' was not declared in this scope
STM32-O-Scope.ino:264:34: error: 'sleepMode' was not declared in this scope
STM32-O-Scope.ino:270:26: error: 'toggleSerial' was not declared in this scope
and many many more "was not declared in this scope" ....

ahull Sun Dec 06, 2015 10:52 pm

Hi stanleyseow

More info on this problem is scattered throughout this rather log thread. I think I touched on it here..

I think the quick fix is to leave #define TOUCH_SCREEN_AVAILABLE and take a minor hit in the space used by the sketch.

stanleyseow Mon Dec 07, 2015 2:17 am

Ok thanks… added the Utouch and it compiles fine…I wonder why ???

zmemw16 Mon Dec 07, 2015 6:46 am

stanleyseow wrote:
#undef TOUCH_SCREEN_AVAILABLE

stanleyseow Tue Dec 08, 2015 2:32 am

Here is mine …
Image2015-12-07 10.18.06 by stanley_seow, on Flickr

Image2015-12-07 10.18.25 by stanley_seow, on Flickr

Why is my time at 1970 ? Unix clock ??

I read thru the Constructions and Constructions2 wiki..

Besides the 1M ohm resistors, do I need the reference source at PB1 ??

I probe it to a Android phone signal generator running a sine wave out to audio out but nothing happens …

My main purpose are just probing for noise level on an ham radio handy that received level audio signal ?

mrburnette Tue Dec 08, 2015 9:07 pm

stanleyseow wrote:Here is mine …

Sign in to reply