Arduino digital read maximum speed

Discussion in 'Embedded Systems and Microcontrollers' started by Alasttt, May 20, 2015.

  1. Alasttt

    Thread Starter Member

    May 13, 2015
    68
    0
    Ive got a data pulse which is 20Khz. time period of 50 us. Im using digital read to read this data then I want to process it. But the arduino doesnt print the 0s and 1s on the serial at that speed . The maximum I can get is a time period of 0.5ms. How can i increase this ?
     
  2. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,388
    497
    Pull the chip datasheet and see the ADC section.
     
  3. Alasttt

    Thread Starter Member

    May 13, 2015
    68
    0
    Thanks. Does digitalread use the adc ? I thought it took the voltage compared it to a reference voltage to decide whether it is a 1 or a 0,
     
  4. Papabravo

    Expert

    Feb 24, 2006
    10,140
    1,790
    Think about this for a moment. It could never print the characters at the sampling speed unless the bit rate was in excess of 220,000 bits per second. It seems unlikely that digital read would use the ADC. The actual point was that you need to pull the data sheet and ditch the Arduino environment in favor of some bare metal programming in assembly language.
     
  5. Alasttt

    Thread Starter Member

    May 13, 2015
    68
    0
    Right thanks. Why is it that the bare metal coding will increase the speed ?. Is it because it will take less flash memory compared to the arduino higher level libraries ?
     
  6. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,804
    832
    The libraries, while presenting additional functionality also introduce additional code to be executed. The more code, the longer it takes and the less frequency it can handle measuring.

    Bare metal coding only executes the bare minimum of steps, so the cycle time is less and the frequency of measurements is more.
     
  7. MikeML

    AAC Fanatic!

    Oct 2, 2009
    5,450
    1,066
    However, all of the time is going into the serial print statement. To see how fast the digital read/write operations are, try:

    Code (Text):
    1. Loop
    2.      DigitalRead(x,mike)
    3.      DigitalWrite(y,mike)
    4. Forever
    where x, y are port pins and mike is byte.
     
  8. JWHassler

    Member

    Sep 25, 2013
    201
    33
    What are you measuring?
    If you are waiting for the occurrence of a 50uSec pulse, search for help on 'external interrupt'
    If counting 20Khz pulses, search for 'Frequency counter'
    If measuring nominally-50nSec pulses, search for 'Pulse-width measurement'
     
  9. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,804
    832
    Translatinmg Mike's example to Arduino...
    Code (Text):
    1. #define x 4
    2. #define y 5
    3. byte mike=0;
    4.  
    5. void setup() {
    6.   // put your setup code here, to run once:
    7.  
    8. }
    9.  
    10. void loop() {
    11.   // put your main code here, to run repeatedly:
    12.   mike=digitalRead(x);
    13.   digitalWrite(y,mike);
    14. }
     
  10. flat5

    Active Member

    Nov 13, 2008
    403
    17
    and include this if you want to see data on terminal screen:

    Code (Text):
    1.  
    2. void setup()
    3. {
    4.   Serial.begin(115200); // 9600 115200 300 2
    5. }  
    6.  
     
Loading...