# Relay Controlled Gain for Microcontroller

I'm thinking about controlling analog circuits with a micro controller and lots of tiny relays (Omron G6L). For signal routing this is fine but gain control is troubling.

Obviously I could do something like this:

But this requires a relay for each step. I'm thinking there has to be a better way.

Is there a clever resistor network arrangement that can yield the voltage dividers found in inverting and non-inverting op amp circuits, panning controls, etc?

More specifically, is there a parallel and / or series network of relay controlled resistors that can yield the ratios necessary to control gain in op amp circuits (both inverting and non-inverting), pan controls, etc?

With 2 relays that is 4 possible ideally. With 3 relays that's 2 x 2 x 2 = 8 steps. With 8 steps of 3dB each that would give me a 21dB range from say -9 to +12 or whatever. As the gain is increased or decreased, the microcontroller would just iterate through a predefined sequence of binary codes.

Surely this has been studied before but I'm not an EE (I'm just doing this for fun) so any pointers would be greatly appreciated.

## 7 Answers

Check out analog multiplexers like 4051. These devices are commonly used for this kind of application. In practice they form an analog (and in fact bilateral) connection between one of the 'inputs' (Y0 - Y7) and the output (Z), selected by a binary selector (A0 - A2). It would replace all your relays (up to eight) and you set attenuation per input by a resistive divider. With a supply voltage of 10V the ON resistance is in the order of couple 100$\Omega$.

Similar story goes for 4052 which has 2 independent 4 channel analog (de)multiplexers.

Basically the same circuit as yours:

simulate this circuit – Schematic created using CircuitLab

Another possibility is to use an DAC and feed your input signal into its reference voltage input and select the attenuation with the digital input. Not all DAC's are fast enough for this application, but it can give you many bits of resolution.

• As wired, the circuit makes the gain depend upon the resistance of the 4051. A better approach may be to have a series string of resistors from the output to ground, and have the 4051 select a "tap" to be fed to the input. Mar 2 '13 at 1:53
• How is this different from a digital potentiometer? The circuits I'm using are +-15V and maybe more for vacuum tube circuits and my understanding is that digital potentiometers do not operate well with such a large signal range. Also, does this IC add any noise or distortion? I like the idea of a relay which is does not add any noise or distortion. Mar 2 '13 at 2:27
• I agree. Digital potentiometers would be ideal here. Also, some microcontrollers already include op amps. A VGA can also work (Variable Gain Amplifier). Mar 2 '13 at 3:52

Ok, I'm going to try to answer my own question.

I wrote a little Java program to compute all of the possible combinations of resistor values for a voltage divider composed of resistors switched in parallel. This program then emits the binary sequence necessary to iterate through the ratios in increasing order, the corresponding ratios and of course the necessary resistor values. I can specify how many switches and their orientation, the target ratios, desired accuracy, specific resistor values to be used and so on. The result is a resistor network with a totally custom taper.

This results are quite good for log tapers. In some cases the results were within 1% of the target ratios. There are some "magic" values that produce useful gains. For example, if you use 2 switches (or 2p4t rotary switch) in an inverted op amp config with a divider like:

10k || 10k
----------
10k || 3.3k


where an upper 10k and the 3.3k are switched, this yields gains of almost exactly -6dB 0dB 6dB and 12dB.

Here's a more elaborate example:

The following are target dB values for 3 switches which of course yields a total of 8 steps along with the values determined by the Java program:

target  computed
-12.01  -10.91
-6.00   -6.89
-3.00   -3.86
0.00   -0.89
3.00    3.12
6.00    6.15
12.00   11.70
21.00   21.71


You can see the values have been customized a little. I went for finer gain control around 0dB but dropped the 1st step and significantly increased the 7th and 8th steps. The resistors necessary to get these gains are shown in the schematic. I also simulated this in LTSpice to confirm everything works as advertise.

The results are not perfect. The average accuracy of all divider ratios was 2.7% but at least one value was off by 15%. Fortunately these tend to be the end values as it is difficult for ratios to reach 0 or 1. The 1st step was only -10.91dB whereas I was looking for -12dB.

I think reverse log should be quite good as well since you can just run through the binary sequence in reverse.

Of course this is also applicable to non-inverted op amp configurations or anything that uses a voltage divider including tone stacks and panning controls and so on.

So the answer is - it certainly is possible. But it requires some work to find the right resistor values. I found no obvious "golden rules".

The end result you seem to be after is a programmable-gain amplifier (PGA). You've mentioned a microcontroller so for example a Microchip MCP6S21 offers gain steps of 1, 2, 4, 5, 8, 10, 16 and 32 V/V and can be controlled over an SPI bus. Most I've seen don't offer attenuation as you require, but attenuating the signal (or amplifying less) before-hand might be a good option.

That particular part may not meet your exact requirements but PGAs are widely available and it may be a term you haven't heard of before and searched for, but they are often used to solve this particular problem.

• If I'm not mistaken it looks like this PGA has a signal range of less than 7 Vpp, it adds 30% more noise and 200 times greater THD than a good regular op amp. Mar 2 '13 at 16:09
• @ioplex Yes that was just an example of a cheap one. Digikey has hundreds listed, for example a PGA280 (wider supply and attenuation as well) might be closer to what you need. Not saying they're the right choice for every application - just for many they reduce the I/O and component count and can generally be more covenient. Mar 2 '13 at 23:08
• @ioplex - Do you know how much noise and distortion the relays would add? Mar 5 '13 at 10:25
• I assume absolutely 0 noise and 0 distortion because it's a physical gold plated contact with an on resistance of 100 milli ohms and off resistance of 500 mega ohms and can handle 0.3A at 125V and 1A at 24V. Please correct me. Mar 5 '13 at 16:48

You might consider a motorized potentiometer. It will give you nice and smooth control.

You can find them on ebay, and SparkFun carries them as well.

Another way to control the gain of an OpAmp electronically is with a Digital Potentiometer. This way you can have fine control over the gain if you want. And it has the benefit of a very small, low cost solution.

The AD5231 is a pretty good device with 1024 positions.

Another way to do this is with multiplying DACs. Analog Devices makes them, Texas Instruments makes them, and probably so do other companies to.

Here is the internal structure of a 12-bit MDAC:

If you imagine Iout1 also being at ground potential, the resistance between Vref and Ground doesn't depend on the position of S1 to S12, so the current flowing through Vref doesn't depend on the switches and is proportional to Vref, but what current flows through AGnd, and what current flows through Iout1 does depend on the position of the switches. Each switch of the R-2R ladder controlls where half of the current going into it is going: through Iout1 or AGnd. So S1 is the MSB, S12 is the LSB.

So if we combine this device withe an op-amp in an inverting amplifier circuit (which has the "-" input of the op-amp at ground potential):

simulate this circuit – Schematic created using CircuitLab

we get a digitally controlled attenuator. If the feedback resistor and DAC are switched arround you get a digitally controlled gainer. This circuit can be slightly modified to give both gain and attenuation.

The thing to remember about the MDAC is that the value of R is not precise (typically 5 to 10KOhm) and can change by several KOhm from one IC to another of the same type, but the RATIO between the resistors is very precise. This makes it possible to build digitally controlled amplifiers, digitally controlled filters‎, etc.

Two issues:

First, you can't get gains above and below 1 (positive and negative when expressed in dB) from just switching in diferent feedback resistors. In fact, the type of non-inverting topology you show can not have gain less than 1.

Second, why not use a programmable gain amp in the first place?