Digital Logic
How a rising edge detector turns a press into one pulse
A clocked circuit sees a held button as a 1 on every tick, so anything it triggers happens over and over. An edge detector turns that long 1 into a single one-tick pulse at the moment of the press. It takes two D flip-flops and an AND gate: one flip-flop holds the button's state now, the other its state a tick ago, and a press is the one tick where now is 1 and before was 0.
Held is not pressed
Hold a button for a second in front of a 10 Hz clock and that's ten ticks of "pressed". If each tick toggles a lamp or adds one to a count, one press does it ten times. What you want is something that happens once per press, however long the finger stays down.
Now and a tick ago
FF1 samples the button on each clock edge. FF2 copies FF1's output on the same edge, so it always holds what FF1 held one tick earlier. Then:
| Button | now (FF1) | before (FF2) | PULSE |
|---|---|---|---|
| Up | 0 | 0 | 0 |
| Just pressed | 1 | 0 | 1 |
| Held | 1 | 1 | 0 |
| Just released | 0 | 1 | 0 |
PULSE lights for exactly one tick per press, whether the press lasts a tick or a minute: a rising-edge detector. Swap the inputs, FF1's Q̄ AND FF2's Q, and it marks the release instead: a falling-edge detector. An XOR of the two samples marks both.
Live experiment
Hold the button, get one pulse
A clock ticks two and a half times a second. FF1 samples the button, FF2 keeps the sample from a tick ago, and an AND of FF1's Q and FF2's Q̄ makes PULSE, which flips a lamp through an XOR. Hold the button as long as you like: one pulse, one flip. Then wire the AND to FF2's Q instead.
“One press, one pulse” is lesson 5 of 6 in Toggle Tides, Digital Logic World 3. Free with an account. Lessons unlock in order, so you’ll do the 18 before it first.
Push on, push off
Feed PULSE into a third flip-flop through an XOR with its own output:
On the pulse tick, Q XOR 1 flips the lamp. On every other tick, Q XOR 0 keeps it. One press turns it on, the next turns it off, and holding the button does nothing more. The lamp changes up to two ticks after the press, 0.2 s at 10 Hz, quicker than anyone notices.
Why not clock from the button?
You could wire a debounced button straight to a flip-flop's clock pin. But in an FPGA or a large chip, every flip-flop shares a few dedicated clock wires that reach the whole chip at the same instant. A button on a clock pin starts a second clock, and signals passing between the two can be caught mid-change. Detecting the edge and using the pulse as an enable keeps everything on one clock. That's synchronous design, the first rule of FPGA work, and edge detectors sit on nearly every input an FPGA reads.
The same idea in code is two variables: if the button reads pressed now and didn't last time, act, then save this reading for next time. Every power button, torch mode button and play-pause control works this way.
Free lesson
Now build it yourself
Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.
“One press, one pulse” is lesson 5 of 6 in Toggle Tides, Digital Logic World 3. Free with an account. Lessons unlock in order, so you’ll do the 18 before it first.
Or start from the very first lesson, no account neededPractice questions
Three quick questions from the lessons. Pick an answer to check it.
Show the answer
Answer: Now is 1 and before was 0: just pressed. FF2's Q̄ is 1 when the old sample was 0, so the AND needs now 1 and before 0. That's the single tick right after a press.
- Why not “Now is 0 and before was 1: just released”? That's the falling edge, the release. It needs FF1's Q̄ and FF2's Q instead.
- Why not “Both are 1: held”? Held means the old sample is 1 too, so FF2's Q̄ is 0 and the AND gives 0.
- Why not “Both are 0: not pressed”? With the button up, FF1's Q is 0, so the AND gives 0.
Show the answer
Answer: FF1's Q̄ AND FF2's Q. Released means now 0 and before 1: Q̄ of now, Q of before. That's a falling-edge detector.
- Why not “FF1's Q̄ AND FF2's Q̄”? That's 1 on every tick the button stays up.
- Why not “FF1's Q AND FF2's Q”? That's 1 on every tick the button stays held, not once at the release.
- Why not “FF1's Q AND FF2's Q̄”? That's the rising edge, the moment of the press.
Show the answer
Answer: Flicker on and off every tick. FF1's Q stays 1 while the button is held, so the XOR flips the lamp on every tick. The edge detector turns that long 1 into a single pulse.
- Why not “Flip once”? Flipping once needs a single-tick pulse. A held button gives a 1 on every tick.
- Why not “Stay on”? While held, the XOR sees a 1 on every tick and flips the lamp each time, so it can't stay on.
- Why not “Stay off”? The first held tick already flips the lamp on, and the next flips it off again.
“One press, one pulse” is lesson 5 of 6 in Toggle Tides, Digital Logic World 3. Free with an account. Lessons unlock in order, so you’ll do the 18 before it first.
Questions people ask
How do you detect a rising edge in digital logic?
Keep two samples of the signal, one clock tick apart, in two flip-flops. A rising edge is the tick where the new sample is 1 and the old one was 0, so AND the first flip-flop's Q with the second's Q̄. The output is high for exactly one clock tick.
How do you detect a falling edge, or any edge?
A falling edge is new sample 0 and old sample 1: the first flip-flop's Q̄ AND the second's Q. An XOR of the two samples is 1 on either kind of edge, because that's when they differ.
How do I detect a button press in code instead?
Keep the last reading in a variable. Each pass, if the new reading is pressed and the old one wasn't, act; then store the new reading as the old one. It's the same now-and-before comparison the flip-flops make.
Related topics
- How a D flip-flop worksCopy D to Q at the clock's rising edge, then hold it: one bit of memory.
- Logic gates explainedAND, OR, NOT, XOR and their bubbled twins: each one's rule is a truth table.
- How to debounce a button on ArduinoWhy one press counts as four, and the 50 ms fixes that make it count once.
- How to program a robot as a state machineOne state at a time, each with its own job and its own ways out.