State machine diagram tool

Enter the state table of a sequential circuit (a state machine) to draw its state diagram and trace how the state and output change over a sequence of inputs. Start from examples such as “output 1 after two 1s in a row”, “detect 101” and “remainder of a binary number divided by 3”, then edit the table and watch how the behavior changes. The tool also shows state minimization, which finds and merges states that behave the same.

State table

State diagram and behavior

State minimization

The machine starts in the state on the first row. For a Moore machine, the output shown at each time step is the output of the state entered after reading the input. The red arrow in the diagram is the transition at the selected time step.

State diagrams and state tables

A sequential circuit containing flip-flops remembers past inputs as its “state” and determines the next state and the output from the current state and input. Drawing this behavior with states as circles and transitions as arrows gives a state diagram; writing it as a table gives a state table. For example, a circuit that outputs 1 after two 1s in a row uses three states to remember how many 1s have just been read in a row (zero, one, or two or more). A 0 sends it back to the first state, and a 1 advances it to the next state. Choose an example and move the time slider to see which arrow is followed for each input.

Moore and Mealy machines

A machine whose output depends only on the current state is a Moore machine; one whose output depends on both the state and the input is a Mealy machine. In a Moore machine the output is written inside the state circle, while in a Mealy machine it is written on the arrow as “input/output”. A Mealy machine's output changes as soon as the input changes, and it can often do the same job with fewer states, but noise and changes on the input pass straight through to the output. A Moore machine's output changes in step with the clock, which makes it easier to handle and safer at the cost of one clock cycle of delay. The “detect 101” example needs three states as a Mealy machine, but as a Moore machine it needs one more state meaning “101 found”, for a total of four.

State minimization

Two states that always produce the same output sequence for the same input sequence do not need to be distinguished, so they can be merged into one (equivalent states). To find them, first put states with the same output into the same group, then split groups according to whether the next states on input 0 and on input 1 fall in the same group, and repeat until the grouping no longer changes. States that cannot be reached from the initial state can also be removed. Fewer states means fewer flip-flops to hold the state and smaller logic to compute the next state. In the “table with redundant states” example, six states are reduced to three.

Turning it into a circuit: state assignment

To build a real circuit from a state table, assign a binary code to each state (state assignment). With k states, the number of flip-flops needed is log₂ k rounded up; three states need two. Next, write a truth table that gives the next-state bits and the output from the current-state bits and the input, simplify it with a Karnaugh map or similar method, and connect the results to the flip-flop inputs. One-hot encoding, which uses one flip-flop per state, needs more flip-flops but makes the next-state logic simpler, and it is common in FPGAs.

Practice logic circuit questions

Which correctly describes a sequential circuit?

  1. AIts output depends on the current inputs and a stored internal state
  2. BIts output depends only on the current inputs
  3. CIt handles only analog signals
Show answer and explanation

Answer: A. Its output depends on the current inputs and a stored internal state

A sequential circuit contains memory elements such as flip-flops, so its output depends not only on current inputs but also on past history (its internal state). Counters, registers and state machines are typical examples, able to model things like a vending machine tracking inserted coins. Circuits whose output depends only on current inputs are combinational (adders, decoders, etc.), and real digital systems alternate the two, synchronized by a clock.

Which type of sequential circuit (state machine) has outputs determined only by the current state?

  1. AMealy machine
  2. BMoore machine
  3. CHarvard machine
Show answer and explanation

Answer: B. Moore machine

In a Moore machine, outputs depend only on the current state; in a Mealy machine, they depend on both state and inputs. Moore outputs change only after a clock edge and input changes or glitches do not pass straight to them, so they are stable and predictable, but they respond a clock later and often need more states. Mealy machines need fewer states and react immediately, but input changes pass directly to the outputs. Real designs mix both styles.

What is the basic structure of a state machine written in an HDL?

  1. AOnly memory to remember the state, with no logic
  2. BOnly output logic, with no memory of state
  3. CA register holding the current state, combinational logic choosing the next state, and combinational logic producing outputs
Show answer and explanation

Answer: C. A register holding the current state, combinational logic choosing the next state, and combinational logic producing outputs

A state machine keeps the current state in a flip-flop register, determines the next state from the current state and inputs with combinational logic, and updates the state at the clock edge. Outputs are produced from the current state (and inputs) by combinational logic. Many circuits that follow a procedure, such as protocol controllers, vending machines and traffic lights, are written as state machines.

For a state machine with three states, how many flip-flops are needed to store the state (binary encoding / one-hot encoding)?

  1. A3 / 2
  2. B2 / 3
  3. C1 / 3
Show answer and explanation

Answer: B. 2 / 3

Binary encoding numbers the states in binary, so three states fit in 2 bits (two flip-flops): 00, 01 and 10. One-hot encoding uses one flip-flop per state, so three. Binary leaves the unused code 11, so it is safer to define its destination with a default branch, letting the machine recover if noise ever puts it there.

How many states does a Johnson (twisted-ring) counter made of n flip-flops go through?

  1. An
  2. B2n
  3. C2ⁿ
Show answer and explanation

Answer: B. 2n

A Johnson counter feeds the inverted last output of a shift register back to its input, cycling through 2n states with n flip-flops (with three: 000→100→110→111→011→001). Only one bit changes between neighboring states, so each state can be decoded with a two-input gate and glitches are unlikely. It is also used to generate multiple clocks with evenly staggered phases.

FAQ

Should I use a Moore machine or a Mealy machine?

Use a Moore machine if you want the output synchronized to the clock and stable; use a Mealy machine if you want fewer states or an immediate response to the input. In exams, you tell which type a given diagram is by whether the outputs are written inside the circles or on the arrows.

What is the output at time 0?

In a Moore machine, the output of the initial state is present even at time 0, before any input has been read. The “output sequence” in this tool lists only the outputs after each input is read. A Mealy machine's output is determined only once there is an input, so there is no output at time 0.

How many states can I enter?

Up to 8. There are two inputs, 0 and 1. You can give each state any name of up to 6 characters, and the names also appear in the next-state choices.

How is this tested in exams?

IT certification exams such as Japan's Fundamental and Applied Information Technology Engineer Examinations often ask you to read a state diagram or state table and give the final state or the output for a given input sequence. Entering the table and stepping through time one step at a time lets you check your own trace.

Related terms

Sequential circuit Flip-flop

Related calculators

Semiconductor, Electronics and Computing Calculators →