This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Faster analog sample frequency on the Arduino ide

I know this isnt an arduino forum but when i proposed this question on their forum they pointed me to this website as the problem is processor specific. So im hoping someone will still be able to help me. 

I am starting a project that uses the Arduino nano 33 ble sense which contains the nRF52840 microcontroller I have hooked up 3 microphones to 3 of the analog pins (A0, A1, A2) and need to sample analog inputs from them as fast as possible. The best I've been able to do is 20us per microphone or 79us for all 3 mics. my code to do this is

int mic = 0;
int time1 = 0;
int time2 = 0;
void setup() {
  Serial.begin(115200);

  nrf_saadc_acqtime_t acqTime = NRF_SAADC_ACQTIME_3US;
  adcCurrentConfig.acq_time = acqTime;
  analogUpdate();
}

void loop() {
  time1 = micros();
  for (int i = 0; i < 100000; i++)
  {
    mic = analogRead(A0);
  }
  time2 = micros();
  Serial.println((time2 - time1) / 100000);
}

The problem is that 20us is still not fast enough for my application ideally it would be around 2-3us (but the faster the better) I don't see why I wouldn't be able to do this as the microprocessor runs at 64mhz and I'm only able to sample at 50khz. Is there a way to sample analog signals faster using the Arduino IDE.

  • microphones

    So audio frequencies, then?

    The problem is that 20us is still not fast enough

    Really?

    that's 50kHz - "CD Quality" is only 44kHz

    I don't see why I wouldn't be able to do this as the microprocessor runs at 64mhz

    That's the frequency at which the CPU core runs - which is not the same as the speed that the ADC can sample.

    The Product Specification is here: 

    https://infocenter.nordicsemi.com/topic/ps_nrf52840/keyfeatures_html5.html?cp=4_0_0

    infocenter.nordicsemi.com/.../saadc.html

    BTW: Note that the correct spelling in 64 MHz - uppercase 'M' for mega (lowercase 'm' is milli) and Hz has a capital initial as it's a person's name.

    Is there a way to sample analog signals faster using the Arduino IDE

    It's beyond the chip's capabilities - so the IDE doesn't matter.

    If you really do need that sample rate, you'll need an external ADC.

    But note that BLE is not designed for high-volume, high-speed data - so what are you intending to do with all this data?

  • Yes audio frequencies 

    Really?

    that's 50kHz - "CD Quality" is only 44kHz

    The project I'm doing involves sound triangulation so the faster i can sample then the smaller the device can be and the more precise it can be.

    But note that BLE is not designed for high-volume, high-speed data - so what are you intending to do with all this data?

    Im not sending any audio data via Bluetooth all the triangulation calculation are being done on the processor so i only need to send very small amounts of data over ble

    https://infocenter.nordicsemi.com/topic/ps_nrf52840/keyfeatures_html5.html?cp=4_0_0

    So it says that the maximum sampling rate is 200KHz which is 4 times what I'm getting now so what do i need to do to get to that max sample rate.

  • FEARLESS_Z said:
    So it says that the maximum sampling rate is 200KHz which is 4 times what I'm getting now so what do i need to do to get to that max sample rate.

    I have no idea how this is handled in Arduino - my guess is that there is some overhead with the Arduino library that is hidden behind the curtains. Perhaps the analogRead implementation uses the nrfx_saadc_sample_convert function in blocking mode - this will take longer than just calling the SAMPLE task directly, so this might be where you are loosing the extra micro seconds.
    I also do not know how big the buffer size if configured to in Arduino, but in the non-Arduino case you would have to make sure that it is big enough to accommodate the rapid stream of samples, and also make use of the double buffering feature.

    If you were to achieve this either using the nRF5 SDK, or accessing the SAADC peripheral directly, you could achieve the 200 kHz sampling by configuring 3us acquisition time like you have already done.
    I guess you could incorporate the latter into your Arduino code - and access the registers directly. You would then need to set the mode field of the SAMPLERATE register to 1, and the CC field of SAMPLERATE to 80, before triggering the START and SAMPLE task.
    However, manipulating the registers directly might break the Arduino SAADC driver, if you are both manipulating registers directly and making use of their driver (if you manually make a change that the driver does not know about, it may be put in an invalid state and therefore throw an error / break). 

    If you were to use the nRF5 SDK for this, you should see the SAADC peripheral example to see how you may setup the SAMPLE task to trigger periodically through PPI.

    I am sorry that I cant be of more help with this, but I have a very limited personal experience with Arduino and their drivers. My best recommendation would be to do all the SAADC work directly through register manipulation, to still be able to do it through the Arduino IDE.

    Best regards,
    Karl

Related