Using AdvancedAnalog with ADC

The documentation for AdvancedAnalog is very vague.

When you call the begin command, is it continuously reading the ADC into the buffer?

What is n_samples?
Is it how many samples to average per reading?

What is n_buffers?
Is it how many readings to return? If so how do you access each reading in the buffer?

Thank you,

adc0.begin(resolution, sample_rate, n_samples, n_buffers)
  • enum - resolution (choose from 8, 10, 12, 14, 16 bit)
    • AN_RESOLUTION_8
    • AN_RESOLUTION_10
    • AN_RESOLUTION_12
    • AN_RESOLUTION_14
    • AN_RESOLUTION_16
  • int - frequency
  • int - n_samples
  • int - n_buffers

Bascially yes. It uses the DMA to countinuously read n_samples into the buffer and it also uses the STM32 ability to manage double buffer mode. This means that the data gets written into one buffer, once this buffer is full, the next batch goes into the second buffer. This enables you to process the data from the first buffer while the second buffer ist filled. Make sure that the second buffer is large enough to hold two sample sizes.

If you are familiar with the STM32 HAl library, you can check the deatails at https://github.com/arduino-libraries/Arduino_AdvancedAnalog/blob/main/src/AdvancedADC.cpp.

regards
Herbschi

Are you sure?
Double buffer mode usually uses two buffers of the same size.

That is what I meant actually. Sorry for being vague on this. n_buffers must be twice the size of n_samples.

Herbert

1 Like

I'm beginning to understand...

So the sample code here:
https://docs.arduino.cc/tutorials/giga-r1-wifi/giga-audio

adc1.begin(AN_RESOLUTION_16, 16000, 32, 64)

Is creating 2 buffers with 32 samples each.

SampleBuffer buf = adc.read();

Is reading the buffer that's not in use to buf? Or is the last 32 readings that the adc has taken?

You access the contents of buf like an array,

buf[0], buf[1], buf[2], etc....

Does reading them lock those 32 buffers from being used until this command is called?

buf.release();

Thank you,

Hi @pebbert999 and everyone else! I just made an amend to the current documentation regarding adc.begin() and what the parameters actually mean (see PR).

n_samples is the number of samples we want to acquire from the ADC, stored in a buffer (SampleBuffer). If we set this to e.g. 32, we have 32 samples in the buffer, which can be accessed via buffer[number]. This number is not limited to 32 by the way, I have tested with a 1000 samples in a single buffer.

Here's an example:

......
adc1.begin(AN_RESOLUTION_16, 16000, 32, 64); //initialize the ADC
......

SampleBuffer buf = adc.read(); //create buffer and store ADC values

Serial.println(buf[0]); //prints out first value from the buffer
Serial.println(buf[1]); //prints out second value from the buffer

n_buffer is the amount of free buffers available in a "buffer pool". When we read data, we take the available buffer, read it, and then place it back in the buffer pool. So it does not need to be twice the size of the samples.

Hope this explanation helps! :slight_smile:

2 Likes

Thank you for the clarification. So this is a little bit different from the original double buffering as described by ST (from where I had drawn my assumptions).

Actually I like your method better. :grin:

Herbert

Hi @ksoderby, thanks for the explanation. What happens if you have multiple inputs/channels mapped to the same ADC. ADC Dual Mode seems to suggest that buf1[0] is the value of the first channel while buf1[1] is the value of the second channel. What about buf1.timestamp() in this case is this the timestamp of the start of the acquisition and what would the delay between the different channels be?
Thanks in advance
Best
J