Sunday, 27 July 2014

Microcontrolled Analogue Gain

Digital Controlled Gain

For a long time I've wanted to implement a gain stage where I was able to alter the gain under digital control.

Why?

Back in my Uni days, I have to build an Automatic Gain Control circuit, and was greatly dissapointed that the trick there wasn't variable GAIN but variable ATTENUATION on the front end.  At the end of the day, that approach works well where i used it, but I always wanted to have true programmable gain.

But this was the case of a solution looking for a problem, until I decided that I wanted to build some test equipment that required programmable gain!  So, to test the theory I cooked up this:


It Worked!  Eventually!


At the Drawing Board

For my test gear I wanted to log some circuit voltages over time, but with  a large dynamic range, hence the need for programmable gain.

With an op-amp gain stage, you basically have the option of inverting, non-inverting or differential gain stages.  I was able to eliminate differential as I intended to measure my voltages from a common ground, and if I were to chose a non-inverting amp configuration, I could control the gain by switching the gain set resistor.

In Principle

Everything comes with it's disadvantages, and this approach will mead doing away with unity gain (gain is 1 plus Rf/Rin) but I can live with that.


Select-able Gain

The picture above shows the concept - if you were to switch R1 / R2 in  / out as required, you can vary the gain of the above from 2 to 11 (i.e. 1 + 100/ 100 = 2, and 1 + 100/10 = 11).

This is easy to achieve with physical switches, but note that the gain resistors are switching to GND where the opamp is running from bi-polar supplies.

Kicking It Old School

Now, you could replace the switches with open collector transistors and drive them from logic, and you can achieve digital control of the gain.  Like this:


Switched Gain

Why open collector?  When a transistor is not switched on, it's a high impedance and the resistor floats.  If directly connected to standard logic, you'd set / pull the resistor to / from Ground and 5V and rather than set the gain, you would only be setting different input voltages to your circuit.

For example, if R2 were tied to ground, you may expect Vout to be 11 times Vin, but if R1 is tied to 5V Vout be a sum of (11 * Vin) minus ( 11 * 5V) - where the 55V term will dominate and your output would be hard up against your V- rail.  In short it doesn't work.

How The Cool Kids Are Doing It

Now, one massive advantage of modern microcontrollers is that the IO pins are often configurable as either low impedance outputs, set to digital high or low, or as high impedance inputs.

When set as a high impedance input, the pin is effectively floating.  So in my use above I can directly control my gain setting resistors from an IO pin - set as a low output when I want that resistor to set the gain, or otherwise as a high impedance input.


Micro Controlled.

Add a serial interface to the micro, and you're done - a programmable gain block.  Of note, for this to work you need to run your opamp from a bipolar supply, and don't forget you're using an inverting amplifier.  A -2V output is due to a +1V input if you're running at x2 gain.



Taking Measurements

It's one thing to implement programmable gain, it's another to actually use it.  For a full analogue design, positive and negative signals are quite useful.  

If you want to digitise these signals (i.e. sample with a micro-controller's ADC) you're not going to be able to read the result directly - you can't perform a ADC read on a negative voltage.

Scale It

One approach, is to scale your output.  For example, if your analogue signal swings from -5V to +5V, you could divide it by 2 and then offset it by 2.5V, moving your sample range to a ADC friendly 0 - 5V.

 But this presents some new challenges:
  • Any variation in your 2.5V offset will effect your output
  • Your 2:1 divider needs to be built using well matched parts otherwise you introduce relatively large errors.
  • This introduces an extra amplifier, and with it expense that I was hoping to avoid
So I went looking for other ideas.

It's All About the Magnitude

One approach is to use a precision full wave rectifier. 


LT have a great application note regards this approach, but this also brings it's own issues:
  • You cannot tell if the sample is due to a positive or negative input
  • You now add two extra amplifiers
Useful, but not for my needs.  There are other precision rectifier circuits, but the loss of identifying if the input voltage is positive or negative, is a deal breaker for me.

After lots of googling for inspiration I came across Full Wave Active Rectifiers.  These are pretty cool, and in fact probably coudln't be done 10 years ago as they require rail to rail opamps to work.  The advantages of these opamps are a topic unto themselves but in brief they allow input signals right to the rail (some standard opamps will exhibit 'phase inversion' if you take their input too low, the output will go high.  Not useful at all), and also output to the rails.




But what paked my interest here is that esentially the node labeled Vo above is the positive part of the input waveform, and Vout contains both the positive and negative components.  If I fed this into *two* ADC ports of a micro, I could tell if the original sample were positive or negative.  Most micro's have more than one ADC port these days.
Full 
So now I have the building blocks to employ programmable gain, and measure positive and negative inputs. 

So it was time to put theory to the test:


Test Circuit


Test Board

My test board varies a bit from the Full Wave Active Rectifier above - first of all there's the programmable gain block, as circled in red below.


The yellow block is a unity gain buffer that will feed one ADC channel the positive parts of the test waveform only.  (Of course for this test I was going to observe the voltages on my 'scope, not actually read them on an ADC).

The orange block is a standard inverter, but the gain is x2 because, mistake, but as I was only testing the concept it will do.  I can be fixed with a divider on the input to that stage.

The final blue block is a comparator stage.  High indicates positive input, low indicates negative input, but as it's a comparator if I were to measure an AC signal I could use this output to sample the frequency of the input signal.  Nifty.

So I'd designed a test board and was ready to test, when I got a inspiration - if I could level shift my *design* and not my input signal I could get away from a bipolar rail and simplify my design further.  And yes, I was trying to design a multi-meter equivalent, and I'd also be able to switch between voltage and current measurements with dirt cheap single ended analogue switches.  So until next time...


No comments:

Post a Comment