Programmable control blocks
A Controller (code) block (ctrl) runs a short program at a fixed rate: it samples its analog inputs, runs your code, and drives its outputs with PWM, digital levels or analog levels. A digital power-supply loop, a motor controller or a battery charger can be simulated together with its power stage. The code is written in Rhai, a small scripting language that reads like Rust or JavaScript.
For a program that drives named pins, with GPIO, UART, I²C, SPI and CAN, use the microcontroller and its own small language instead (Programming the MCU). The control block stays the choice for fixed-rate control loops, complementary PWM with dead time, and ADC triggers.

40.1Pins and parameters
| Type | Key | Pins | Variants | Class |
|---|---|---|---|---|
ctrl (U) | library | ain0 … ain(inputs-1), out0 … out(outputs-1), gnd | none | approximate |
| Parameter | Key | Unit | Default | Range | Value comes from | What it does |
|---|---|---|---|---|---|---|
| Analog inputs | inputs | 2 | 0 to 64 (16 in the inspector) | Default, inspector | Number of ain pins. | |
| Outputs | outputs | 2 | 0 to 64 (16 in the inspector) | Default, inspector | Number of out pins. | |
| Update rate | fs | Hz | 100 kHz | > 0. Slider 10 Hz to 10 MHz. | Default, inspector | step() runs every 1/fs, the first time at t = 0. |
| ADC bits | bits | 12 | 1 to 32 (24 in the inspector) | Default, inspector | Input resolution. | |
| ADC reference | vref | V | 3.3 | > 0 | Default, inspector | Input full scale. |
| Output high | vhi | V | 3.3 | ≥ 0 | Default, inspector | High level of PWM and dout. dac() is clamped to 0 … vhi. |
| Output resistance | rout | Ω | 50 | > 0 | Default, inspector | |
| Edge time | tr | s | 10 ns | > 0 | Default, inspector | |
| Code | code | text | a PI regulator | Code tab | The Rhai script. |
Key: the name in the circuit file, in a parameter step (R1.r) and for AI agents. Range: a value outside it is refused with E_PARAM. Value comes from: where you can set it. The All parameters tab and agents can set every parameter.
Each input is high impedance (1 MΩ to the gnd pin) and is read relative to gnd. A sample is quantised: code = round(clamp(V, 0, vref) / vref · (2bits - 1)), and ain[k] is that code converted back to volts. Outputs are voltage sources referenced to the gnd pin.
40.2The Code tab
The inspector shows Parameters and Code tabs. The code editor has line numbers and syntax colours, marks error lines in red, and inserts four spaces for Tab. Changes apply when you leave the editor or press Ctrl+S. The line under the editor says No script errors, Edited · applies when you leave the editor or press Ctrl+S, or the error. Editing the code keeps the script's state, so init doesn’t run again.
40.3The functions you write
| Function | When it runs |
|---|---|
fn init(s) | Optional. Once, before the first step. s is a map that keeps its contents between calls: create fields by assigning them, s.integ = 0.0;. |
fn step(s, t, ain) | Required. Every 1/fs. t is the time in seconds, ain the array of input voltages. |
fn on_adc(s, ch, v) | When an input set up with adc_config finishes a conversion. |
fn on_comp(s, ch, high) | When a comparator set up with comp_config crosses, at the exact instant. |
fn on_edge(s, pin, rising) | On every edge of any input. Its presence makes every input also a digital input (threshold vref/2). |
fn name(s, t) | A timer callback registered with timer_every. |
Only the function parameters and s exist. An unknown variable is an error. There are no global constants: use pi().
40.4The functions you call
| Call | What it does |
|---|---|
pwm(ch, freq, duty) | Edge-aligned PWM on output ch. duty 0 to 1. A running channel takes the new values at its next period. |
timer_pwm(ch, freq, duty, opts) | A full timer. opts: complementary (a second channel with the inverted signal), deadtime (s, delays each rising edge of both), center_aligned (true or false), phase (0 to 1 of a period). |
pwm_off(ch) | Cycle-by-cycle limit: the output goes low now and stays low until the next period. |
dout(ch, b) | A digital level: true is vhi, false is 0. Stops PWM on that channel. |
dac(ch, volts) | An analog level, clamped to 0 … vhi. Stops PWM on that channel. |
timer_every(period, "name") | Calls fn name(s, t) every period seconds, at exact instants. Up to 16 timers. |
adc_config(ch, opts) | Input ch converts on its own: opts bits, fs, or trigger = "pwm0_start", "pwm0_mid" or "pwm0_end" to sample in step with a PWM channel. Calls on_adc. |
comp_config(ch, opts) | Comparator ch (0 to 15) on an input: opts ain, threshold (V, default 1), hyst (V, default 0). Calls on_comp. |
log(msg), print(msg) | A line in the Console tab. |
clamp(x, lo, hi), sat(x, lo, hi), min, max | Limits. |
pi(), sqrt, exp, ln, sin, cos, and Rhai's float methods (abs, floor, round, tan, atan, log10, to_int, to_float) | Maths. |
q15(x), q15_to_f(q), q15_mul, q15_add, q15_sub | Q15 fixed point with saturation, as in DSP firmware. |
40.5Limits and errors
Each call runs at most 100 000 operations. A script error becomes an E_SCRIPT diagnostic with the line and column, for example "U3 line 4 col 18: Variable not found: erro", and stops that block until the code is fixed. A missing step function, an output channel that doesn’t exist and an unknown trigger are reported the same way. The console keeps the last 5000 lines.
40.6The default program
fn init(s) {
s.integ = 0.0;
}
fn step(s, t, ain) {
let err = 5.0 - ain[0] * 6.0; // output divided by 6 on ain0
s.integ = clamp(s.integ + 0.002 * err, 0.0, 0.95);
let duty = clamp(s.integ + 0.05 * err, 0.0, 0.95);
pwm(0, 200e3, duty); // 200 kHz PWM on out0
dout(1, err > 0.5); // out1 high while regulating up
}
- Open Digital control, the digital PI buck.
- Select the controller, open the Code tab and add
timer_every(1e-3, "report");to init, and the functionfn report(s, t) { log("integ=" + s.integ); }. - Press Ctrl+S and open the Console tab.
You should seeA line per millisecond shows the integrator settling as the output reaches 5 V.
The browser's fallback engine runs a subset of Rhai without adc_config, comp_config and timer_every. The WebAssembly engine, which is normally used, runs all of it.