What Is a State Machine?

Oftentimes when we are creating electronics projects, we are trying to emulate or create a series of events. Most of the time the order of events we want to happen, and when they happen depends on what is currently happening and a series of external factors. For example, James Colvin wrote a Simon Says game. In Simon Says, you either go on to the next challenge or lose depending on if you do the action correctly and which action was previous.

This is sequential logic, and an example of that is called a state machine. If you don’t understand sequential logic, read this post. State machines are an important building block in FPGA design. In a state machine, you have your state register, which stores what the current state is, that would be the current event in my previous example. If you don’t know what a register is, read this post about registers. Feeding into the state register is the next state logic, and this logic dictates the next state based on the present state and inputs. Out of the state register comes the present state which also leads to the output logic. This decides what signals have what value depending on the present state. You can see the block diagram for a state machine below. A block diagram is a visual tool to divide code and ideas into chunks, much like a flow chart.

The basic block diagram for a state machine. You can see that the next state depends on the present state and inputs, and that the output is defined by the present state and some logic.

The next state logic can be represented with something called a state diagram. For an example, I have included my state diagram for the stepper motor controller for the claw machine I’m making. The claw machine is a classic game in arcades where you control a claw to attempt to get a prize out of the box.


The claw machine. Image from http://www.bmigaming.com/Images/smart-prizetimecraneclaw-2player-60.jpg.

Below you can see the state diagram for he stepper motor controller. The blue circles are the states named for which of the 4 signals on the stepper motor they set high. The two-color blue circle is the start state. This is the state that the reset signal goes to. The system will automatically start in this position. The purple text by each state are the output signals. The text on the arrows show which signal causes the machine to go in that direction.

Stepper motor controller state diagram.
Stepper motor controller state diagram.

To demonstrate how the state diagram works, I will trace a path. The path I am going to trace is in green dotted lines in the state diagram below.

State diagram with the path in green dotted lines. The path is described below.
State diagram with the path in green dotted lines. The path is described below.

Starting at the state sig0, the motor will be off, as enable is 0 to get to that state. Then if we turn enable on we will follow the green dotted arrow to sig1. In sig1 the motor will take a full step as one of the pins is now high. Then if we leave enable on and make sure direction is 1 we will move to sig2 where the next pin will be high. This will take another step in the same direction. Then if enable and direction are kept the same we move to sig3, and then sig4. The direction it steps depends on the orientation of the pins. Just know that going this direction in the state digram is one direction of steps, and going the other direction is another direction in steps. Then if we turn enable off we will move back to sig0 where the motor will turn off.

Now that you understand the concept behind state machines, stay tuned to hear about how to code a state machine.

For more information on state diagrams, visit the Learn site page.

To learn how to use a stepper motor with FPGA using this state diagram, check out my instructable.


Be the 1st to vote.

4 Comments on “What Is a State Machine?”

  1. State machines are a great concept because they clarify the flow of the program. There is also less coding bugs thanks to automatic code generators where you draw the state diagram

    1. Hi Wojtekss,
      I had no idea there were automatic code generators, that could certainly come in handy. I find even without that though, having a standard structure to code helps minimize errors.

Leave a Reply

Your email address will not be published. Required fields are marked *