Arduino digital read maximum speed

Thread Starter

Alasttt

Joined May 13, 2015
68
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 ?
 

Papabravo

Joined Feb 24, 2006
21,094
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.
 

Thread Starter

Alasttt

Joined May 13, 2015
68
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.
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 ?
 

djsfantasi

Joined Apr 11, 2010
9,155
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.
 

MikeML

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

Code:
Loop
     DigitalRead(x,mike)
     DigitalWrite(y,mike)
Forever
where x, y are port pins and mike is byte.
 

JWHassler

Joined Sep 25, 2013
306
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 ?
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'
 

djsfantasi

Joined Apr 11, 2010
9,155
Translatinmg Mike's example to Arduino...
Code:
#define x 4
#define y 5
byte mike=0;

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:
  mike=digitalRead(x);
  digitalWrite(y,mike); 
}
 

flat5

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

Code:
void setup() 
{
  Serial.begin(115200); // 9600 115200 300 2
}
 
Top