They both compare two input voltages and drive their output high or low depending on which voltage is higher. An op-amp however also has a third mode when voltages are equal, holding the voltage level.
Comparators compare input voltages
Short explanation; if the positive input voltage is greater than the negative, the output goes high, otherwise low.
The two images above show the two states a comparator can be in, either drive the output high, or low, depending on the which of the input voltages is higher.
Op-amps can deal with equal inputs
Consider the circuit above, when the op-amp starts, its output is at 0V, and because the negative input is connected to the output, the negative input is also at 0V.
Now the op-amps does what its made for, positive input is greater than the negative, so drive the output to high. I put a graph next to the circuit to show you the output voltage.
As soon as the output voltage hits the +2V level, both inputs are in balance, and so the output voltage holds at that level. If the positive input would change, the output would again follow it. This is a voltage-follower.
If we apply double our maximum voltage over two identical resistors in series, we expect to measure half our voltage at the midpoint. It is this midpoint we are going to measure.
The voltage at the top can be calculated by multiplying the analog-read-voltage by 2. In this example I will assume a 5V microcontroller.
If we add another identical resistor, we add another 5V to the maximum voltage we can read, we can extend this as many times as we like.
We do only intend to measure after the first resistor, and then multiply the measured voltage by three to get the correct voltage. This means we can group the other resistors together by adding them up.
The voltage is below ground/negative
If we want to measure below ground, we have to set this resistor network upside down. This example assumes a 3.3V microcontroller.
Instead of connecting the bottom end to the ground, we now connect the top to the maximum of our controller. And it is now after the top resistor where we read our voltage.
Once again, we simplify the network to only use two resistors by adding them up.
Calculating the actual voltage is not that complicated. First we multiply the voltage on the analog input by three and then add the lowest voltage we can read on the network, which is -6.6V. On the positive network we did not do this because that value was 0V.
So if we read +3.3V the actual value = (3.3 x 3) – 6.6 = +3.3V.
And if we read 0V the actual value = (0 x 3) – 6.6 = – 6.6V.
Both voltages are out of range
When the input can be below ground as well as above the controller’s maximum, we can’t tie one end of the network to ground or max-voltage. We need to tie it in the middle, using another resistor divider. This example assumes a 5V controller.
The standing resistors on the left are equal, and without any voltage to measure would produce +2.5V at the middle for the analog pin to read.
The to-measure-voltage applied to the right will then pull that voltage up or down through the third resistor.
Then how to calculate that third resistor? And how to translate the value read at the analog pin back to the real world value? I could not figure this out in a simple way so I wrote a tool for the Windows platform to do this for me.
This tool gives you the resistor values you need and a map function to translate the value back to real-world-voltage. At the right side you can see the simulation results.
The resistor values are very high, to use as little power as possible, but you can lower them as long as you divide all of them with the same value.