Skip to content
BetterDL

Sequence detector

Also called: pattern detector, sequence recognizer, bit-pattern detector

A finite state machine that outputs 1 whenever the most recent inputs match a given bit pattern, such as 101, by tracking how much of the pattern it has seen.

A sequence detector watches a serial stream of bits, one per clock, and raises its output when the latest bits spell out a target pattern. It's the classic finite state machine design exercise, and real hardware uses the idea to find frame markers and sync words in serial data.

The states record how much of the pattern has been matched so far: for 010, "nothing", "seen 0" and "seen 01". Then:

  • Progress arrows: each partial match plus the next pattern bit leads to the next partial match.
  • Every other arrow: append the new bit and keep the longest ending that is still a start of the pattern. That's the suffix rule. It stops you throwing away progress you still have.

Two choices shape the design:

  • Mealy or Moore. An n-bit pattern needs n Mealy states, or n + 1 Moore states (the extra one shows the match).
  • Overlapping or non-overlapping. Only the arrows taken right after a match change.

Always check a finished detector by tracing an input you've counted by hand, including a near miss.

start0/01/00/01/00/11/0ABC

Worked examples

Example

Designing a Mealy 010 detector

Overlapping, Mealy. States: A = nothing, B = seen 0, C = seen 01. The diagram shows the result.

  1. 1.

    Progress: A on 0 → B, B on 1 → C, each with output 0.

  2. 2.

    Match: C on 0 completes 010, output 1. The longest ending of 010 that starts the pattern is 0, so go to B.

  3. 3.

    A on 1: 1 starts nothing → stay in A.

  4. 4.

    B on 0: 00 ends in 0 → stay in B.

  5. 5.

    C on 1: 011 has no ending that starts 010 → back to A.

Example

Checking it by tracing

Apply 0, 1, 0, 1, 0 from A. By hand, 010 ends at inputs 3 and 5 (sharing input 3).

  1. 1.

    0: A → B, 0. 1: B → C, 0. 0: C → B, 1.

  2. 2.

    1: B → C, 0. 0: C → B, 1.

  3. 3.

    Outputs 0, 0, 1, 0, 1: matches at inputs 3 and 5 ✓.

Common mistakes

  • Sending every mismatch back to the start. In a 010 detector, B on 0 must stay in B: the new 0 is a fresh start.

  • Forgetting the extra Moore state. A Moore detector needs a state whose only job is to output 1.

  • Testing only with a clean match. Include a near miss and a repeated bit so every arrow gets used.

Practice Sequence detector

Interactive questions with instant feedback and a worked solution for every wrong answer.

Learn it step by step

Sequence detector is taught in Finite State Machines.