# Op-amps and virtual ground Apr 2016

The voltage-divider-tied-to-3.3V trick allows shifting the level of an input voltage to another range. But there are a few issues with it.

The first one is that we still cannot measure anything above 3.3V this way.
Regardless of the values of Z1 and Z2, higher voltage would cause `Vout`

to rise
above 3.3V: not good for the ADC.

We could consider combining two voltage dividers, i.e. the first to reduce the voltage swing, feeding the second one for level shifting, but generally that’s not a good idea:

One reason is that a voltage divider assumes that there is no
current flowing in or out of `Vout`

. With cascaded voltage dividers,
we break that assumption and their behaviour will change.

There’s another way - instead of tying the divider to 3.3V, we tie it to a lower fixed voltage:

- suppose we want to handle a voltage swing of ±5V
- that’s a total range of 10V
- it needs to be reduced and shifted to the range 0..3.3V
- we can use a 3:1 voltage divider for this
- for example with Z1 = 20 kΩ and Z2 = 10 kΩ
- with a 3:1 divider, Vout will swing a third of the input

So far so good, but we need to reference this divider to some voltage. Let’s say we tie it to 2.4V:

- when
`Vin`

is +5V,`Vout`

will be`(5-2.4)/3 = +0.87V`

relative to 2.4V = 3.27V - when
`Vin`

is +2.4V,`Vout`

will be`(2.4-2.4)/3 = 0V`

relative to 2.4V = 2.4V - when
`Vin`

is 0V,`Vout`

will be`(0-2.4)/3 = -0.8V`

relative to 2.4V = 1.6V - when
`Vin`

is -5V,`Vout`

will be`(-5-2.4)/3 = -2.47V`

relative to 2.4V = -0.07V

This is not quite inside the allowed range, but by increasing Z2 a bit
for a slightly higher division ratio than 3:1 (perhaps 2.2 kΩ or 2.4 kΩ),
the maths will work out and place `Vout`

well in range.

*Which brings up the next question: how do we create a 2.4V reference voltage?*

Again, we *could* use a voltage divider, such as a 26 kΩ Z1 in series with a
24 kΩ Z2 between +5V and ground. But that that’s a bad idea for exactly the
same reason as before: combining voltage dividers leads to currents flowing in
unintended ways.

What we need is a *stiff* 2.4V supply: one which doesn’t change if we load it a
bit and draw some current (or *feed* it some current, when `Vin > 2.4V`

).

Meet another workhorse of the electronics words, the Operational Amplifier, and its notation:

The topic of op-amps is far too extensive to cover in full, but we only need a few properties here:

- whenever
`V+`

is above`V-`

,`Vout`

rises - whenever`V+`

is below`V-`

,`Vout`

drops - the gain of an op-amp is very high, a tiny input change leads to a large
output swing while
`V+`

and`V-`

stay close together - the input current at both
`V+`

and`V-`

is (extremely close to) zero

Also important to know is that `Vs+`

and `Vs-`

are the power supply connections,
and that both inputs (`V+`

and `V-`

) and the output (`Vout`

) will be limited by
those two supply levels.

The circuit we are going to need here is the voltage follower:

Considering the above properties, `Vout`

will “follow” `Vin`

: when `Vin`

goes
up, `V+`

becomes higher than `V-`

, forcing up `Vout`

until `Vin`

and `Vout`

match again. Similarly when `Vin`

goes down.

Since the input draws almost no current, yet the output can supply or consume
current, this is also called a *buffer amplifier*: it isolates or buffers the
input from loading effects on the output. Which makes it ideal for creating
a “stiff” voltage reference:

The output is 2.5V, same as the `V+`

input, since it’s tied to a 2:1 voltage
divider across the 5V supply. But unlike the input, it can supply many milliamps
without affecting its output voltage. This voltage level can be used as a
“virtual ground”, i.e. as a reference for other voltages.

Perfect for our needs. By tweaking the resistors a bit, we can produce any voltage we want, and use that as reference for the second (right-hand side) part of that first diagram above:

So with 4 suitably-chosen resistors, this will map any ± `Vin`

voltage to
the 0..3.3V of the ADC. But there’s another important drawback, which
requires another op-amp and 2 more voltages.