---
title: Comparators, Schmitt triggers and oscillators
heading: Op amps without negative feedback
eyebrow: C6 · Lesson 48 · Comparators and oscillators
xp: 50
---

::: hook
Take away the negative feedback and an op amp becomes a decision-maker: its output slams to one rail or the other depending on which input is higher. That's a **comparator**, the boundary between the analog world and the digital one. Add *positive* feedback and it gains memory, switching at two different thresholds so that noise can't make it chatter. Add a capacitor that it charges and discharges, and it oscillates on its own.

::: play
- A relaxation oscillator: the op amp's output charges C through R; the output switches whenever the capacitor's voltage crosses a threshold set by the divider on the + input.
- Watch the capacitor's voltage on the scope. Between what two levels does it swing?
- Change R or C. How does the period change? (A tiny step source starts it, as electrical noise would.)
:::
:::

```sim circuit
U1 p n out GBW=10M SR=1M VSAT=10    @ 4,1
R1 n out 10k                        @ 4,-2 right 3
C1 n 0 100n                         @ 2,0 down 2
R2 p out 10k                        @ 4,4 right 3
R3 k p 10k                          @ 1,4 right 3
V9 k 0 step 1m                      @ 1,4 down 2
slider: R1 2k..50k log
slider: C1 20n..500n log
scope: v(out), v(n)
time: 0..10m s
```

::: predict
## In the oscillator above, the divider feeds half the output back to the + input, and the output swings between +10 V and −10 V. Between what levels does the capacitor swing?

- [ ] 0 and 10 V
- [x] −5 V and +5 V
- [ ] −10 V and +10 V
- [ ] It doesn't swing

::: reveal
Between −5 V and +5 V. With the output at +10 V, the + input sits at +5 V; the capacitor charges toward +10 V until it passes +5 V, which flips the output to −10 V and the threshold to −5 V. Then it discharges toward −10 V until it passes −5 V, and so on. Each half cycle takes $RC\ln 3$, so the period is $2RC\ln 3 \approx 2.2RC$.
:::
:::

::: nameit
## Comparators and positive feedback

**Comparator.** With no feedback, $v_\text{out} = +V_\text{sat}$ when $v_+ > v_-$ and $-V_\text{sat}$ otherwise. If the input is noisy near the threshold, the output **chatters**, switching several times on each crossing.

**Schmitt trigger.** Feed a fraction β of the output back to the + input. The threshold then depends on the output's state:

$$V_{\text{th}\pm} = \pm\beta V_\text{sat} \qquad \text{hysteresis} = 2\beta V_\text{sat}$$

(for an inverting Schmitt with the reference at 0). Once the output switches, the threshold jumps away from the input, so small noise can't switch it back. Digital inputs of microcontrollers use Schmitt triggers for exactly this (C2.26).

**Relaxation oscillator.** An inverting Schmitt trigger whose input is a capacitor charged from its own output through R. The capacitor heads toward $\pm V_\text{sat}$ but turns round at $\pm\beta V_\text{sat}$:

$$T = 2RC\ln\frac{1 + \beta}{1 - \beta}$$

which is $2.2RC$ for β = ½. The output is a square wave; the capacitor's voltage is a train of exponential segments.

**In practice.** Dedicated comparator chips switch in nanoseconds and have outputs made for logic; op amps used as comparators are slower, limited by their slew rate. The 555 timer is a pair of comparators and a flip-flop built for oscillators and timers. Relaxation oscillators aren't precise (their period depends on R, C and the thresholds); clocks use crystals.
:::

```sim circuit
V1 x 0 sine 1 V 100 Hz              @ 0,4 down 2
V2 in x sine 100m V 5k Hz           @ 0,2 down 2
U1 in 0 o1 GBW=1M SR=1M VSAT=5      @ 5,3 flip
R5 in p 10k                         @ 0,2 up 4
U2 p 0 o2 GBW=1M SR=1M VSAT=5       @ 3,-1 flip
R6 p o2 100k                        @ 3,-4 right 3
scope: v(in), v(o1), v(o2)
time: 0..10m s
```

