Comparator

A basic comparator is shown below. We use ground (0V) for the reference at the inverting input. Therefore when the signal at the non-inverting input is greater than 0V, the output is high. When the signal at the non-inverting input is less than 0V, the output is low.

gschem Screenshot 1

Transient Analysis:

tran 0.01m 10m
plot v(a1) v(n1)

ngspice Screenshot 1

To make the output inverted, just swap the inputs around:

gschem Screenshot 2

Transient Analysis:

ngspice Screenshot 2

You can use a voltage divider to set the reference voltage:

gschem Screenshot 3

Transient Analysis:

ngspice Screenshot 3