Measuring the Timing of An Arduino's Signal to Output

Hi everyone,

For a project that I am working on, I am trying to measure the time between an arduino receiving an input signal from a sensor and an output (something like an LED) to activate. I plan on putting different circuit models to stall the signal between the arduino and the output.

I have tried to use functions such as mills() and micros() to measure this time, but I notice that the time it measures decreases to the point of being 0 and then nothing happens. I do not think what I have done is correct but I cannot figure out what to do. Below is an example of coding I tried using:

int led = 12; //Define the LED pin
int buttonpin = 2; //Define the push button pin
int val1; //Define a numeric variable 1
int val2; //Define a numeric variable 2
unsigned long startTime; //Start Time
unsigned long currentTime; //End Time
unsigned long elapsedTime; //Difference of Times

void setup()
{
  pinMode(led,OUTPUT);
  pinMode(buttonpin,INPUT);
  Serial.begin(9600);
}

void loop()
{
  val1 = digitalRead(buttonpin); // check the state of the button
  if(val1==HIGH) // if button is pressed, turn LED on
  {
    startTime = millis();
    digitalWrite(led,HIGH);
  }
  else
  {
    digitalWrite(led,LOW);
  }
  val2 = digitalRead(led);
  if (val2 == HIGH)
  {
    currentTime - millis();
  }
  elapsedTime = currentTime - startTime;
  Serial.println(elapsedTime);
  delay(100);
}

I have tried looking this up on the Internet to no avail. I am not sure if this is possible, but if so, could anyone help me?

The problem is that you are looking at the level of the push buttons, you need to look at the edge. That is you need to start timing when the input becomes high not when it is high.
To do this keep a record of the last state you read and compare it to the current state. Only when the current state is high and the last state was low start the timer.
Also do not use an else with this statement as that just messes you up.

An example of this is given in the examples in the IDE called state change detection.

Note micros updates only every four microseconds.

If you want to measure the time Arduino needs to discover a button is pressed and take the appropriate action it is better to get another device to monitor both input and output. In other words get a logic analyzer (or an oscilloscope) - it is an incredibly useful device.

Grumpy_Mike:
The problem is that you are looking at the level of the push buttons, you need to look at the edge. That is you need to start timing when the input becomes high not when it is high.
To do this keep a record of the last state you read and compare it to the current state. Only when the current state is high and the last state was low start the timer.
Also do not use an else with this statement as that just messes you up.

An example of this is given in the examples in the IDE called state change detection.

Note micros updates only every four microseconds.

Grumpy_Mike:
The problem is that you are looking at the level of the push buttons, you need to look at the edge. That is you need to start timing when the input becomes high not when it is high.
To do this keep a record of the last state you read and compare it to the current state. Only when the current state is high and the last state was low start the timer.
Also do not use an else with this statement as that just messes you up.

An example of this is given in the examples in the IDE called state change detection.

Note micros updates only every four microseconds.

I understand what you mean, but how would you recommend I measure the time between the state changes? I am trying to determine where I should put the micros() functions in the code and I am stumped on where they should be. I would buy something like an oscilloscope, but I do not have the money for that so I am just trying to measure it with the arduino itself.

I am editing off of the IDE example "State Change Direction" for reference of the code I am trying to place the micros() functions in.

I am trying to measure the time between an arduino receiving an input signal from a sensor and an output (something like an LED) to activate. I plan on putting different circuit models to stall the signal between the arduino and the output.

The minimum time between the reading of an input and the execution of an output is likely to be less than the 4 microsecond resolution of micros(). For more precision you will need to use the input capture on a hardware timer.

What time range are you looking at for the delay of the output after reading the input?

What type of circuitry are you planning to use to delay the output.

As others have said, this is truly the job for a scope or logic analyser.

but how would you recommend I measure the time between the state changes?

Once the state change has been detected make a note of the micros timer. Once you have finished the action again make a note of the micros timer. Then subtract one from the other and that is your time.

However, that is likely to be very short. From memory it takes about 4 uS to do a digitalWrite so you are only going to read 0 or 4.

Measuring such a small time is not on with an Arduino alone. If you can arrange To repeat the trigger action many times then you could measure how long it takes for 1000, and then divide the result by 1000, but as you are using a push button input then you can’t.

So it looks like you’re trying to do the impossible.

Why do you want to measure this in the first place?
You could calculate it by taking the assembled machine and counting the number of clock cycles it takes.

Grumpy_Mike:
You could calculate it by taking the assembled machine and counting the number of clock cycles it takes.

This. You can calculate the exact timing by looking at the assembly code, and the processor data sheet. If it's an educational project (which I strongly suspect it must be), then as a bonus, you can explain in your thesis or report, exactly why a certain time delay occurs.

  1. Usually the important time is from the triggering action to the response - not from the time Arduino notices the trigger. So by definition the Arduino cannot measure this time. Also it is difficult to estimate using cycle counting.
  2. Probably you are interested in the worst case delay. Any active interrupt (such as millis, Serial communication) may increase the response time considerably.