76  State Machine Fundamentals for IoT

In 60 Seconds

Finite state machines model IoT device behavior as discrete states (idle, sensing, transmitting, sleeping) with defined transitions triggered by events or conditions. Moore machines produce output based on current state only; Mealy machines also consider the triggering event – Moore is simpler to debug, Mealy is more responsive.

76.1 Learning Objectives

By the end of this chapter, you will be able to:

  • Explain Finite State Machines: Describe FSM components (states, transitions, events, guards, actions) and their role in structuring IoT device behavior
  • Compare State Machine Types: Differentiate Moore, Mealy, and Hierarchical state machines and select the appropriate type for specific IoT applications
  • Design State Transitions: Construct transition rules with explicit triggers, guard conditions, and entry/exit actions for embedded systems
  • Apply Anti-Pattern Awareness: Identify and avoid common state machine mistakes including state explosion, missing transitions, and blocking operations

A state machine is like a set of rules that tells a robot what to do next based on what just happened - like a choose-your-own-adventure book!

76.1.1 The Sensor Squad Adventure: The Traffic Light Challenge

Blinky the LED had a very important job - controlling the traffic light at the busiest intersection in Sensor City! But there was a problem. Blinky kept forgetting what color to show next.

“I know!” said Signal Sam. “You need a STATE MACHINE! It is like a rulebook that always tells you what to do next.”

Sam helped Blinky create simple rules:

  1. GREEN state: Let cars go for 30 seconds, then switch to YELLOW
  2. YELLOW state: Warn everyone for 5 seconds, then switch to RED
  3. RED state: Stop cars for 30 seconds, then switch to GREEN

Now Blinky never got confused! The state machine always knew: “I am in the GREEN state. When my timer ends, I go to YELLOW. No other choices!”

But then something exciting happened. A button was added for pedestrians! Now Blinky’s state machine got smarter:

  • If in GREEN and button pressed: Remember the request, finish the timer, then go to YELLOW
  • Add WALK state: After RED, show the walking person for 15 seconds
  • Back to GREEN: After WALK ends, return to normal traffic

The Sensor Squad learned that state machines are perfect for anything that follows rules - traffic lights, door locks, washing machines, and even robots!

76.1.2 Key Words for Kids

Word What It Means
State The current situation or mode something is in (like “sleeping” or “awake”)
Transition Moving from one state to another (like waking up from sleep)
Event Something that happens that might cause a change (like an alarm ringing)
Guard A rule that must be true before you can change states (like “only wake up if alarm is set”)

76.1.3 Try This at Home!

Design Your Own State Machine for Getting Ready for School!

  1. Draw circles for each STATE: SLEEPING, AWAKE, EATING, DRESSED, READY
  2. Draw arrows between states and label what EVENTS cause each change
  3. Add GUARDS (rules): “Can only go to DRESSED if clothes are clean”
  4. Follow your state machine tomorrow morning!

This is exactly how engineers program smart devices - they plan all the states and rules before writing any code!

What is a State Machine?

A state machine is a model that describes something that can only be in ONE state at a time, with clear rules for moving between states. Your phone is a perfect example:

  • LOCKED state: Screen off, waiting for input
  • UNLOCKING state: Checking fingerprint or PIN
  • UNLOCKED state: Full access to apps
  • LOW_BATTERY state: Limiting functions to save power

Why IoT Devices Need State Machines

IoT devices must handle many situations reliably:

Without State Machine With State Machine
Code becomes spaghetti with nested if/else Clean, organized logic
Hard to add new features Easy to add new states
Difficult to debug Visual diagram shows all paths
Race conditions and bugs Predictable behavior

Key Terms

Term Definition
State A distinct mode of operation (e.g., IDLE, ACTIVE, ERROR)
Transition Movement from one state to another
Event An input that may trigger a transition
Guard A condition that must be true for a transition to occur
Action Code that runs during a transition or while in a state
Hierarchical FSM States containing sub-states for complex behaviors

