# How to create a 100ms delay between button pressed and line grounded?

I have a line pulled to +5V. When a button is pressed, it should ground the line after a 100ms delay. How would I implement such a delay?

• How accurate does the 100ms delay need to be? If not very, then I would probably use a 555, but that's mainly because I know I've got several scattered about in old parts bins. – Glen Yates Aug 3 '17 at 19:55
• What are your design constraints? Do you really want to "ground the line"? Is the line some kind of weak pull up for a microcontroller? – PICyPICyPICy Aug 3 '17 at 19:57
• I realized that I asked a bad question -- sorry. I will now ask the correct one. – the_rover Aug 3 '17 at 20:22

simulate this circuit – Schematic created using CircuitLab

OA1 is an open drain comparator who's output is your line that's pulled up to 5V. The value of R1 will determine the length of the delay with larger values resulting in larger delays. The delay can be calculated as t = -ln(.5)*R1*C1.

If there are debounching issues, use a comparator with hysteresis (schmitt trigger).

Another, maybe better, way to change the timing is to change the voltage on the non-inverting input. This could be done with a DAC for digital control of the timing.

Here is the circuit:

The rest is firmware.

The passive pullup on the GP3 pin is enabled. The pin therefore goes low when the button is pressed and high when not. The firmware debounces this and puts the result on the OUT pin. Since debouncing includes a delay, you simply make this delay your desired 100 ms.

Normally I use 50 ms debouncing delay, because that's about the maximum where humans still feel the action is immediate. If you want a deliberate delay, make the debouncing delay as long as you need.

Look at the button every iteration of a timed loop. If the instantaneous state of the button is the same as the debounced state, reset the debounce counter to maximum.

Decrement the debounce counter each loop. If it reaches 0, then then the last state of the input line becomes the new debounced state. Reset the counter to maximum.

Adjust the debounce counter maximum value to however many loop iterations you do in your desired 100 ms delay time.

For example, lets say you run the loop every overflow of the free-running timer 0. That's every 256 instruction cycles, which is every 256 µs. It would take 391 iterations to get to 100 ms. Since it would be easier to use a 8 bit counter, you could divide the timer 0 overflows by 2 (run the loop every second time). Now it takes 195 iterations for 100 ms, which fits nicely into a 8-bit variable.

Since no background information was given we seem to be limited to a button, a ground line, a 5 V supply and an unspecified pull-up resistor. Assuming that the circuit will be used on Earth we can use gravity and distance to provide an accurate timer.

From $h = \frac {1}{2} gt^2$ where h is height, g is the acceleration due to gravity and t is time, and $v = gt$, where v is final velocity we can calculate that a body in free fall will travel 0.04903325m in 100 ms.

simulate this circuit – Schematic created using CircuitLab

Figure 1. Gravity timer. On push the OUT line will be pulled low after 100 ms.

The circuit requires a manual reset.

• +1 just for the silliness, and to point out how important providing good specs is. – Olin Lathrop Aug 3 '17 at 20:13
• I was going to say an RC circuit and an op amp, but this is better. – PICyPICyPICy Aug 3 '17 at 20:14
• You should probably make the height adjustable, the gravitational acceleration (g) is slightly different in different locations on the planet. Bouncing and sensitivity to shock and vibration are probably only some very minor issues and can be ignored. – Oskar Skog Aug 3 '17 at 20:15

You can use monostable circuit. For example, a 555 timer can be configured into a monostable circuit. Once trigger is activated the output will be high for given period of time (say 100ms). This can be used to develop a circuit for your requirements. Other than 555 timer, same operation can be realized using a micro-controllers or two transistors or logic gates etc. More ideas can be given, depending upon your interest.

How would I implement such a delay?