Teensy 4.1 Datalogger

Dave18

Member
Hello,

I am looking to use a teensy 4.1 as a datalogger and have simplified my code to a minimum working example. In the full version of the code, I would take ~1000 samples instead of 10 and write them to an SD card. For this code, the time between the samples is 7-8 microseconds and I would like to know what I can do to increase the sampling rate.

Code:
#include <ADC.h>

int signalPin = A1;  // Set the pin for the incoming signal
ADC *adc = new ADC();   // adc object
String dataBuffer = "";  //Initialize the databuffer

void setup() {
  Serial.begin(9600);       //Initialize the serial monitor
  pinMode(signalPin, INPUT_DISABLE);
  adc->adc0->setAveraging(4);                                        // Set the number of averages
  adc->adc0->setResolution(12);                                      // Set the resolution of the ADC
  adc->adc0->setConversionSpeed(ADC_CONVERSION_SPEED::HIGH_SPEED);   // Set high-speed conversion
  adc->adc0->setSamplingSpeed(ADC_SAMPLING_SPEED::VERY_HIGH_SPEED);  // Set high-speed sampling rate

  for (int i = 0; i < 10; i++) {                                        //Take 1 reading per iteration; each iteration takes 7-8 microseconds (sampling rate ~138 kHz)
    int sensorValue = adc->adc0->analogRead(signalPin);                 //Record the value of the sensor pin
    dataBuffer += String(micros()) + "," + String(sensorValue) + "\n";  //Write the time and the senor value to the data buffer
  }

  Serial.print(dataBuffer);  //Print the data buffer, or write to SD card
  dataBuffer = "";           //Reset the data buffer
}

void loop() {
}

Thanks,
David
 
String concatenation is unnecessary overhead - however normally I'd advise using nsprintf() to append to a buffer but Teensy only has sprintf not nsprintf I believe.

Maybe hard-code a buffer-appending print function to do this?
 
Have you looked into the example "conversionSpeed"? It says in continuous mode it is faster?

Set averaging to 1?

Also "adc_test" could give you more hints what limits you can expect.

What sampling rate do you need?
 
Have you looked into the example "conversionSpeed"? It says in continuous mode it is faster?

Set averaging to 1?

Also "adc_test" could give you more hints what limits you can expect.

What sampling rate do you need?
Hi Tom, thank you for your response. Where would I find the conversionSpeed example to look into? Where do I find the adc_test? I do not have a specific sampling rate in mind, just closer to the clock speed.
 
String concatenation is unnecessary overhead - however normally I'd advise using nsprintf() to append to a buffer but Teensy only has sprintf not nsprintf I believe.

Maybe hard-code a buffer-appending print function to do this?
Hi Mark, thank you for your help. I tried this with some success, but am not very experienced in this area, so pardon my naivety.

When I try to us sprintf, I need to use a char buffer instead of a string buffer. A char buffer is not larger enough to store my 1000 data points. Is there a different way I should go about implementing this?
 
Hi Tom, thank you for your response. Where would I find the conversionSpeed example to look into? Where do I find the adc_test? I do not have a specific sampling rate in mind, just closer to the clock speed.
I am referring to the examples that come with the Teensyduino installation in the Arduino IDE under examples/Teensy/ADC.

edit: it seems that lower resolutions (8bit) give you also a higher sampling rate.
 
To increase sampling rate, it is better to use a binary file format and write directly raw values. And avoid any conversions in the Teensy. Then write a small application, Python, C,.... on the PC to convert it to a suitable format.
Long time ago, I found a free data viewer app, which displayed graphs, curves, .... and able to read any known data format. It had an option to specify your own binary format. But can't remember its name, and where I found it.

Angelo
 
Back
Top