76.2 Prerequisites

Before diving into this chapter, you should be familiar with:

Key Concepts

  • Finite State Machine (FSM): A computational model describing a system as a finite set of states, transitions triggered by events, and actions executed on state entry/exit — the primary design pattern for IoT device firmware behavior
  • State: A configuration of an IoT device that persists until an event triggers a transition — examples: IDLE, CONNECTING, SENSING, TRANSMITTING, ERROR, SLEEP each with distinct behavior and resource usage
  • Transition: A change from one state to another triggered by a specific event or condition — represented as arrows in state diagrams with labeled trigger conditions and optional guard conditions
  • Event: An external stimulus or internal timer expiration that causes a state machine to evaluate transition conditions — examples: sensor reading ready, button pressed, timeout expired, MQTT message received
  • Guard Condition: A boolean predicate evaluated when an event occurs, determining whether the associated transition should fire — e.g., “if battery > 20% THEN transition to TRANSMIT, ELSE transition to LOW_POWER”
  • Moore Machine: An FSM variant where outputs depend only on the current state (not on the input event), simplifying reasoning about device behavior at each state regardless of how it was entered
  • Mealy Machine: An FSM variant where outputs depend on both the current state and the triggering input event, enabling more expressive behavior with fewer states at the cost of harder analysis

76.3 Introduction to State Machines

Time: ~10 min | Difficulty: Intermediate | Unit: P04.FSM.U01

State machines are one of the most powerful design patterns in embedded systems and IoT development. They provide a structured way to model complex device behavior that responds to events, handles errors gracefully, and maintains predictable operation even in challenging conditions.

76.3.1 Why State Machines Matter in IoT

IoT devices face unique challenges that make state machines essential:

  1. Constrained Resources: Limited memory means we cannot keep track of extensive history; states encapsulate “what matters now”
  2. Unreliable Networks: Devices must handle disconnection, reconnection, and partial failures gracefully
  3. Power Management: Sleep states, wake events, and power transitions are naturally modeled as states
  4. Safety-Critical Operations: Door locks, medical devices, and industrial controls require provably correct behavior
  5. Long-Running Operations: Devices may run for years; state machines prevent logic drift and memory leaks

76.3.2 Types of State Machines

Comparison diagram of Moore, Mealy, and Hierarchical state machine types showing how each determines output differently and their typical IoT applications

Type Output Determined By Best For IoT Example
Moore Machine Current state only Simple, predictable devices LED status indicator
Mealy Machine State + current input Responsive systems Button debouncing
Hierarchical (HSM) Nested state context Complex behaviors Smart thermostat

Consider a sensor with 3 operating modes and 5 possible events. A Mealy machine requires checking each state-event combination:

\[\text{Mealy Transitions} = \text{States} \times \text{Events} = 3 \times 5 = 15 \text{ possible transitions}\]

A Moore machine producing 2 distinct outputs per state needs to split states by output, requiring 4 states instead of 3:

\[\text{Moore Transitions} = 4 \times 5 = 20 \text{ possible transitions}\]

Moore machines use ~33% more states but simplify debugging because output depends only on current state, not triggering event. For safety-critical IoT (medical devices, industrial control), the verification advantage outweighs state count.

76.3.3 A Simple IoT State Machine Example

Consider a smart door lock with these states:

State diagram for a smart door lock showing LOCKED, UNLOCKING, UNLOCKED, and FAULT states with transitions triggered by valid code entry, motor completion, timeout, and motor stall events

Key observations:

  • Each state has a clear purpose and distinct behavior (LED output)
  • Transitions have explicit triggers (events) and conditions (guards)
  • Error handling is explicit, not hidden in exception handlers
  • The diagram IS the documentation

76.4 Core State Machine Concepts

76.4.1 States

A state represents a distinct mode of operation where the device behaves consistently. Good states have:

  • Clear identity: Named for what the device is doing (IDLE, SAMPLING, TRANSMITTING)
  • Single responsibility: One primary behavior per state
  • Observable output: External indication of current state (LED, message, behavior)

