What modelling means in embedded systems
Modelling an embedded system means describing its structure or behaviour in a form you can reason about, simulate or test before (or alongside) building the real thing. Models range from a whiteboard state diagram to a detailed simulation of a motor, its controller and its power electronics.
What a model is
In software development, a model is a simplified representation of a system that captures the aspects you care about and leaves out the rest. A UML state diagram captures when a device changes mode, but not how its registers are configured. A Simulink model of a motor captures its electrical and mechanical dynamics, but not the colour of its casing. A model is useful when it answers a question more cheaply than building and testing the real system.
Kinds of models used in embedded design
- Structural models show the parts of a system and how they connect: block diagrams, UML class and component diagrams, SysML block definition diagrams.
- Behavioural models show how the system responds over time: state machines, sequence diagrams, activity diagrams and flowcharts.
- Dynamic or physical models describe continuous physical behaviour using equations, such as the temperature of a room or the speed of a motor. Tools like MATLAB/Simulink are widely used for these.
- Requirements models link requirements to design elements and tests, often in SysML or dedicated requirements tools.
Model-based embedded software development
Model-based design (MBD), sometimes called model-based development, makes the model the central artefact of development. Engineers design and simulate behaviour in a modelling tool, verify it against requirements, and then generate production code from the model, or hand-write code that is checked against it.
MBD is particularly common in automotive, aerospace and industrial control, where control algorithms are complex and safety standards such as ISO 26262, DO-178C and IEC 61508 demand traceability and rigorous verification.
The typical model-based workflow
- Capture requirements and link them to model elements.
- Build a model of the environment (the “plant”): the physical system the controller will interact with.
- Design the controller or logic model, often combining block diagrams for control laws with state machines for modes and fault handling.
- Simulate the controller and plant together across many scenarios, including faults and edge cases.
- Generate code from the controller model using a code generator such as Simulink Embedded Coder, dSPACE TargetLink or Ansys SCADE, or write it by hand.
- Verify the code against the model through progressively more realistic test stages.
- Integrate the code with drivers, the operating system and other software on the target.
MIL, SIL, PIL and HIL
Model-based projects typically test in stages:
- Model-in-the-loop (MIL): the controller model runs against the plant model, entirely in simulation. This validates the algorithm.
- Software-in-the-loop (SIL): the generated or hand-written code runs on a PC against the plant model. This checks that the code behaves like the model.
- Processor-in-the-loop (PIL): the code runs on the target processor or an equivalent, still against a simulated plant. This exposes compiler, word-length and timing effects.
- Hardware-in-the-loop (HIL): the real controller hardware runs against a real-time simulation of the plant, with electrical signals connected as in the real product. This tests the full embedded system without needing the real machine.
These stages are covered alongside other techniques in testing and validating embedded software.
Benefits
- Early defect detection. Simulation finds design errors before hardware exists.
- Safer testing. You can simulate dangerous conditions, such as a motor stall or a sensor failure, without risking equipment or people.
- Traceability. Requirements, model elements, code and tests can be linked, which supports certification.
- Consistency. Automatically generated code reflects the model, which can reduce manual translation errors.
- Shared understanding. Diagrams are easier for control engineers, software engineers and reviewers to discuss than raw code.
Limitations and when it’s overkill
- Tool cost. Commercial MBD toolchains can be expensive, which matters for small teams.
- Vendor lock-in. Models built in one tool don’t move easily to another.
- Model accuracy. A simulation is only as good as its plant model. If the model misses real effects such as friction, sensor noise or delays, simulated success can mislead.
- Generated code isn’t automatically right. It still needs review, testing and integration, and may need tuning for memory or speed on small MCUs.
- Skills. Teams need people who understand both the modelling tools and the underlying engineering.
For a simple product, such as a device that reads a sensor and sends the value over Bluetooth, full MBD is usually more than you need. Lightweight modelling, such as a hand-drawn state machine, often gives most of the benefit at no tool cost.
UML for embedded systems, with examples
The Unified Modeling Language (UML) is a general-purpose notation for describing software. Not every UML diagram is equally useful for embedded work; state machines and sequence diagrams tend to earn their keep. Here are concrete examples.
State machine diagram: a thermostat controller
A simple heating thermostat might have these states and transitions:
- Idle: heater off. Transition to Heating when temperature falls below the setpoint minus a hysteresis band.
- Heating: heater on. Transition to Idle when temperature rises above the setpoint plus the hysteresis band.
- Fault: heater forced off and an alarm raised. Entered from any state if the sensor reading is invalid or out of range. Leaves only after a valid reading and a user reset.
- Off: entered when the user switches the system off, from any state except Fault.
Drawing this before coding forces you to answer questions that are easy to miss in code: What happens if the sensor fails while heating? Can the user switch off during a fault? The hysteresis band avoids rapid on-off cycling, a common bug in naive implementations.
Sequence diagram: reading a sensor over I2C
A sequence diagram shows messages between parts of the system over time. For a periodic temperature read:
- A timer interrupt signals the application task that a reading is due.
- The application calls SensorDriver.startConversion().
- The sensor driver asks the I2C driver to write the command register on the sensor at its bus address.
- After the conversion time, the application calls SensorDriver.read(), which triggers an I2C read.
- The sensor returns raw bytes; the driver converts them to degrees and returns the value, or an error if the bus transaction failed.
The diagram makes timing and error paths explicit, which helps when reviewing whether the conversion delay and bus errors are handled correctly.
Class and component diagrams: structuring drivers
Class or component diagrams help structure firmware layers: an application layer depends on a TemperatureSensor interface; concrete drivers for specific sensor parts implement that interface; those drivers depend on an I2CBus interface, which a board-specific HAL implements. This layering makes it easy to swap sensors and to unit-test application logic on a PC with a fake sensor. Even in C, which has no classes, the same structure can be expressed with structs of function pointers.
SysML and other extensions
SysML extends UML for systems engineering, adding requirement diagrams and parametric diagrams that capture equations and constraints. It is used in model-based systems engineering (MBSE) to describe the whole product, including hardware, software and mechanical parts. The MARTE profile extends UML for real-time and embedded systems, adding concepts such as timing and resources.
From statechart to C code without a big toolchain
You don’t need a commercial generator to benefit from state machines. Common hand-coded patterns include:
- A
switchstatement on the current state, with events handled inside each case. - A state transition table, where rows are states, columns are events, and each cell names the next state and an action.
- Event-driven frameworks, such as the open-source QP framework, that implement hierarchical state machines and active objects for embedded C and C++.
The key discipline is keeping the code and the diagram in sync, ideally by reviewing both together whenever behaviour changes.
Embedded control system design
An embedded control system uses a processor, sensors and actuators to regulate a physical process: holding a temperature, controlling a motor’s speed, balancing a drone. Designing one combines control engineering with embedded software, and model-based tools are especially helpful here.
1. Define requirements and the plant
Specify what “good control” means: how quickly the system should respond, how much overshoot is acceptable, how accurately it must hold the target, and how it should behave during faults. Identify the plant, the physical system being controlled, and its sensors and actuators.
2. Model the plant
Build a mathematical model of the plant from physics, from measured data (system identification), or both. Even a rough first-order model with a time delay can be enough to design a reasonable controller for many thermal and mechanical systems.
3. Design the controller
Choose a control strategy. PID (proportional-integral-derivative) control is the most common starting point. More complex systems may use cascaded loops, feedforward, state-space or model-predictive control. Tune the controller in simulation against the plant model and check its response to disturbances and noise.
4. Discretise and choose a sample rate
Controllers designed in continuous time must be converted to discrete form to run on a microcontroller. The sample rate should be fast enough relative to the plant’s dynamics; a common rule of thumb is to sample many times faster than the closed-loop bandwidth, but the right rate depends on the system. Sampling jitter matters too: the control loop should run at a steady, predictable interval, usually from a hardware timer interrupt or a high-priority RTOS task.
5. Account for implementation effects
Real implementations differ from ideal models:
- Actuator saturation and integrator windup. When the actuator hits its limit, the integral term can keep growing and cause large overshoot. Anti-windup logic prevents this.
- Quantisation. ADC resolution and PWM resolution limit precision.
- Fixed-point arithmetic. On MCUs without a floating-point unit, controllers are often implemented in fixed-point, which requires careful scaling to avoid overflow and precision loss.
- Delays. Sensor filtering, computation time and communication add latency that can reduce stability margins.
- Sensor noise. Derivative terms amplify noise and usually need filtering.
6. Generate or write the code, and test it
Implement the controller, either by generating code from the model or by hand, then test progressively with SIL, PIL and HIL before trying it on the real plant. Include fault cases, such as a disconnected sensor or a stuck actuator, and define safe behaviour for each.
The control loop also has to coexist with the rest of the firmware. Timing budgets and interrupt priorities are part of the wider hardware-software co-design of the product.
Choosing tools
- For heavy control and safety-critical work: MATLAB/Simulink with Stateflow and Embedded Coder, dSPACE tools, or Ansys SCADE are common in industry.
- For open-source simulation: Scilab/Xcos, OpenModelica and Python libraries such as python-control can model and simulate many systems.
- For UML and SysML: a range of commercial and open-source modelling tools exist, from full MBSE suites to lightweight diagram-as-code tools such as PlantUML.
- For state machines in code: hand-written patterns or frameworks like QP.
As for the frequent question of C versus C++, both are widely used. Code generators commonly emit C for portability, and C++ is popular for hand-written embedded application code on 32-bit MCUs.
Summary
Modelling helps embedded teams find problems before hardware exists and communicate designs clearly. Full model-based development, with simulation, code generation and MIL/SIL/PIL/HIL testing, is most valuable for complex control and safety-critical systems. UML state machines and sequence diagrams are useful on almost any project, even without a big toolchain. And embedded control design depends on good plant models, sensible sample rates, and careful handling of saturation, fixed-point effects and delays. For where modelling fits in the bigger picture, see our introduction to embedded system design.