Latency exists in all electronic circuits and can occur by hardware (in all circuits), and by hardware and software in the cases of microcontrollers.
What happens is that there is an interval between the instant which a circuit receives a command and the instant it provides an output.
Even in the simple control of a relay, there is a time between the instant that the sensor has the light cut off and the relay closes its contacts, according to the circuit of Figure 1.
It's just microseconds, but it can mean a lot in an application where that time is important.
All circuits have latencies, which can vary between nanoseconds and microseconds, depending on the components used and the number of stages.
But the biggest problem is with microcontrollers, because the hardware latency for these components is small, but software latency can influence, and very much, their operation in a design.
Software Latency
What happens is that microcontrollers perform their functions by sequentially processing a program. It is one instruction after another, and it takes time. In some cases, we even introduce a delay between one execution and another on purpose.
For example, if we have a microcontroller read two inputs, in which two sensors are connected to check, if they were triggered simultaneously, it does it sequentially, first one and then the other as shown in Figure 2.
Well, let us assume that in the desired application, the two sensors are closed for very short time intervals, as shown in Figure 3.
If when we finish reading one the other is no longer activated, the reading will not be of the same time, as shown in Figure 3.
Then see that when reading A, the sensor is at level 1, but when the reading is made at sensor B, it has already returned to level 0, and when comparing the results the microcontroller takes them as non-simultaneous.
This worsens if a delay is introduced between one instruction and another. In this case, as we have seen, the "delay 500" was introduced only at the end of the execution of the block (subroutine). However, if this delay is between one reading and another, the problem will be greater.
Delay 500 means half a second between one operation and another and this can mean that a reading is made from one sensor to the one from the other only half a second later, what is a long time for an electronic circuit.
If the program you have developed for your design includes many delays, be careful as they can influence the outcome you are going to get.
Maybe that weird run you got for your design does not actually feedback what you want when the sensors are activated, or there is an erratic functioning of the devices controlled, like motors, when you turn on your design, it may be in latencies produced in the execution of the program.
Analyze, because maybe a change in the software can solve the problem.
Also check to be able to work around the problem using external logic, or input logic shields.
Using Hardware
You can have the simultaneous reading of two sensors without depending on the sequential execution of the program with external circuits (hardware) and some of them are simple.
For example, a NAND or AND gate allows you to know whether two sensors are triggered simultaneously even if the duration of their contact is of the order of microseconds, as shown in Figure 4.
Note that in this case, we use only one input of the microcontroller and therefore we know the states of the sensors with a reading.
And, the logic functions can also be obtained with diodes and transistors as described in article MIC185.
The Importance of Knowing Digital Logic
Many despise the knowledge of digital electronics, finding that it is enough to know how to use a microcontroller and its programs, so that no external logic functions are needed.
However, digital logic is important precisely in cases where the microcontroller does not do everything, or has problems when we want it to be everything.
It is at this point, good knowledge of digital (and analog electronics) can help solve latency and many other problems.