76.4.2 Transitions

Transitions are the arrows between states. Each transition specifies:

  • Source state: Where the transition starts
  • Target state: Where the transition ends
  • Trigger event: What causes the transition (button press, timer, message)
  • Guard condition: Optional boolean that must be true for transition to occur
  • Action: Optional code to execute during the transition

76.4.3 Events

Events are inputs to the state machine that may cause transitions:

  • External events: Button presses, sensor readings, network messages
  • Internal events: Timer expirations, computation complete, error detection
  • Synthetic events: Generated by the state machine itself for sequencing

76.4.4 Guard Conditions

Guards filter transitions based on runtime conditions:

transition: IDLE -> ACTIVE
  trigger: start_button
  guard: battery_level > 20%
  action: log("Starting operation")

Without the guard, a low-battery device might start an operation it cannot complete.

76.4.5 Entry and Exit Actions

Actions that run automatically when entering or leaving a state:

  • Entry action: Initialize resources, start timers, set outputs
  • Exit action: Clean up resources, stop timers, save state

76.5 State Machine Anti-Patterns

Common Mistakes to Avoid
  1. State explosion: Too many states make the machine unmaintainable. Use hierarchical states instead.

  2. Missing transitions: Every state should handle every possible event, even if just to ignore it.

  3. Hidden state: Avoid storing state information outside the FSM context. All state should be explicit.

  4. Transition side effects: Keep entry/exit actions simple. Complex logic should be in state handlers.

  5. Blocking operations: Never block in state handlers. Use sub-states for long operations.

76.6 Worked Example: IoT Sensor State Machine in Python

This Python implementation demonstrates a practical state machine for an ESP32-like temperature sensor that manages power modes, sampling, and error recovery.

from enum import Enum, auto

class State(Enum):
    DEEP_SLEEP = auto()
    WAKE_UP    = auto()
    SAMPLING   = auto()
    TRANSMIT   = auto()
    ERROR      = auto()

class SensorFSM:
    """FSM for battery-powered IoT temperature sensor."""
    def __init__(self, max_retries=3):
        self.state = State.DEEP_SLEEP
        self.retries = 0
        self.max_retries = max_retries

    def transition(self, event: str):
        old = self.state
        if self.state == State.DEEP_SLEEP and event == "timer_expired":
            self.state = State.WAKE_UP
        elif self.state == State.WAKE_UP and event == "peripherals_ready":
            self.state = State.SAMPLING
        elif self.state == State.SAMPLING and event == "reading_valid":
            self.state = State.TRANSMIT
        elif self.state == State.TRANSMIT and event == "ack_received":
            self.retries = 0
            self.state = State.DEEP_SLEEP
        elif self.state == State.TRANSMIT and event == "timeout":
            self.retries += 1
            if self.retries >= self.max_retries:
                self.state = State.ERROR
        elif self.state == State.ERROR and event == "reset":
            self.retries = 0
            self.state = State.DEEP_SLEEP
        if self.state != old:
            print(f"  [{old.name}] --{event}--> [{self.state.name}]")

fsm = SensorFSM()
for event in ["timer_expired", "peripherals_ready",
               "reading_valid", "ack_received"]:
    fsm.transition(event)
# [DEEP_SLEEP] --timer_expired-->   [WAKE_UP]
# [WAKE_UP]    --peripherals_ready--> [SAMPLING]
# [SAMPLING]   --reading_valid-->   [TRANSMIT]
# [TRANSMIT]   --ack_received-->    [DEEP_SLEEP]

What to observe:

  • Each state has a single responsibility (sleep, sample, transmit)
  • Entry actions initialize resources and set hardware modes
  • Guards (retry count check) prevent infinite retransmission loops
  • The ERROR state provides a recovery path back to DEEP_SLEEP
  • Power consumption varies by state: 10 uA (sleep) to 80 mA (transmit)

