1. State Machines in PLC Programming: Moore, Mealy, Hierarchical, SFC and Industrial Applications


0
Categories : PLC Stage Machine

State machines are one of the most powerful programming concepts used in industrial automation.

They are especially useful when a machine can exist in several clearly defined operating conditions such as:

  • Idle
  • Starting
  • Running
  • Stopping
  • Opening
  • Closing
  • Manual
  • Automatic
  • Faulted

Instead of controlling the entire machine with many unrelated bits, latches, and conditions, a state machine gives the program a structured way to answer a very important question:

What condition is the machine currently in, and what is it allowed to do next?

For simple equipment, this may seem unnecessary.

But as a machine becomes more complex, the benefits become significant.


What Is a State Machine?

A State Machine, also called a Finite State Machine or FSM, is a programming model where a system exists in one defined state at a time.

A state represents the current operating condition of the machine.

For example, an industrial warehouse door could use:

0   Idle
10  Opening
20  Fully_Open
30  Closing
40  Fully_Closed
50  Stopped
60  Faulted

The PLC always knows which operating state is active.

The machine then moves from one state to another only when specific conditions are satisfied.

These changes are called state transitions.


The Three Main Elements of a State Machine

A state machine normally consists of three fundamental elements:

  1. States
  2. Transitions
  3. Actions

1. States

A state describes the present condition of the machine.

Examples include:

Idle
Running
Opening
Closing
Waiting
Stopped
Faulted

In Studio 5000, a common method is to store the current state in a DINT.

For example:

Machine_State

The values might be:

0   Idle
10  Starting
20  Running
30  Stopping
40  Stopped
90  Faulted

Using values such as 10, 20, 30, and 40 instead of 1, 2, 3, and 4 also leaves room for future states.

For example:

20 Running
25 Paused
30 Stopping

This can make later modifications easier.


2. Transitions

A transition is the condition that allows the machine to move from one state to another.

For example:

Current State = Fully_Closed
AND
Open_Request = TRUE
AND
Open_Permissive = TRUE

Then:

MOV 10 Machine_State

The door changes to:

Opening

Conceptually:

Fully_Closed
      |
      | Open_Request
      | Open_Permissive
      v
   Opening

Once the door reaches the upper limit switch:

Opening
      |
      | Open_Limit
      v
Fully_Open

Transitions define the behavior of the machine.


3. Actions

An action is what the machine does while a particular state is active.

For example:

State = Opening

could generate:

CMD_Open

Similarly:

State = Closing

could generate:

CMD_Close

The physical output should usually still pass through additional logic such as:

Permissives
Interlocks
Fault Logic
Safety Conditions

before energizing the real output.

That separation is extremely important in industrial control systems.


Why Use State Machines in PLC Programs?

A PLC program can certainly operate without a state machine.

Many machines are programmed using traditional Ladder Logic with control bits such as:

Cycle_Active
Motor_Run
Auto_Mode
Sequence_Start
Step_Complete
Cycle_Stop
Fault_Mode
Reset_Mode

This may work well initially.

But as the machine grows, many combinations of those bits can exist.

The programmer or technician must mentally determine which combinations are valid.

For example:

Cycle_Active = TRUE
Auto_Mode = TRUE
Motor_Run = FALSE
Sequence_Start = TRUE
Fault_Mode = FALSE
Step_Complete = FALSE

What exactly is the machine doing?

That can become difficult to determine.

With a state machine:

Machine_State = 30

might immediately mean:

Closing

This can make troubleshooting much easier.


State Machines Improve Troubleshooting

From an automation technician’s perspective, state machines provide a very useful troubleshooting method.

Instead of asking:

Why isn’t the machine running?

You can ask:

What state is the machine currently in?

Then:

What transition condition is preventing it from entering the next state?

For example:

Current State:
Waiting_For_Product

The next state should be:

Transfer_Product

The transition requires:

Product_Present
AND
Downstream_Ready
AND
Safety_OK

If the machine is not advancing, troubleshooting becomes very focused.

Check:

Product_Present
Downstream_Ready
Safety_OK

One of those conditions is preventing the transition.

This is far more efficient than randomly searching hundreds of ladder rungs.


Not All State Machines Are the Same

When studying state machines, several different terms appear:

Moore Machine
Mealy Machine
Hybrid State Machine
Hierarchical State Machine
Harel Statechart
Parallel State Machines
Sequential Function Chart

These terms are related, but they are not all exactly the same type of classification.

This distinction is important.


Moore State Machine

A Moore State Machine is a model where outputs depend primarily on the current state.

Conceptually:

Output = Function of State

For example:

Machine_State = Opening

therefore:

CMD_Open = TRUE

Another example:

Machine_State = Fully_Open

therefore:

Open_Indicator = TRUE

The output is directly associated with the state.


PLC Example

A simple Ladder implementation could look conceptually like:

EQU Machine_State 10
------------------------( CMD_Open )

Where:

10 = Opening

The command exists because the state is active.

This architecture tends to be easy to read and troubleshoot.


Mealy State Machine

