State Diagrams

Designing a sequential circuit starts with a picture: the state diagram. It shows every state the circuit can be in, and which input moves it from one state to another. Once the diagram is right, a fixed procedure turns it into flip-flops and gates. This lesson walks through that whole procedure on three small machines. You should know D flip-flops and the difference between Mealy and Moore machines.

Learning objectives

After this lesson you will be able to:

  • Read and draw a state diagram.
  • Turn a diagram into a state table.
  • Choose a state assignment and the number of flip-flops.
  • Derive next-state and output equations.
  • Deal with unused states.

Reading a state diagram

A state diagram uses circles for states and arrows for transitions. Each arrow is labelled with the input that causes it. In a Mealy machine the label is input / output, and in a Moore machine the output is written inside the circle. An arrow that starts and ends at the same state is a loop: the machine stays where it is for that input. One state is marked as the starting (reset) state.

From diagram to circuit: the procedure

  1. Draw the state diagram from the word description.
  2. Write the state table: present state, input, next state and output.
  3. Choose a state assignment: give each state a binary code. With S states you need n flip-flops where 2^n is at least S.
  4. Write the truth table for the flip-flop inputs (for D flip-flops, D equals the next state) and for the outputs.
  5. Simplify the equations with Boolean algebra or a Karnaugh map, and draw the circuit.

Example 1: a 2-bit counter with enable

A counter has four states, 00, 01, 10 and 11, and an input x. When x = 1 it counts up by one, and when x = 0 it stays where it is.

Present Q1Q0 x Next Q1Q0 (D1 D0)
00 0 00
00 1 01
01 0 01
01 1 10
10 0 10
10 1 11
11 0 11
11 1 00

Reading the table for each bit gives the equations: D0 = Q0 XOR x and D1 = Q1 XOR (Q0 x).

Result: two D flip-flops, two XOR gates and one AND gate implement the counter. The state diagram has four circles, each with a loop for x = 0 and an arrow to the next state for x = 1.

Example 2: a detector for two 1s in a row

A Mealy machine must output 1 when the current input and the previous input are both 1. It only needs to remember the last input, so two states are enough: S0 (the last input was 0, or nothing yet) and S1 (the last input was 1).

Present state x = 0 x = 1
S0 (Q = 0, last input was 0) S0 / 0 S1 / 0
S1 (Q = 1, last input was 1) S0 / 0 S1 / 1

With the codes S0 = 0 and S1 = 1 and a single flip-flop Q, the next state is simply the input, so D = x, and the output is z = Q x.

Result: one flip-flop and one AND gate. For the input 0, 1, 1, 0, 1, 1, 1 the outputs are 0, 0, 1, 0, 0, 1, 1.

Example 3: a simple traffic light controller

A light cycles through green (G), yellow (Y) and red (R). It stays green until a timer signal T = 1 arrives, then goes to yellow. Yellow lasts one clock cycle and then goes to red. It stays red until T = 1 and then returns to green. We use the codes G = 00, Y = 01 and R = 10.

Present state T Next state
G (00) 0 G (00)
G (00) 1 Y (01)
Y (01) 0 R (10)
Y (01) 1 R (10)
R (10) 0 R (10)
R (10) 1 G (00)

The state 11 is never used, so we treat it as a don't care when simplifying. The result is:

  • D1 = Q0 + Q1T'
  • D0 = Q1'Q0'T
  • Green = Q1'Q0', Yellow = Q0, Red = Q1

Result: the controller needs two flip-flops and a handful of gates. A check shows that if the circuit ever starts in the unused state 11, both of these equations send it to 10 (red) at the next clock edge, whatever T is, so it cannot get stuck there.

Unused states

With S states and n flip-flops you have 2^n - S unused codes. These are not impossible: noise or a bad power-up can put the circuit in one of them. Always check where an unused state goes. A self-starting design returns to a valid state by itself, as in Example 3.

Common mistakes

  • Forgetting a transition. Every state needs an arrow for every possible input value.
  • Miscounting flip-flops. Five states need 3 flip-flops, not 2.
  • Ignoring unused states. Check that they lead back into the valid cycle.
  • Mixing up Mealy and Moore labels. Outputs belong on the arrows for Mealy, and inside the circles for Moore.
  • Using next-state values as present-state inputs. The equations take the present state as input and give the next state.

Practice problems

Check your logic with the truth table generator and the K-map tool.

  1. How many flip-flops are needed for a machine with 5 states?

    Answer: 3 flip-flops, because 2^2 = 4 is too small and 2^3 = 8 is enough.

  2. Apply the input 0, 1, 1, 0, 1, 1, 1 to the "two 1s in a row" detector. What are the outputs?

    Answer: 0, 0, 1, 0, 0, 1, 1.

  3. In the traffic light controller, what is the next state of yellow (01) when T = 0?

    Answer: Red (10), because yellow lasts exactly one clock cycle whatever T is.

  4. In Example 3, what happens if the circuit starts in the unused state 11?

    Answer: At the next clock edge it moves to 10 (red) for both values of T, so it recovers by itself.

Frequently asked questions

What is the difference between a state diagram and a state table? They hold the same information. The diagram is easier for people to read, and the table is the step before writing equations, because it lists every present state and input combination.

How do I choose the state assignment? Any assignment works, but a good one gives simpler equations. Counters usually use binary counting order, and some designers use one flip-flop per state (one-hot coding), which needs more flip-flops but simpler logic.

Do I always need a Karnaugh map? For small machines Boolean algebra is enough. Karnaugh maps help when there are three or four variables, and the don't care entries from unused states make the map easier to simplify.

To explore the equations further, use the Boolean calculator.

Next lesson: State Reduction