State Machine

What Is a State Machine?

A state machine (or finite state machine) is a representation of an event-driven, reactive system that transitions from one state to another if the condition that controls the change is met. Most state machines are expressed using diagrams, but they can also be represented via text.

Three basic components are needed to build a state machine:

  • States: Different steps, tasks, or modes of operation that distinguish the process
  • Transitions: Methods of moving between different states
  • Actions: Events or changes that occur throughout the execution of a state machine

Basic components of a state machine can be observed in an automatic gear transmission system modeled using Stateflow. (See example.)

Early state machines were used by mathematicians and computer scientists to describe computing systems. They simplified complex logic into a more manageable, graphic representation. Compared with text alternatives, the visual nature of a state machine makes it more apparent how different system states relate to one another and what conditions result in a transition or action. Over time, the uses for state machines have expanded to include modeling complex logic in dynamic systems such as aircraft, automobiles, robots, and mobile phones.

Examples of operations containing complex logic include:

  • Scheduling a sequence of tasks or steps for a system
  • Defining fault detection, isolation, and recovery logic
  • Supervising how to switch between different modes of operation

You can use a state machine to represent a simplified version of a car’s automatic gear transmission. The example below has four operating states, labeled first, second, third, and fourth. Like the gears they represent, the states in the state machine are exclusive, meaning only one state is active at a time. This state machine monitors the speed of the car and transitions gears when the speed threshold for the operating gear is met.

A state machine of an automatic transmission system in Stateflow transitions between gears when a speed threshold is reached. (See example.)

Mealy and Moore State Machines

There are two common standards to consider when designing a state machine:

  • Mealy: Outputs are a function of state and inputs, represented by defining outputs in transitions.
  • Moore: Outputs depend only on the state, represented by defining outputs in the states themselves.
A state machine diagram highlighting input states of receiving a nickel, dime, or no coin and the corresponding outputs.

A state machine of a vending machine modeled using Mealy semantics defines whether a soda is dispensed during transitions. (See Stateflow example.)

A state machine diagram showing five states for a traffic light facing two directions.

A state machine of a traffic light modeled using Moore semantics defines outputs within the state for the traffic signal color. (See Stateflow example.)

To learn more about these semantics and when to implement them, see Overview of Mealy and Moore Machines.

Harel Concepts with State Machines

As the complexity of software components evolved, the basic building blocks of state machines needed to as well. In the 1980s, Dr. David Harel identified additional capabilities required to accurately reflect more intricate details of systems:

  • Hierarchy introduces a parent state (or states) to further structure and compartmentalize the design.
  • Parallelism or orthogonality allows for a single diagram to include multiple states operating simultaneously.
  • Event broadcasting enables information exchange between two independent states or state machines.

When these capabilities are coupled with state machines, the representations are called Harel state charts.

Elements of a Harel state chart are needed to define the added complexity of a security system. (See example.)

State Machines with Stateflow

Stateflow® is a graphical programming environment based on finite state machines. You can start from simple state diagrams and build out state machines to model complex logic in dynamic systems. You can design state machines as well as state transition diagrams, flow charts, state transition tables, and truth tables. This approach can help you when developing applications for supervisory control, task scheduling, fault management, communication protocols, user interfaces, and hybrid systems.

State machines in Stateflow can be simulated to evaluate how they perform under different conditions. For example, a state machine modeling an elevator could be simulated to see how it responds when different buttons are pressed, or a fault occurs. By simulating the state machine, you can ensure the logic behaves as expected, evaluate whether conditions are missing, and test different scenarios.

To learn more about modeling state machines, see Stateflow and Simulink®. To get started creating state machines in Stateflow, see Model Finite State Machines.


See also: control logic, state diagram, control systems, embedded systems, FDIR, Model-Based Design, model-based testing, PackML, modeling and simulation, simulation software, model-based systems engineering (MBSE)