A Mealy State Machine allows outputs to depend on both:

Current State
+
Current Input

Conceptually:

Output = Function of State + Input

Consider an industrial door that is closing.

The state is:

Closing

While closing, the photoeye becomes blocked.

The logic could evaluate:

State = Closing
AND
PhotoEye_Blocked = TRUE

and immediately trigger a response.

For example:

Stop Closing
Initiate Reopen

The behavior depends not only on the state, but also on the current input condition.


Moore vs Mealy

The basic difference can be summarized like this:

MooreMealy
Output depends on current stateOutput depends on state and input
Very predictableVery responsive
Easy to visualizeUseful for event-driven reactions
Good for state commandsGood for transition-dependent actions

Neither architecture is automatically better.

The correct approach depends on the equipment.


Hybrid State Machines

In practical industrial automation, many PLC programs are not purely Moore or purely Mealy.

They use a combination of both.

This is often best described as a Hybrid State Machine.

For example:

State = Closing

generates:

CMD_Close

That resembles Moore behavior.

But the final command may still be immediately overridden by:

PhotoEye_Blocked
Safety_Not_OK
Motor_Overload
Interlock_Active
Emergency_Stop

The control structure could look like this:

Current State
     |
     v
State Command
     |
     v
Permissives
     |
     v
Interlocks
     |
     v
Fault Logic
     |
     v
Final Command
     |
     v
Physical Output

This structure is extremely practical in machinery control.


Hierarchical State Machines

As machines become larger, one flat state list can become difficult to manage.

Imagine a machine with the following states:

Idle
Auto_Load
Auto_Process
Auto_Unload
Manual_Jog_Motor
Manual_Jog_Valve
Manual_Jog_Conveyor
Resetting
Faulted

A hierarchical structure can organize them more clearly.

For example:

MACHINE

├── Stopped

├── Manual
│   ├── Jog_Motor
│   ├── Jog_Valve
│   └── Jog_Conveyor

├── Automatic
│   ├── Loading
│   ├── Processing
│   └── Unloading

└── Faulted

Now the machine has high-level modes and sub-states inside those modes.

This concept is closely associated with Harel Statecharts.


Harel Statecharts

Harel Statecharts extend traditional finite state machines.

They support concepts such as:

Hierarchy
Parallel States
Nested States
Events
Transitions

This is useful when a machine has several operating layers.

For example:

Machine Mode
    |
    +-- Automatic
    |      |
    |      +-- Load
    |      +-- Process
    |      +-- Unload
    |
    +-- Manual
           |
           +-- Jog Conveyor
           +-- Jog Valve

Instead of creating one enormous flat state list, the machine can be broken into logical structures.


Parallel State Machines

Large industrial systems often contain several mechanisms operating at the same time.

For example:

Conveyor State Machine

Robot State Machine

Filler State Machine

Reject Station State Machine

Each section can have its own state.

The different state machines can exchange signals such as:

Ready
Busy
Complete
Request
Permission
Faulted

For example:

Conveyor_Ready
Robot_Ready
Filler_Ready

When all required sections are ready, the system can continue.

This creates a modular architecture.


Why Multiple State Machines Can Be Better Than One

Suppose a packaging cell contains:

  • Conveyor
  • Robot
  • Wrapper
  • Reject station

One possible design is a single massive state machine:

State 10
State 20
State 30
State 40
State 50
State 60
State 70
State 80
State 90
State 100
...

That can quickly become difficult to manage.

A better design may be:

Conveyor_State
Robot_State
Wrapper_State
Reject_State

Each module owns its own behavior.

The modules then coordinate through handshake signals.

This is a common industrial design philosophy.


Sequential Function Chart

Sequential Function Chart, or SFC, is another important concept used in PLC programming.

SFC is standardized under IEC 61131-3.

It is based around:

Steps
Transitions
Actions

For example:

STEP 1
Load Product
     |
     | Product Loaded
     v

STEP 2
Process Product
     |
     | Process Complete
     v

STEP 3
Unload Product

SFC is particularly useful for processes that naturally operate as sequences.

Examples include:

Batch processing
Mixing
Filling
CIP systems
Wash cycles
Recipe execution
Packaging sequences

Is SFC the Same as a State Machine?

Not exactly.

They share many concepts.

Both use:

States or Steps
Transitions
Conditions
Actions

However, there is an important difference.

Moore and Mealy describe state machine behavior models.

SFC is primarily a PLC programming language and graphical sequence representation.

In practice, they can solve similar problems, but they come from different conceptual backgrounds.


State Machine vs Step Sequencer

Another common source of confusion is the difference between a state machine and a step sequencer.

A typical step sequencer may look like:

Step 10
   |
Step 20
   |
Step 30
   |
Step 40

The sequence normally follows a relatively linear path.

A state machine can be more flexible.

For example:

             Faulted
                ^
                |
Idle ---> Running ---> Paused
 ^          |            |
 |          v            |
 +------- Stopped <------+

The system can move between different states depending on events and operating conditions.


When Is a Step Sequencer Better?

