Teensy 4.0 sampling rate 1MHz ?

rsh50

Member
Hi All,
I am working on a project that requires a high sampling rate above 192kHz and ideally 1MHz. The Audio Shield datasheet indicates its max sampling rate is 96kHz.
I wanted to know if Teensy 4 alone can sample higher rates without an external Audio shield but using its onboard ADC. I saw a few posts, but there were no conclusive solutions that I could use.

I wanted to know
1) The maximum sampling rate of the Teensy 4.0 board using its onboard ADC and how I can achieve the maximum sampling.
2) How and where can I store this data, as the memory onboard might not be enough for 1MHz?

I have attached the code below that samples at 96kHz using Audio Shield Rev D2, but how can I modify this to max sampling using only Teensy 4.0
Any suggestions are welcome. Thank you

Code:
#define AUDIO_SAMPLE_RATE_EXACT 96000.0f
#define AUDIO_SAMPLE_RATE AUDIO_SAMPLE_RATE_EXACT
#include <Audio.h>
#include <Wire.h>
#include <utility/imxrt_hw.h>

// Single-channel configuration
AudioInputI2S            i2s_in;
AudioRecordQueue         queue1;
AudioConnection          patchCord1(i2s_in, 0, queue1, 0);
AudioControlSGTL5000     sgtl5000_1;

// Buffer size (adjust as needed)
#define BUFFER_SIZE 440320
DMAMEM byte coil_w[BUFFER_SIZE];   

elapsedMicros recordingTime;
int buf_idx = 0;
const int led = 13;


void setup() {

  AudioMemory(100);
 
  sgtl5000_1.enable();
  sgtl5000_1.inputSelect(AUDIO_INPUT_LINEIN);
  sgtl5000_1.volume(0.5);
 
  pinMode(led, OUTPUT);
  digitalWrite(led, HIGH);
  Serial.begin(115200);
}

void loop() {
  if (Serial.available()) {
    char ch = Serial.read();
    if (ch == 'c') {
      digitalWrite(led, LOW);
      record_short_seg();
    }
    else if (ch == 'w') {
      Serial.write(coil_w, BUFFER_SIZE); // Send data via serial
    }
  }
}

void record_short_seg() {
  startRecording();
  buf_idx = 0;
  recordingTime = 0;
  while (buf_idx < BUFFER_SIZE) {
    if (queue1.available()) {
      memcpy(coil_w + buf_idx, queue1.readBuffer(), 256);
      queue1.freeBuffer();
      buf_idx += 256;
    }
  }
  stopRecording();

  // Calculate effective rate
  float duration_sec = recordingTime / 1000000.0;
  Serial.print("Time: ");
  Serial.println(duration_sec);
  float effective_rate = (BUFFER_SIZE / 2) / duration_sec; // 16-bit samples
  Serial.print("Effective Rate: ");
  Serial.println(effective_rate);
}

void startRecording() {
  queue1.begin();
}

void stopRecording() {
  queue1.end();
  while (queue1.available() > 0) { // Flush buffers
    queue1.readBuffer();
    queue1.freeBuffer();
  }
}
 
There is also a working program from a different thread (link below). This one does not use DMA, but rather simply uses the ADC's internal timer. It also optionally logs the data to a file on SD, which can handle quite high logging rates and very large files. If you have PSRAM on the under-side of your T4.1, you might be able to log a small amount of data there. It's also possible to stream data to your host via USB at well over the required 1MSPS. I think you can even just write to serial monitor, which is blazingly fast, and then simply copy and paste from serial monitor into a spreadsheet.

 
Back
Top