Power budget calculation for this state machine:

State Duration Current Energy per Cycle
DEEP_SLEEP 300 s 10 uA 3,000 uAs
WAKE_UP 50 ms 5 mA 0.25 uAs
SAMPLING 10 ms 12 mA 0.12 uAs
TRANSMIT 200 ms 80 mA 16 uAs
Total per cycle 300.26 s - 3,016 uAs

Average current: 3,016 / 300.26 = 10.04 uA – the sleep state dominates. A 220 mAh CR2032 coin cell lasts 220,000 / 10.04 = 21,912 hours (2.5 years).

76.6.1 Interactive: IoT Sensor Power Budget Calculator

Adjust the state machine timing and current draw to estimate battery life for your sensor design.

76.7 Real-World Deployment: Zigbee Door Lock State Machine

Commercial Zigbee door locks (Yale, August, Schlage) use hierarchical state machines with 8-12 states. A simplified version of the critical path:

LOCKED -> [valid_code] -> UNLOCKING -> [motor_done] -> UNLOCKED
  |                          |                           |
  |<-- [timeout 30s] -----+---- [auto_relock] ----------+
  |                        |
  +<-- [motor_stall] -> FAULT -> [manual_override] -> LOCKED

Why state machines matter here: If the motor stalls during unlocking, the lock must NOT remain in an undefined state. The FAULT state explicitly handles the motor stall, alerts the user via BLE notification, and allows manual key override. Without an explicit state machine, a stall could leave the firmware in a half-locked state with no recovery path – a security and safety hazard.

Key design choice: Yale uses a Moore machine (output depends only on state) for the lock mechanism because it is deterministic and auditable. The LED always shows the same color for LOCKED regardless of what triggered the lock – making the system predictable and easier to certify for UL 294 (security standard).

Place these state machine steps in the correct order for a battery-powered IoT sensor duty cycle.

Common Pitfalls

Creating an FSM where some states have no defined behavior for certain events. When an undefined event arrives in a state without a handler, the system behavior is undefined — typically a hang or crash. Always define an explicit transition (even if it is a self-loop with no action) for every event in every state.

Over-decomposing IoT device behavior into dozens of micro-states where a few well-designed states would suffice. A 30-state FSM for a simple temperature sensor is unmaintainable and error-prone. Combine states that differ only in timing, not in fundamental behavior.

Designing a single large FSM when the device has genuinely independent subsystems (radio state machine, sensor state machine, power management state machine). Concurrent FSMs running in parallel are easier to reason about than a single monolithic FSM combining all behaviors.

Writing firmware that implements an FSM without first drawing the state diagram. Undocumented FSMs accumulate implicit states and transitions over time that are not visible in the code, making debugging and modification increasingly difficult.

76.8 Summary

State machines provide a powerful framework for designing reliable IoT device behavior:

  • States represent distinct modes of operation with clear behaviors
  • Transitions define how the device moves between states based on events
  • Guards add conditional logic to transitions
  • Actions execute code during transitions or while in states
  • Hierarchical states manage complexity through nesting
  • Power management maps naturally to states (sleep, wake, active, transmit)

The visual nature of state machines makes them ideal for documentation, verification, and communication with team members.

Key Takeaway

In one sentence: Finite state machines model IoT device behavior as discrete states with defined transitions, making complex device logic explicit, verifiable, and maintainable compared to nested if/else approaches.

Remember this rule: Moore machines output based on state only (simpler to debug), Mealy machines output based on state plus input (more responsive) – choose Moore for safety-critical systems and Mealy for interactive devices.

76.9 Knowledge Check

76.10 What’s Next

If you want to… Read this
Build an ESP32 state machine lab ESP32 State Machine Lab
Apply state machines to IoT system design State Machines in IoT
Study process control and PID Process Control and PID
Explore specialized IoT architecture Specialized Architecture
Learn about IoT reference architectures IoT Reference Architectures