Flip-Flops, Debouncing and Counters: the short answer
A gate's output depends only on its inputs right now. A flip-flop also remembers.
- Level
- Intermediate
- Time
- About 6 min
- Topic
- Digital logic
Understand how circuits remember a state and count events, and why buttons must be debounced.
Before this: Logic Gates and Truth Tables, Digital Inputs and Outputs
A gate's output depends only on its inputs right now. A flip-flop also remembers. A D flip-flop copies its D input to its Q output at the moment the clock rises, then holds that value until the next rising edge. A row of flip-flops is a register; the 74HC595 shift register uses eight of them to turn three microcontroller pins into eight outputs.
A counter is flip-flops wired so each clock edge adds one. The CD4017 is a decade counter: each clock pulse moves a single HIGH output to the next of its ten pins, which makes LED chasers and simple sequencers without any code.
Buttons bounce. When the contacts close they chatter for a few milliseconds, which a counter or an interrupt sees as several presses. Fix it in hardware (an RC filter, around 10 kΩ and 100 nF, ideally into a Schmitt-trigger input) or in code by ignoring changes for 20 to 50 ms after the first one. In software, a variable that holds a state between loop() calls is your flip-flop, and `count++` on each clean press is your counter.
A flip-flop remembers one bit
A D flip-flop copies its D input to its output Q only on the clock edge, then holds it. Between edges the input can change and nothing happens. That is what makes it the building block of registers, counters and state machines: everything changes together, on the clock.
Datasheets list a setup time and a hold time. The data must be steady for the setup time before the edge and the hold time after it. Break them and the output can wander for a moment, which is called metastability.
Counters and dividers
Connect Q-not back to D and the flip-flop toggles on each clock edge, so its output is at half the clock frequency. Chain several and you get a binary counter, each stage halving again. Three stages divide by eight and count from 0 to 7.
In a ripple counter each stage is clocked by the previous one, so the outputs change slightly one after another and can show wrong values for a moment. A synchronous counter clocks all stages together and avoids this at the cost of extra gates.
Reset and modulus
A counter that must count to a number other than a power of two uses a reset: a gate detects the target value and clears the counter. A decade counter such as the 74HC390 or 4017 counts to ten. Always give the counter a defined start state, because on power-up the flip-flops come up randomly.
- Ignoring setup and hold times when the data arrives close to the clock edge.
- Reading a ripple counter's outputs while it is still changing.
- Forgetting to reset the counter, so it starts from a random value.
- fout = fin ÷ 2ⁿ
- frequency after n flip-flops in a chain
- Count to 2ⁿ − 1
- highest value of an n-bit counter
Practise this lesson with Maker
The explanation above is free for everyone. The practice for Intermediate lessons in every domain are part of the Maker plan at ₹499 a month, and Pro includes them too.
With Maker you also get: a worked example, a hands-on task, a 3-question check.
