State Diagram

What Are State Diagrams?

A state diagram or a state transition diagram is a graphical representation of a state machine‘s finite number of states, state transitions, and rules that govern the transitions. State diagrams are used as high-level starting points for complex software design because their simplicity allows clear communication of different modes of operation.

In a state diagram:

  • Boxes represent states or different modes of operation
  • Arrows indicate the transition from one state to another
  • Text associated with each arrow represents the rule that governs the transition
Figure 1: State diagram of a microwave oven with 3 modes of operation (off, heating and idling), modeled using Stateflow.

Figure 1. State diagram of a microwave oven with 3 modes of operation (off, heating, and idling), modeled using Stateflow.

A state machine’s outputs or actions are descriptions of the behavior of each system state. Two types of state machines—Moore and Mealy—are classified by where the machine output is defined.

Moore Implementation of a State Diagram

In this type of state machine, the outputs depend only on the state of the system and are defined as state actions. Regardless of how you enter a state, the state action remains the same. For example, in the state diagram in Figure 2, the output from Heating remains the same regardless of whether the transition occurs from the Idling or OFF state.

Figure 2: State diagram modeled using Moore Implementation with Stateflow.

Figure 2. State diagram modeled using Moore implementation with Stateflow.

Mealy Implementation of a State Diagram

In this type of state machine, the outputs depend not only on the state of the system, but also on inputs to the system. As shown in the state diagram in Figure 3, outputs in a Mealy implementation are defined at the transitions.

Figure 3: State diagram modeled using Mealy implementation with Stateflow.

Figure 3. State diagram modeled using Mealy implementation with Stateflow.

With the Mealy implementation, state diagrams can be reorganized and simplified with the addition of loops to update machine outputs. This is increasingly beneficial with more complex designs.

Figure 4: Reorganized state diagram modeled using Mealy implementation with Stateflow.

Figure 4. Reorganized state diagram modeled using Mealy implementation with Stateflow.

Both Mealy and Moore machines are popular for their simplicity and clarity, and the two styles are often mixed within the same state diagram.

State Charts: Enhanced State Diagrams

The basic building blocks of state diagrams are not enough for a single diagram to represent complex logical systems. You will need additional capabilities to capture intricate design details efficiently. State charts, also known as Harel charts, add capabilities including hierarchy, parallelism or orthogonality, and event broadcasting.

The hierarchy capability allows further design compartmentalization and can reduce the number of transition lines required between states. Parent states can be introduced to create hierarchy within state machines. For example, in the state chart in Figure 5, the parent state of Baking includes the substates of Heating and Idle and the associated state diagram.

Figure 5: State chart showing  hierarchy, modeled using Stateflow.

Figure 5. State chart showing hierarchy, modeled using Stateflow.

Parallelism or orthogonality capabilities allow for a single state chart to include multiple states operating simultaneously. For example, in the state chart in Figure 6, Oven and Oven_Light can be thought of as two independent state machines operating simultaneously. The chart represents the overall system in a single graphic containing two parallel state machines.

Figure 6: State chart showing hierarchy and parallelism, modeled using Stateflow.

Figure 6. State chart showing hierarchy and parallelism, modeled using Stateflow.

Event broadcasting capabilities expand the functionality of the system by allowing information exchange between two independent states or state machines. In the previous case, the two state machines were independent, as the oven light’s operation did not depend on the oven’s heating system. With event broadcasting, a feature that turns off the oven light when baking is complete can be added using a simple command within the Oven state’s logic, as highlighted in the state chart in Figure 7.

Figure 7: State chart showing hierarchy, parallelism, and broadcasting, modeled using Stateflow.

Figure 7. State chart showing hierarchy, parallelism, and broadcasting, modeled using Stateflow.

State charts with hierarchy, parallelism, and broadcasting capabilities help to represent complex system functionalities without cluttered state diagrams.

State Diagrams and State Charts with Stateflow

Stateflow® is a graphical programming environment based on finite state machines. Using Stateflow, you can start from simple state diagrams and build out state charts to model complex logic in dynamic systems, including automatic transmissions, robotic systems, mobile phones, and more. Applications of this complex logic include:

If you need the logic created to control a software component, you can use the automatic code generation capabilities from Simulink to convert the state chart into C, HDL, or PLC code for deployment.

For more information on modeling state diagrams, including these advanced techniques, see Stateflow and Simulink. The example, Modeling a Security System, demonstrates hierarchy, parallelism, and event broadcasting in Stateflow.

See also: control logic, state machine, control systems, embedded systems