Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 3 of 3

Thread: analogRead() vs Frequency on T4

  1. #1
    Junior Member
    Join Date
    Oct 2019
    Posts
    2

    analogRead() vs Frequency on T4

    I modified an example of code to determine the approximate speed of Teensy 4's analogRead() at 600Mhz; then, I tested different clock speeds ranging from from 24Mhz to 816Mhz to see if changing frequency led to slower or faster read speeds.

    I noted that clock speed changes -apparently- did not affect the speed of the analogRead(); analogRead() took approximately 5 microseconds inside a for cycle and it did not move from there.

    Is the analogRead() speed always a constant unrelated to the clock speed?


    The code below explained: It take a start time with micros(). Executes 100 times analogRead() by a For{}. Takes the end time of the For{} and determines the average time needed by one analogRead() operation. 5 microsends.

    Code:


    Code:
    #define VREF (3.292)         // ADC reference voltage (= power supply)
    #define VINPUT (1.328)       // ADC input voltage from NiCd AA battery
    #define ADCMAX (4095)        // maximum possible reading from ADC
    #define EXPECTED (ADCMAX*(VINPUT/VREF))     // expected ADC reading
    #define SAMPLES (1)      // how many samples to combine for pp, std.dev statistics
    
    const int analogInPin = A0;  // Analog input is AIN0 (Teensy3 pin 14, next to LED)
    const int LED1 = 13;         // output LED connected on Arduino digital pin 13
    
    int sensorValue = 0;        // value read from the ADC input
    unsigned long StartTime=0;
    unsigned long TotTime=0;
    unsigned long ReadTimeUnit=0;
    int x=0;
    void setup() {    // ==============================================================
         
        
          pinMode(LED1,OUTPUT);       // enable digital output for turning on LED indicator
          // analogReference(EXTERNAL);  // set analog reference to internal ref
          analogReadRes(12);          // set ADC resolution to this many bits
          analogReadAveraging(SAMPLES);    // average this many readings
         
          Serial.begin(115200);       // baud rate is ignored with Teensy USB ACM i/o
          digitalWrite(LED1,HIGH);   delay(1000);   // LED on for 1 second
          digitalWrite(LED1,LOW);    delay(3000);   // wait in case serial monitor still opening
         
          Serial.println("# Teensy ADC test start: ");
    } // ==== end setup() ===========
    
    void loop() {  // ================================================================ 
         
          
                 
          StartTime = micros();   // record start time in microseconds
              
          for (int i=0;i<100;i++) {
          x = analogRead(analogInPin);
                                       } 
          TotTime = micros()-StartTime;   // record duration in microseconds
          
          ReadTimeUnit=TotTime/100;
          
          Serial.print("Microseconds/ADC-Read: ");
          Serial.print(ReadTimeUnit,DEC);
          Serial.print("     ADC VALUE: ");
          Serial.print(x, DEC);
          Serial.println();
          delay(1000);      
                
                }
    Last edited by defragster; 12-11-2019 at 08:53 AM. Reason: add # CODE tag

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    10,342
    There is an active thread looking into T4's analogRead() : pjrc.com/threads/58387-Teensy-4-0-which-pins-for-which-ADC

    That is a WIP you might try.

  3. #3
    Junior Member
    Join Date
    Oct 2019
    Posts
    2
    Quote Originally Posted by defragster View Post
    There is an active thread looking into T4's analogRead() : pjrc.com/threads/58387-Teensy-4-0-which-pins-for-which-ADC

    That is a WIP you might try.

    Thank you!

    Marco

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •