Teensy 3.6 Speed of analogRead

Status
Not open for further replies.
Hi,
I'm working with an Accelerometer, i use the ADC library but the maximum which i get is 13760-15020 Sapmles per second.

My code:
Code:
#include <ADC.h>

ADC *adc = new ADC();; // adc object
volatile int elapsed; 
int readPin = A9; // ADC0
int value = 0;

void setup() {

    pinMode(readPin, INPUT);
    Serial.begin(1000000);
    adc->setAveraging(0); // set number of averages
    adc->setResolution(12); // set bits of resolution
    adc->setConversionSpeed(ADC_CONVERSION_SPEED::HIGH_SPEED); // ,  change the conversion speed
    adc->setSamplingSpeed(ADC_SAMPLING_SPEED::HIGH_SPEED); // change the sampling speed
    adc->startContinuous(readPin);
    while(!Serial){};
}

void loop() {
    elapsedMicros elapsed;    
    while (elapsed <= 1000000)
   {
      value = (uint16_t)adc->analogReadContinuous(ADC_0); // the unsigned is necessary for 16 bits, otherwise values larger than 3.3/2 V are negative!
      Serial.println(value, DEC);
    }
    exit(0);
}

I don't know what i'm doing wrong but the lines
Code:
    adc->setConversionSpeed(ADC_CONVERSION_SPEED::HIGH_SPEED); 
    adc->setSamplingSpeed(ADC_SAMPLING_SPEED::HIGH_SPEED);
don't really matter, i run my code without them and i still get 13-15k.

Any ideas how can i increse the speed of reading?
 
Last edited:
Thanks for your fast reply. I have seen these example and comments but how i said it doesn't matter if i use LOW_SPEED or VERY_HIGH_SPEED or if i jutr comment it out, i always get 13-15k.

I thought about DMA, i found also some code https://forum.pjrc.com/threads/43233-ADC-conversion-time but i get errors like :

Code:
error: could not convert '((ADC_Module*)adc.ADC::adc0)->ADC_Module::fail_flag' from 'volatile ADC_Error::ADC_ERROR' to 'bool' if(adc.adc0->fail_flag) {
error: no matching function for call to 'usb_serial_class::println(volatile ADC_Error::ADC_ERROR&, int)' Serial.println(adc.adc0->fail_flag, HEX);
 
maybe you are measuring the speed of Serial.print(), try
Code:
void loop() {
  elapsedMicros elapsed;
  uint32_t cnt = 0;
  while (elapsed <= 1000000)
  {
    value = (uint16_t)adc->analogReadContinuous(ADC_0); // the unsigned is necessary for 16 bits, otherwise values larger than 3.3/2 V are negative!
    cnt++;

  }
  Serial.println(cnt);
  delay(3000);
}
 
actually, i think the semantics requires that you check if conversion is ready
Code:
void loop() {
  elapsedMicros elapsed;
  uint32_t cnt = 0;
  while (elapsed <= 1000000)
  {
    if (adc->isComplete(ADC_0)) {
      value = (uint16_t)adc->analogReadContinuous(ADC_0); // the unsigned is necessary for 16 bits, otherwise values larger than 3.3/2 V are negative!
      cnt++;
    }
  }
  Serial.println(cnt);
  delay(3000);
}
 
I get 2997402 Samples. I did something like that already. Probably i'm measuring the speed of Serial.print(), but i would like to save the reading to a file, i do it with Putty->Logging. Do you think that the speed of Serial.print() is the issue that i can't get faster? Is there any alternativ to save the measurement?

edit.
with code from Post#5 i get 117188
 
Status
Not open for further replies.
Back
Top