# Comparator (`comparator`)

> Outputs 1 while in1 is above in2, otherwise 0.

![comparator](https://voltecx-web.nguyenvanqui291.workers.dev/docs/reference/img/comparator.svg)

[Component reference](https://voltecx-web.nguyenvanqui291.workers.dev/docs/reference/) · [繁體中文](https://voltecx-web.nguyenvanqui291.workers.dev/zh/docs/reference/control/comparator/)

**Group:** Control · **Kind:** `{"block":"comparator","kind":"control"}`

## When to use it

Use it for a trip level, a threshold, or a PWM gate made from a reference and a Carrier. Use PWM Generator for the standard gate.

## Pins

| Pin | Domain | Meaning |
|---|---|---|
| `in1` | signal | Larger side: reference or modulation |
| `in2` | signal | Smaller side: carrier or threshold |
| `out` | signal | 1 while in1 is above in2 |

## Common parameters

- `hys`: Hysteresis: out turns 1 above in2 + hys and 0 below in2 − hys and holds in between; 0 = plain in1 > in2 (Default: 0)

## Parameters

| Key | Unit | Default | Typical range | Description |
|---|---|---|---|---|
| `hys` | — | 0 |  | Hysteresis: out turns 1 above in2 + hys and 0 below in2 − hys and holds in between; 0 = plain in1 > in2 |

_Parameter descriptions are in English in both languages._

**Use for:** Overcurrent trips · Carrier-based PWM · Level detection

**Not for:** Custom on and off levels → [`hysteresis`](https://voltecx-web.nguyenvanqui291.workers.dev/docs/reference/control/hysteresis/) · Exact PWM edges → [`pwm`](https://voltecx-web.nguyenvanqui291.workers.dev/docs/reference/control/pwm/)

## Formulas

| Formula | Equation | Source |
|---|---|---|
| Turn-on level | von = in2 + hys | Sedra & Smith, Microelectronic Circuits, 7th ed. (2015), bistable multivibrator (Schmitt trigger) section |
| Turn-off level | voff = in2 - hys | Sedra & Smith, Microelectronic Circuits, 7th ed. (2015), bistable multivibrator (Schmitt trigger) section |
| Carrier comparison | out = 1 when in1 > in2 + hys (from off) | Holmes & Lipo, Pulse Width Modulation for Power Converters (2003), ch. 3 |

## Quick check

Inputs:

- `in1`: Signal = 10.5 —
- `in2`: Threshold = 10 —
- `hys`: Hysteresis half-width = 200m —, `hys`

Outputs:

- `von`: Turn-on level = `in2 + hys` —
- `voff`: Turn-off level = `in2 - hys` —
- `out`: Output from the off state = `if(in1 > in2 + hys, 1, 0)` —

## Model logic

- With hys = 0 the output is 1 while in1 > in2 and 0 otherwise, so equal inputs give 0; it has no state and no delay.
- With hys > 0 it turns 1 above in2 + hys, turns 0 below in2 - hys and holds in between, starting at 0; the band is 2·hys wide.
- The solver does not land a step on a crossing, so the flip shows at the end of the step; only a Transport Delay reading it gets the interpolated crossing time.
- The output levels are only 0 and 1, propagation delay is not modelled, and a negative or non-finite hys is refused.

## Model

The output is `out = 1` if `in1 > in2`, else 0. There is no delay. With `hys` = 0 (the default) there is no hysteresis and equal inputs give 0. With `hys` > 0 the output keeps a state: it turns 1 when `in1 > in2 + hys`, turns 0 when `in1 < in2 - hys` and holds in between, starting at 0. The block does not predict crossing times, so the solver does not land a step exactly on a crossing.

## Common mistakes

- With a noisy or slow input the output can chatter near the threshold, and the switching time is only as exact as the step. Use a small maximum step, or PWM Generator, which places its edges exactly.
- An output of Space-Vector Modulator between -1 and 1 must not be compared with the Carrier, which runs from 0 to 1. Set output of the Space-Vector Modulator to duty, or its lower half never turns the switch on.

## Datasheet and sources

No datasheet applies. The threshold comes from your design, for example an overcurrent trip level set from the device current rating with a margin.

## Example

Open the example with the command `file.openExample` and the name `demo:comparator`.

---
Generated by `cargo xtask reference` from the usage texts and parameter specs. Do not edit by hand.
