Exporting Code to a Microcontroller

BlockWerk turns a tested diagram into standalone C++, Rust or Python, with an initialize() and a step() you call from your own firmware. This guide shows how to export just the controller and run it on a board such as an Arduino, ESP32 or STM32.

What You Get

  • C++: a .hpp/.cpp pair with an Inputs struct, an Outputs struct and a model class with initialize() and step(inputs, outputs), plus plain C functions for static allocation (no heap) and an arduino_example.ino with the timing loop already written.
  • Rust: the same shape: new(sample_time), reset() and step(&inputs, &mut outputs).
  • Python: a model class for testing and analysis on a PC.

The exported code computes the same numbers as the simulation on the canvas.

Step by Step: Export Only the Controller

  1. Put the controller in a Subsystem. Select the controller blocks and group them into a Subsystem (see Working with Subsystems).
  2. Define its inputs and outputs. Inside the Subsystem, add one InPort per signal your firmware supplies (for example setpoint and speed) and one OutPort per command (for example duty). The port names become the field names in the generated code.
  3. Keep the test setup outside. Leave the setpoint sources and the plant model on the main canvas, wired to the Subsystem, so you can test the loop in simulation first.
  4. Open Export Embedded Code and set Diagram Scope to your Subsystem. Only the controller is exported; the plant and the test sources are left out.
  5. Choose the Embedded Hardware profile. It selects 32-bit float, which suits microcontrollers with a single-precision FPU (ESP32-S3, Cortex-M4F).
  6. Set the sample time to your loop period, for example 0.002 s for a 500 Hz loop, and pick C++ or Rust.

Running It in Your Firmware

Call step() once per sample period, from a hardware timer or a fixed-rate task:

MyController ctrl(0.002);
MyControllerInputs in{};
MyControllerOutputs out{};

// setup:
ctrl.initialize();

// every 2 ms:
in.setpoint = readSetpoint();
in.speed = readEncoder();
ctrl.step(in, out);
writePwm(out.duty);

The code is exact for the sample time it was exported with. Export again when you change the loop rate.

Script Blocks in the Export

  • A JavaScript Script block is translated to C++, Rust and Python, and runs once per step as on the canvas. Numbers, let/const/var, if/else, loops, arithmetic, comparisons, logic and Math.* are translated.
  • Text, arrays and Math.random are not. The export then shows a warning saying why, and that script's outputs are 0.
  • A Python Script block runs in the Python export only; in C++ and Rust its outputs are 0. Write the script in JavaScript to export it to a microcontroller.

Tips

  • Standard blocks are checked number for number against the simulation engine: a solid choice for the core of a controller. Use a script for logic that is easier to write as code.
  • Read the warnings in the export dialog: they name every block that exports differently from the canvas.
  • Want a ready-made starting point? Open the example Embedded Controller → C++ / Rust from the Examples menu.

See Also

  • Subsystem: Groups blocks into one component with its own inputs and outputs.
  • InPort: Defines an input port of a subsystem.
  • OutPort: Defines an output port of a subsystem.
  • Script: Runs your own JavaScript or Python at every step.

Simulate in BlockWerk

Interactive sandbox directly in your browser — zero installation, WASM-powered.

Simulate in BlockWerk →