A slow sine with a little fast noise feeds a plain comparator (U1) and a Schmitt trigger (U2) whose thresholds are ±0.5 V. The comparator chatters at each crossing; the Schmitt trigger switches once.


::: example
## A Schmitt trigger on a ±12 V op amp feeds back through 100 kΩ, with 10 kΩ to ground. What are its thresholds?

::: solution
1. **Feedback fraction.**
   $$\beta = \frac{10}{110}$$
2. **Thresholds.** $\beta \times 12$ V gives ±1.09 V, so the hysteresis is 2.18 V: noise smaller than that can't make it chatter.

**Answer:** the thresholds are ±1.09 V, 2.18 V of hysteresis.
:::
:::

::: example
## A relaxation oscillator uses R = 10 kΩ, C = 100 nF and β = 0.5. What is its frequency?

::: solution
1. **Period.**
   $$\begin{aligned} T &= 2RC\ln\frac{1.5}{0.5} \\ &= 2 \times 1\ \text{ms} \times 1.10 \\ &= 2.20\ \text{ms} \end{aligned}$$
2. **Frequency.**
   $$\begin{aligned} f &= \frac{1}{2.20\ \text{ms}} \\ &= 455\ \text{Hz} \end{aligned}$$

**Answer:** the oscillator runs at 455 Hz.
:::
:::

::: example
## A ±12 V Schmitt trigger must clean a signal that carries 0.3 V of peak noise. With 10 kΩ from the non-inverting input to ground, what feedback resistor gives enough hysteresis?

::: solution
1. **Hysteresis needed.** The noise can swing the input 0.6 V peak to peak, so the hysteresis, $2\beta V_\text{sat}$, must be at least 0.6 V:
   $$\beta \ge \frac{0.6}{24} = 0.025$$
2. **Feedback resistor.** With 10 kΩ to ground,
   $$\frac{10}{10 + R_f} = 0.025$$
   gives $R_f$ = 390 kΩ.
3. **With less.** A noisy edge crosses the threshold several times and the output chatters.

**Answer:** a 390 kΩ feedback resistor gives enough hysteresis.
:::
:::

::: check
given: beta = 0.1 | 0.2 | 0.5; Vsat = 5 | 12
answer: beta*Vsat
unit: V
tolerance: 0.5%

## A Schmitt trigger feeds back β = {beta} of its ±{Vsat} V output. What's its upper threshold?

::: right
Right. $\beta V_\text{sat}$ = {answer}.
:::

::: misses
- [Vsat] Only a fraction β is fed back.
- [2*beta*Vsat] That's the hysteresis, the gap between the two thresholds.
- [any] Not yet. $V_\text{th} = \beta V_\text{sat}$.
:::
:::

::: check
given: beta = 0.1 | 0.2 | 0.5; Vsat = 5 | 12
answer: 2*beta*Vsat
unit: V
tolerance: 0.5%

## Same Schmitt trigger: β = {beta}, ±{Vsat} V. How wide is its hysteresis?

::: right
Right. From $-\beta V_\text{sat}$ to $+\beta V_\text{sat}$: {answer}.
:::

::: misses
- [beta*Vsat] That's one threshold; the hysteresis spans both.
- [any] Not yet. $2\beta V_\text{sat}$.
:::
:::

::: check
given: R = 10 | 47 | 100; C = 10 | 100; beta = 0.5
answer: 2*R*1e3*C*1e-9*ln(3)*1000
unit: ms
tolerance: 0.5%

## A relaxation oscillator has R = {R} kΩ, C = {C} nF and β = ½. What's its period?

::: right
Right. $2RC\ln 3$ = {answer}.
:::

::: misses
- [R*1e3*C*1e-9*ln(3)*1000] That's half a period.
- [2*R*1e3*C*1e-9*1000] Include the $\ln 3$.
- [any] Not yet. $T = 2RC\ln\frac{1 + \beta}{1 - \beta}$.
:::
:::