A step sequencer can be a good solution when the process is very linear.

For example:

1. Open Valve
2. Fill Tank
3. Close Valve
4. Start Mixer
5. Mix for 60 seconds
6. Stop Mixer
7. Drain Tank

This sequence naturally progresses from one step to the next.

A state machine becomes more useful when the system can:

Pause
Resume
Stop
Restart
Recover
Fault
Return to Manual
Jump between operating modes

The more branching behavior a machine has, the more useful a state machine becomes.


Which State Machine Type Is Best?

There is no universal best architecture.

The design should match the machine.

ArchitectureTypical Application
MooreState-driven outputs and commands
MealyFast input-dependent reactions
HybridGeneral industrial machinery
HierarchicalLarge machines with modes and submodes
Parallel FSMModular machines and production cells
SFCBatch and strongly sequential processes
Step SequencerSimple linear operations

For many discrete industrial machines, a Hybrid State Machine offers an excellent balance between readability and responsiveness.


A Practical PLC Architecture

A professional state-machine PLC architecture might be organized as:

INPUT MAPPING
      |
      v
REQUESTS
      |
      v
PERMISSIVES
      |
      v
INTERLOCKS
      |
      v
STATE TRANSITIONS
      |
      v
CURRENT STATE
      |
      v
STATE COMMANDS
      |
      v
FEEDBACK / DIAGNOSTICS
      |
      v
FAULTS
      |
      v
ALARMS
      |
      v
HMI STATUS
      |
      v
OUTPUT MAPPING

This separation provides several benefits.

It makes the program easier to:

  • troubleshoot,
  • modify,
  • test,
  • document,
  • commission,
  • maintain.

Example: Industrial Warehouse Door

Consider this simplified warehouse door state machine:

0   Idle
10  Opening
20  Fully_Open
30  Closing
40  Fully_Closed
50  Stopped
60  Faulted

The state transitions might include:

Fully_Closed
+
Open_Request
+
Open_Permissive
------------------->
Opening

Then:

Opening
+
Upper_Limit_Switch
------------------->
Fully_Open

Closing could operate as:

Fully_Open
+
Close_Request
+
Close_Permissive
------------------->
Closing

A fault could trigger:

Any Operating State
+
Critical_Fault
------------------->
Faulted

The program now has a clearly defined operating model.


State Machines and Fault Handling

Fault behavior is an important part of state-machine design.

A simple approach could be:

Any State
   |
   | Critical Fault
   v
Faulted

Then:

Faulted
   |
   | Fault Reset
   v
Idle

However, industrial machines may require more sophisticated recovery.

For example:

Closing
   |
   | Fault
   v
Faulted
   |
   | Reset
   v
Stopped

The machine may intentionally return to a safe stopped condition instead of automatically resuming.

This is an important design decision.


State Machines and Safety

A state machine should never replace proper machine safety.

Safety functions such as:

Emergency Stops
Guard Doors
Light Curtains
Safety Relays
Safety PLCs
Safe Torque Off

must still be implemented according to the required safety architecture.

The state machine should respond appropriately to those safety conditions.

For example:

Safety_OK = FALSE

might inhibit motion commands or move the machine into a controlled state.

But standard PLC state logic should not be treated as the sole safety mechanism.


State Machines and HMI Design

State machines can also improve HMI diagnostics.

Instead of displaying only:

Machine Running

the HMI can display:

Current State:
Waiting for Product

or:

Current State:
Closing Door

The HMI can also display:

Next Transition Waiting For:
PhotoEye_Clear

This gives operators and technicians much more useful diagnostic information.


State Machines as a Troubleshooting Tool

One of the biggest advantages of state-machine architecture is that it changes the troubleshooting process.

Instead of asking:

Why is the output OFF?

we can troubleshoot systematically:

1. What state is active?

2. What state should be active?

3. What transition is required?

4. Which transition condition is missing?

5. Is the command generated?

6. Are permissives satisfied?

7. Is an interlock blocking the command?

8. Is feedback confirming the action?

9. Is a fault preventing the transition?

This creates a very powerful troubleshooting methodology.


Final Thoughts

State machines are much more than a programming technique.

They are a structured way to describe machine behavior.

They help answer three fundamental questions:

Where is the machine now?

What should it be doing?

What condition allows it to move next?

Different state-machine architectures solve different problems.

We explored:

Moore
Mealy
Hybrid
Hierarchical
Harel Statecharts
Parallel State Machines
Sequential Function Chart
Step Sequencers

Understanding these differences allows automation professionals to select the correct architecture instead of forcing every machine into the same programming pattern.

In many industrial applications, the most practical solution is not purely Moore or purely Mealy.

It is often a hybrid architecture where:

State
   |
   v
Normal Command

is combined with:

Permissives
Interlocks
Faults
Process Inputs
Safety Conditions

to determine the final machine behavior.

In the next article, we will go deeper into one of the most important comparisons:

Moore vs Mealy State Machines in PLC Programming

We will compare both architectures using practical industrial examples and Ladder Logic concepts.

Leave a Reply

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