Hi everyone
is there anyone can help me measure time between two TTL signals via the arduino uno board?
thanks
When the first signal arrives, record the value of millis() or micros() in a variable.
Do the same when the second signal arrives.
Subtract the first value from the second.
The arduino processors timer provide you with an input capture functionality. Either look into the datasheet or try using this library.
Keep in mind that a TTL signal is always present. The VALUE may change, from HIGH to LOW or from LOW to HIGH. It is most like the time of the change that you are interested in, but the complete lack of details in your post makes that hard to be sure of.
Thanks everyone.
The first signal is periodic Square wave say 50KHZ. the second signal comes as a surprise. (TTL)
I need the time between the latest pulse periodic signal to the second signal that comes surprisingly.
Then i need to make a table -
how many times there was x delay between 2 signals.
how many times there was y delay between 2 signals.
how many times there was z delay between 2 signals.
and so on..
As I mentioned, the signals are ALWAYS present. One signal may be in a HIGH state while the other is also HIGH. One may be in a LOW state while the other is HIGH. Or, they may both be in a LOW state,
Draw a picture, of a square wave (using one color) and of the other signal (using another color). Show, on that picture, EXACTLY what you are trying measure.
How can i upload a file in here?
Or maybe you want it in mail?
Adir.eilon@gmail.com
Thanks
time between 2 signals.pdf (55.5 KB)
Something like this might work:
//for an Uno, Nano etc
//for a 2560 could also be 18-21...
const int pinA = 2;
const int pinB = 3;
volatile bool
bevtB;
volatile unsigned long
timeA,
timeB;
void setup()
{
Serial.begin(9600);
pinMode( pinA, INPUT );
pinMode( pinB, INPUT );
attachInterrupt(digitalPinToInterrupt(pinA), ISR_pinA, RISING);
attachInterrupt(digitalPinToInterrupt(pinB), ISR_pinB, RISING);
bevtB = false;
}
void loop()
{
unsigned long
timeDiff;
if( bevtB )
{
if( timeB >= timeA )
{
Serial.print( "Time between pulses was: " );
timeDiff = timeB - timeA;
Serial.print( timeDiff );
Serial.println( "uS" );
}//if
noInterrupts();
bevtB = false;
interrupts();
}//if
}//loop
void ISR_pinA( void )
{
timeA = micros();
}//ISR_pinA
void ISR_pinB( void )
{
timeB = micros();
bevtB = true; //signals we saw an edge on B to mainline
}//ISR_pinB
Keep in mind that even a signal as slow as 50kHz signal has a period of 20uS and it takes the processor time to respond to and process things. How much accuracy do you need?
An input capture might be a better choice.
Thanks a lot Blackfin.
Can you by any chance explain every row you wrote (as a code)?
Remember that i need Tx and Rx of the arduino to show the results on a Pc so maybe we should take another pins of the arduino.
Thanks again.
Using 'micros()' will give you resolution of about 4 microseconds (250 kHz). At 50 kHz that's only 5 intervals.
For higher resolution I would set up Timer1 for input capture. Use a rising edge external interrupt to set Timer1 to 0 and the input capture interrupt to record the value of Timer1 when the input capture pin goes high. This will give you a precision of 1/16th of a microsecond so you will get about 320 intervals.
There's a pretty good example of an input capture here:
It's quite a bit more complex but gives much better resolution.