::: check
given: R = 10 | 22; C = 10 | 47; beta = 0.2 | 0.8
answer: 2*R*1e3*C*1e-9*ln((1 + beta)/(1 - beta))*1000
unit: ms
tolerance: 0.5%

## A relaxation oscillator has R = {R} kΩ, C = {C} nF and β = {beta}. What's its period?

::: right
Right. $2RC\ln\frac{1 + \beta}{1 - \beta}$ = {answer}.
:::

::: misses
- [2*R*1e3*C*1e-9*ln(3)*1000] $\ln 3$ is only for β = ½.
- [any] Not yet. $T = 2RC\ln\frac{1 + \beta}{1 - \beta}$.
:::
:::

::: check
kind: choice

## Why does a Schmitt trigger switch cleanly on a noisy signal while a plain comparator chatters?

- [ ] It filters out the noise
- [x] Once it switches, its threshold jumps away from the input, so noise smaller than the hysteresis can't switch it back
- [ ] It's faster
- [ ] It uses negative feedback

::: right
Right. The positive feedback moves the threshold as soon as the output changes.
:::

::: misses
- [1] The noise still reaches its input; the hysteresis makes it harmless.
- [3] Speed doesn't prevent chatter.
- [4] It uses positive feedback.
:::
:::

::: check
kind: parts
given: V = 5 | 12 | 15; n = 0.1 | 0.3 | 1; R = 4.7 | 10

## A ±{V} V Schmitt trigger must reject {n} V of peak noise, with {R} kΩ to ground at its non-inverting input.

::: part
answer: 2*n/(2*V)
tolerance: 0.5%

### What β does it need?

::: misses
- [n/(2*V)] The noise swings twice its peak.
- [any] $2\beta V_\text{sat} \ge 2v_n$.
:::
:::

::: part
answer: R*(2*V/(2*n) - 1)
unit: kΩ
tolerance: 0.5%

### What feedback resistor?

::: misses
- [any] $\frac{R}{R + R_f} = \beta$.
:::
:::
:::

::: check
kind: derivation
vars: R = 1000..1000000, C = 0.000000001..0.000001, b = 0.05..0.95

## A relaxation oscillator has R, C and feedback fraction b.

::: step
answer: 2*R*C*ln((1 + b)/(1 - b))

### What's its period?
:::

::: step
answer: 1/(2*R*C*ln((1 + b)/(1 - b)))

### Its frequency?
:::

::: step
answer: (exp(1/2) - 1)/(exp(1/2) + 1)

### What β makes the period exactly RC?
:::

::: right
Right. β ≈ 0.245 gives T = RC.
:::
:::

::: explain
## How does a relaxation oscillator keep oscillating without any input signal?

::: model
The op amp's output is always at one of its two saturation levels, and it charges the capacitor toward that level through the resistor. The positive feedback divider sets a threshold partway there. When the capacitor's voltage crosses the threshold, the comparator flips its output to the other level, which also moves the threshold to the opposite side. Now the capacitor heads the other way until it crosses the new threshold, and the output flips back. The cycle repeats on its own, each half taking a time set by RC and the threshold fraction.
:::
:::

::: practice

::: check
given: R = 1 | 10; C = 1 | 10
answer: 2.2*R*C
unit: ms
tolerance: 1%

## Relaxation oscillator with β = ½: R = {R} kΩ, C = {C} µF. Period (≈ 2.2RC)?

::: misses
- [R*C] It's about 2.2RC.
- [any] $2.2RC$.
:::
:::

::: check
given: R1 = 10 | 22; R2 = 100 | 220
answer: R1/(R1 + R2)
tolerance: 0.5%

## A divider of {R1} kΩ (to ground) and {R2} kΩ (to the output) feeds back what fraction β?

::: misses
- [R1/R2] β is $R_1/(R_1 + R_2)$.
- [any] $R_1/(R_1 + R_2)$.
:::
:::

::: check
given: T = 1 | 2 | 5
answer: 1000/T
unit: Hz
tolerance: 0.5%

## An oscillator's period is {T} ms. Frequency?

::: misses
- [any] $1/T$.
:::
:::
:::
