notesonly.in

One notebook for every subject — open it anywhere.

Log in

Finite State Machines Explained: Complete Beginner's Guide

A finite state machine (FSM) is a mathematical model of a system that can be in exactly one of a fixed, finite number of states at any moment. When an input arrives, the machine moves from its current state to a next state according to a fixed set of transition rules, and it may produce an output at the same time. It is called "finite" because the number of states is limited, which also means the machine has a limited memory of the past.

FSMs matter because almost every digital device you use is controlled by one. Traffic light controllers, vending machines, elevator controllers, washing machine timers, UART serial receivers, I2C bus handlers, and even the "logged out / logging in / logged in" logic of a website are all state machines. Learning FSMs is the first step to designing real digital hardware, because every synchronous digital circuit is an FSM built from flip-flops plus combinational logic.

Key Concepts

  • State: a distinct condition of the system, such as "red light on" or "waiting for coin".
  • Input alphabet: the set of all possible inputs, for example {coin_1, coin_2} or {0, 1}.
  • Transition function: the rule S(next) = f(S(current), input) that decides the next state.
  • Moore machine: output depends only on the present state. Outputs change only on a clock edge, so they are glitch-free and stable.
  • Mealy machine: output depends on the present state AND the current input. It reacts faster but the output can glitch when inputs change.
  • State diagram / state table: a drawing (circles and arrows) or table listing every state, input, next state and output.
  • Flip-flops needed: n = ceil(log2(S)) flip-flops can represent S states (for example, 5 states need 3 flip-flops).

Formulas

  • Next-state equation: S(t+1) = f(S(t), X(t)), where X(t) is the input at time t.
  • Moore output: Z(t) = g(S(t)) — a function of the state alone.
  • Mealy output: Z(t) = g(S(t), X(t)) — a function of state and input.
  • Minimum flip-flops for S states: n = ceil(log2 S). Example: S = 6 states needs ceil(log2 6) = 3 flip-flops (since 2^2 = 4 is too few, 2^3 = 8 is enough).

These assume a synchronous (clocked) design. The formulas give the minimum hardware; one-hot encoding uses S flip-flops (one per state) and is often faster in FPGAs despite using more flip-flops.

Worked Example

Design a vending machine FSM that sells a drink for 5 rupees and accepts 1-rupee and 2-rupee coins. States track money collected: S0 (0), S1 (1), S2 (2), S3 (3), S4 (4). Now trace the coin sequence 2, 2, 1, 1:

  1. Start in S0. Insert a 2-rupee coin: 0 + 2 = 2, move to S2. No output.
  2. Insert another 2-rupee coin: 2 + 2 = 4, move to S4. No output.
  3. Insert a 1-rupee coin: 4 + 1 = 5, which reaches the price. Output = dispense drink, return to S0.
  4. Insert a 1-rupee coin: 0 + 1 = 1, move to S1. A new purchase cycle begins.

The machine dispensed exactly one drink after collecting 2 + 2 + 1 = 5 rupees, and the leftover 1 rupee correctly started the next cycle. Every transition used only the current state and the coin input, exactly as the FSM model requires.

Common Misconceptions

  • "An FSM can remember anything." Wrong — finiteness is the whole point. An FSM cannot count without limit, so it can never recognize arbitrarily long balanced brackets; that needs a pushdown automaton (a stack).
  • "Mealy machines are always better because they are faster." Not always. Mealy outputs can glitch when inputs change between clock edges, so Moore machines are preferred where a clean, stable output matters.
  • "The number of states equals the number of flip-flops." No. Flip-flops store the state in binary, so 8 states need only 3 flip-flops (2^3 = 8).

Real-World Applications

  • Serial communication receivers (UART, SPI, I2C) that step through start bit, data bits and stop bit.
  • Traffic light and elevator controllers in embedded systems.
  • Communication protocol handlers, such as the TCP connection handshake (SYN sent, established, closing).
  • Lexical analyzers in compilers, which scan source code character by character.
  • Game character AI, such as patrol, chase and attack modes of an enemy.

A finite state machine is a system with a limited set of states, transition rules driven by inputs, and Moore or Mealy style outputs. With just flip-flops for state storage and logic gates for transitions, it becomes the backbone of digital design, from a simple vending machine to the protocol engines inside your phone.