# Gas station without pumps

## 2019 May 22

### Interaction between bias resistor and active high-pass filter

Filed under: Circuits course — gasstationwithoutpumps @ 00:02
Tags: , , , ,

In grading the preamplifier lab, I made a mistake when correcting a number of student papers.  Students who had used a bias resistor rather than a transimpedance amplifier to convert the microphone’s current output to voltage had not taken into consideration the interaction between the bias resistor and the input impedance of the next stage, which was usually an active high-pass filter.  In grading, I overcorrected the student work, changing both the i-to-v gain and the first-stage gain, when the correct action would have been to change either one, leaving the other alone.

Schematic of bias resistor and active high-pass filter. The input is the current I_in.

The passband gain for the circuit is $R_b\frac{R_f}{R_b + R_i} = (R_b || R_i) \frac{R_f}{R_i}$. The first version corrects the gain of the filter, while the second version corrects the gain of the current-to-voltage conversion. In my grading, I mistakenly applied the correction twice getting $(R_b || R_i) \frac{R_f}{R_b + R_i}$.

There are two ways to get to the correct answer: using Thévenin equivalence and from first principles.

If we replace the current input and $R_b$ with a Thévenin equivalent, whose AC voltage is the AC component of $I R_b$ and whose resistance is $R_b$, then we get a simple active high-pass filter with passband gain $\frac{R_f}{R_i + R_b}$ for a total passband gain of $R_b\frac{R_f}{R_b + R_i}$ and a corner frequency of $\frac{1}{2 \pi (R_i+R_b) C_1}$.

For those who don’t quite trust themselves to do Thévenin equivalence, we can use first principles to reason about the various currents in the schematic. The negative-feedback loop holds the op amp’s negative input to $V_{ref}$, and the input node has a voltage, so we get
$V_{input} = V_{dd} - I_b R_b = V_{ref}-I_f \frac{j\omega R_i C_1 + 1}{j \omega C_1}$
which we can rearrange to get
$I_b = \frac{V_{dd} - V_{ref}}{R_b} + I_f \frac{j \omega R_i C_1 + 1}{j\omega R_b C_1}$.
Because $I = I_b + I_f$, we get
$I= \frac{V_{dd} - V_{ref}}{R_b} + I_f \frac{j \omega R_i C_1 + 1}{j\omega R_b C_1} + I_f$
and can solve for $I_f$ to get
$I_f = (I- \frac{V_{dd} - V_{ref}}{R_b}) \frac{j\omega R_b C_1}{1+j\omega(R_b+R_i)C_1}$.

Finally, because $V_{out}-V_{ref} = I_f R_f$, we get
$V_{out}-V_{ref} = R_f (I- \frac{V_{dd} - V_{ref}}{R_b}) \frac{j\omega R_b C_1}{1+j\omega(R_b+R_i)C_1}$.

Our transimpedance gain (including the DC offsets for input current and output voltage) is
$\frac{V_{out}-V_{ref}}{I- \frac{V_{dd} - V_{ref}}{R_b}} = R_f \frac{j\omega R_b C_1}{1+j\omega(R_b+R_i)C_1}$.
At DC, this has the appropriate gain of 0, and for high frequencies (in the passband), the gain is approximately $\frac{R_f R_b}{R_b + R_i}$, as claimed earlier. The corner frequency, where the real and imaginary parts of the denominator match is at $\omega = \frac{1}{(R_b+R_i)C_1}$.

## 2017 June 23

### Uncompensated transimpedance amplifier

Filed under: Circuits course — gasstationwithoutpumps @ 16:07
Tags:

In my book Applied Electronics for Bioengineers, I have students build transimpedance amplifiers for phototransistors (and some students use them for electret microphones as well).  In the book, I never deal with compensating the transimpedance amplifiers to avoid oscillation, as I try to stay away from students having to reason about phase of signals and oscillation has never been a problem in the student designs.

But I thought that I ought to understand the method myself, especially if I need to help students trying to do higher bandwidth, higher gain transimpedance amplifiers.  First I read up on the subject—one of the better introductions is the Maxim application note 5129 Stabilize your transimpedance amplifier.  The key concepts are the following:

• When the frequency is high enough (where the open-loop gain is limited by the gain-bandwidth product) the phase change of the amplifier is about –90° (or 90° for the negative input).
• If we set up a transimpedance amplifier with feedback resistor R, then the feedback consists of a low-pass RC filter: a voltage divider with R on tap and the input capacitance of the amplifier and any capacitance in parallel with the current source on the bottom.
• The phase change of a low-pass RC filter (gain $\frac{1}{1+j\omega RC}$) approaches –90° above the corner frequency.
• Having a phase change of 0° and gain ≥ 1 around a feedback loop results in instability and possible oscillation.

That means that we can have instability at frequencies between $\frac{1}{2\pi RC}$ and the gain-bandwidth product (though we probably only have problems for frequencies at least a factor of 3 above the low-pass corner frequency, since the phase change of the filter is only asymptotically –90°).  If the parasitic capacitances are low and we only request small transimpedance gain, then RC is small, and the corner frequency of the low-pass filter is above the gain-bandwidth product, so there are no problems.  Will the students ever encounter problems?

Today I tried to make an unstable transimpedance amplifier using the MCP6004 op amps that we use in class.  The op amps have a gain-bandwidth product of 1MHz, so I needed an RC time constant much larger than 160ns.  I chose 2MΩ and 47nF for an RC time constant of 94 ms and a corner frequency of 1.69Hz.

The very large bypass capacitors are to make sure that there are no sneak paths through the power supply and positive input—to make sure that I’m looking at the phenomenon I’m really interested in.

I connected the amplifier up to the Analog Discovery 2, and I definitely got instability:

There does seem to be a somewhat unstable oscillation happening.

The reasoning about the amplifier instability suggests that the oscillation should be at about the frequency where the gain around the loop is 1, that is where $\frac{f_{GBW}}{f}\frac{1}{2\pi f RC}=1$ or $f= \sqrt{\frac{f_{GBW}}{2\pi RC}}$. For the circuit I made, that would be around $\sqrt{1MHz \; 1.69Hz}= 1.3kHz$.

I did some FFTs of the waveform (averaging over hundreds of traces to reduce noise, since the signal is fluctuating).

The peak is around 1380Hz, very close to the predicted oscillation frequency. Also visible are harmonics of 60Hz, which are the correct output of the transimpedance amplifier (picking up stray currents by capacitive coupling).

To compensate a transimpedance amplifier, we need to add a small capacitor in parallel with the feedback resistor, making the gain of the feedback filter $\frac{1+j\omega R_{F}C_{F}}{1+j\omega R_{F}(C_{F}+C_{i})}$, where $R_{F}$ and $C_{F}$ are the feedback components and $C_{i}$ is the input capacitance. For “optimal” compensation, we want to set the upper corner frequency $1/(2\pi R_{F}C_{F})$ at the geometric mean of the lower corner frequency $1/(2\pi R_{F}(C_{F} + C_{i}))$ and the gain-bandwidth product $f_{GBW}$. Using a larger capacitor (overcompensating) increases the phase margin (thus allowing for some variation from specs) at the cost of reducing the bandwidth of the final amplifier.

We can set the equation up as $1/(2\pi R_{F}C_{F})^2 = f_{GBW}/(2\pi R_{F}(C_{F} + C_{i}))$, which we can simplify by assuming that $C_{i} \gg C_{F}$ to get $C_{F} = \sqrt{ \frac{C_{i}}{2 \pi R_{F}f_{GBW}}}$, which for my design comes to 61pF.

A 68pF compensation capacitor cuts out the oscillation peak, but there is still a fair amount of noise around the corner frequency of the amplifier (1.2kHz). Overcompensating with a 680pF capacitor reduces the noise substantially, but the bandwidth is reduced to 120Hz.

I also tried a somewhat more realistic example, with only a 2.2nF input capacitance, which calls for about a 13pF compensation capacitor. A 20pF capacitor does fine:

The oscillation is well suppressed by the compensation capacitor.

Now I have to decide how much (if any) of this to include in my book. Perhaps it can be an optional “advanced” section in the transimpedance amplifier chapter?

## 2016 October 2

### Pulse monitor using log amplifier

I am currently rewriting the optical pulse monitor lab for my Applied Electronics for Bioengineers book, because I’ve moved it earlier in the course, and because I need to remove the calculation of how big the signal should be, which never worked out very well.  There were way too many assumptions in the calculation, and it turned out to be much easier and more accurate to “try it and see” than to do the calculations.

I always do the labs (often several times in different ways) while writing the lab chapters, so I’ve been playing with pulse monitors again.  I’ve decided that I will definitely have the students do a logarithmic transformation of the photocurrent to voltage, as that eliminates the need to guess a resistance value for the first-stage amplifier.  The amplifier can be designed in two stages, with the first one doing the logarithmic conversion and the second one providing sufficient gain over the desired frequency range (where “sufficient” gain is determined by measuring the output of the first stage).

In Using 4¢ diode for log-transimpedance, I talked about using a 1N914 diode as the feedback element of a transimpedance amplifier to get an output voltage as (roughly) the log of the input current.  There are actually two circuits that are subtly different ways to convert the log of current to voltage:

Two circuits that convert the logarithm of light intensity to voltage.

One of the exercises in the book is to compare the circuits (particularly looking at the constraints on what Vref can be in each).

Because I’d not played with it before, I tried using the unity-gain buffer design this weekend, to make sure the circuit worked and to see whether a single-stage amplifier provided enough signal to record with PteroDAQ using the 16-bit ADCs in the Teensy boards. I was also interested in trying out a new design for holding the phototransistor, so that students could experiment with ambient-light pulse monitors and cisillumination, where the illuminating LED is on the same side of the finger as the phototransistor.  In previous years we have always used transillumination, shining the light through the finger, but most wearables use a cisillumination design, because it is mechanically simpler and much cheaper to make.

I played around with a couple of different ways to make a finger cradle and ended up with the following design:

This is cut from a scrap of some softwood (pine? fir?), with a 1″ diameter hole for the finger, a ¼” hole at right angles for the wires, and two ⅛” holes between them for the 3mm LED and phototransistor.

The finger cradle worked great for ambient light, but when I tried using it for cisillumination, I could not get it to work—I had plenty of photocurrent, and if I modulated the LED the photocurrent was modulated, but I never saw a fluctuation that corresponded to the pulse in my finger. It turned out that the problem was that the wood is too transparent—I was getting so much light through the wood, that the fluctuation in what was returned from my finger was a tiny fraction of the total light—too small to be visible above the noise floor.

I mentioned this problem to my wife, who suggested I use black electrical tape. I’m not sure she quite understood the problem, since the wood was solid between the LED and phototransistor, but her solution was a good one—I just needed to put the tape in the middle of the block of wood!

By cutting between the two 3mm holes I could put black electrical tape to block the short-circuiting light path.

With this design I could get small but recordable signals with either ambient light or cisillumination:

Response of first stage amplifier with an LED desk lamp shining through my left thumb.

Output of first stage using an IR emitter on the same side of the left thumb as the phototransistor.

Both the plots above are a little misleading, as they were sampled at 60Hz, to alias out any 60Hz or 120Hz interference. With steady bright light from my LED desk lamp, I got similar plots even at 600 Hz, but with compact fluorescent illumination, the signal was buried in 120 Hz interference:

With 600 Hz sampling, it is easy to see the effect of modulated light.

I don’t particularly like the electrical tape and wood approach to making the finger cradle—it is a bit fragile, and the tape needs to be replaced frequently.  Furthermore, ambient light reaches the phototransistor through the sides and ends of the block, unless the whole thing is wrapped in electrical tape. I think that I’ll buy a chunk of black ABS plastic and try making the whole thing out of it. I can probably get a dozen finger cradles out of a 1′ length of plastic, if I don’t mess anything up (and if all my drills work with ABS—I’ve never worked with ABS as anything but Lego bricks).  Another alternative is to go to the hardware store and see how thick the black ABS couplers are—I might be able to make a finger cradle by cutting one in half lengthwise, if the wall thickness is at least 6mm.  I’d still have to add something to keep it from rolling around.

In the past, I’ve thought about using pulsed light to make pulse monitor measurements that are less sensitive to fluctuations in ambient light. The idea is that I would make a measurement with the LED off, turn the LED on, wait a little while for the sensor to settle, then make another measurement. The difference between the measurements would be due just to the LED light, if the ambient light is changing slowly enough not to be very different between the measurements. I’m not sure that this is a good idea (the large change in light level means that there would have to be less analog amplification than for steady illumination, for example), but I was curious how long the on-pulse would have to be to get good measurements.

I hooked the IR emitter up to a hysteresis oscillator, to get nice sharp square waves, then recorded the output of the amplifier with my Bitscope USB oscilloscope. I used the differential probe to get an extra gain of 5 and AC coupling. I recorded 1156 traces, then used my retriggering and averaging program to superimpose and average the pulses. I ended up with between 2300 and 3500 pulses being averaged (depending where on the waveform you look).

The circuit responds fairly quickly to a rising edge, roughly like having an RC time constant of 84.5µs.

The response to a falling edge is slower, roughly like an RC time constant of 207.8µs.

The response to rising and falling edges is quite different, because of the nonlinear nature of the diode. When we are charging the parasitic capacitance, we have a fairly large photocurrent to do it with, but when we are discharging, the current gets quite small as the voltage across the diode drops. With a linear system, response to a positive step and negative step would be identical, except for the sign.

Both response times are fast enough that the shape we were seeing for the pulse waveforms is due to changes in the opacity of the finger, not due to distortion or filtering in the amplifier.  There is a sudden increase in opacity, followed by a gradual recovery as the blood flows through the capillaries and veins out of the thumb.

If I were to try the difference between LED-on and LED-off measurement, I’d want to have the LED on for at least 400µs, then off for at least 1ms.  (I’d probably do on for 400µs, then off until the next 1/60s tick, making a 2.4% duty cycle for the LED.)

Note also that the 50mV swing here is much larger than the 3mV swing that I got from pulse measurements, so gain would have to be limited to avoid clipping.

## 2016 August 20

### Using 4¢ diode for log-transimpedance

In Transimpedance pulse monitor does need low-pass, I realized that Schottky diodes were not going to work well for the transimpedance amplifier, and in Using nFET body diode for log-transimpedance, I tested using the body diode of a power nFET, finding that it worked quite well over at least 7.5 decades (from 1nA to 40mA).  But I wanted to see whether students could use a cheap 4¢ general-purpose diode.

I used the same setup as when testing the nFET body diodes.  The results were pretty much the same whether I used a 1N914B or 1N4148 diode (they share a datasheet, but the 1N914B has somewhat better constraints on the forward voltage):

The gain (in mV/dB) is about 85% larger than using an nFET body diode.

Note that at currents over about 1mA the diode current starts to saturate, deviating from the exponential pattern.

When I tried using the 1N914B diode in the same log-transimpedance amplifier as I used for the nFET body diode, it didn’t work—I got output that looked nothing like a pulse (nor like 60Hz interference). I could recover proper behavior by putting a large (100nF) capacitor in parallel with the diode, to make a low-pass filter to remove signals above a few Hz, but that wasn’t necessary for the nFET body diode (perhaps it had enough internal capacitance to do the filtering). I could reduce the capacitor to 100pF, with 60Hz interference coming in, though not being too bad, but reducing to 10pF gave me noise again rather than the pulse signal.

I was hoping not to need that extra capacitor, because the design is already more complicated than I would like for this stage of the course, and figuring out what capacitor to use is difficult—trial and error is easier than rational design here!

I tried tracking down the big, short (less than 250µs) spikes that were corrupting the signal. The first thing I tried cleaned up the problem entirely: disconnecting the power supply from the laptop so that the USB power was coming from the laptop battery rather than the power supply . That this worked actually surprised me, since the 3.3V supply and the 1.65V Vref both had beefy bypass capacitors.

I don’t know whether the noise problems are in the microcontroller (which is providing the regulated 3.3V from the noisy USB 5V) or are coupled into the analog circuit some other way. Putting a 10µF capacitor from the USB5V to GND did not help when the power supply was connected, so perhaps the problem is radiated from the power-supply cable rather than conducted through the USB cable.

I’ve noticed problems before with noise from the laptop power supply causing problems in my analog circuits (the 90kHz interference in my ultrasound experiments), and I’ve see much bigger problems with some of the cheap Windows laptops students use. The bottom line, I guess, is that  I have to tell students to run PteroDAQ from battery power, not switching-supply power, even if the power supply seems more than adequately bypassed.

## 2016 August 15

### Using nFET body diode for log-transimpedance

Filed under: Data acquisition — gasstationwithoutpumps @ 21:20
Tags: , , ,

In Transimpedance pulse monitor does need low-pass, I realized that Schottky diodes were not going to work for the transimpedance amplifier, but I didn’t have any ordinary silicon signal diodes to test with.  I’ve previously used the base-emitter junction of bipolar transistors for log amplifiers, but I decided this time to test the body diode of an nFET.

I spent a fair amount of time trying to measure the V-vs-I characteristic over a fairly wide range (though only with low currents).  I ended up using several tricks:

• using several different sense resistors to measure the current
• censoring the data so that very low differential voltages across the sense resistor are not plotted
• using unity-gain buffers to provide sufficient drive for the analog-to-digital converter (essential when using large sense resistors)
• using low-pass filters after the unity-gain buffers to try to reduce 60Hz interference (not entirely successful for the largest sense resistor)
• doing fitting to estimate the effective input offset of the differential analog-to-digital input for the voltage across the sense resistor (mainly from the unity-gain buffers)

Here is the test fixture I used. (I reduced the noise a bit more on the 5.6MΩ run by increasing the low-pass filter resistors to 2.2kΩ, but that made things worse for the 120kΩ sense resistor.)

The result of all this care was one of the cleanest logarithmic response plots I’ve collected:

I have over 7 decades of data here, and the log fit is excellent over the whole range.

Measuring down to 1nA on a breadboard is not easy, as the 60Hz interference is a big problem.

I fitted the slope of the log curve by alternating between fitting the offset using the 4.7Ω data and the slope using the 120kΩ data (the 5.6MΩ data seemed a bit too noisy to me). I collected several other sets of data but the plot was too cluttered when I tried to include them, so I kept just enough to get good overlap between the ranges. I’ve got a good logarithmic fit here for about 150dB, and it looks like I could go another 20dB higher (though thermal effects might start mattering above 0.1A).

The equivalent resistance at the bottom of the current range is about 150MΩ and about 14Ω at the highest current I measured with (47mA). Because the equivalent resistance varies so much, the corner frequency of the low-pass filter made by putting a 680nF capacitor in parallel to the diode also varies a lot (17kHz@14Ω, 1.6mHz@150MΩ).

I tried using the body diode in the same minimally filtered circuit as I used for testing the Schottky diode, and got usable results in even in moderately low light:

The 60Hz noise is huge, but can be filtered out digitally.

Brighter light makes the 60Hz noise be smaller (probably because the capacitive coupling introduces a current which is a smaller fraction of the total current), but does not change the strength of the filtered signal. When I switch to very bright light (a 650 lumen bike headlamp right against the finger), then the signal gets stronger, but I had to shift the bias voltage down to 1.65V, as the DC bias on the diode got to 0.55V or more.

So the question still plaguing me: can I use a log-impedance amplifier as the second amplifier lab, given that low-pass filtering is essential?

Putting the filtering in the second and third stage is simpler than putting it in the transimpedance stage, as the corner frequency is independent of the light level then.  It is sufficient to put the RC filter in just the second stage, as long as the attenuation at 60Hz is sufficient—I found that about 48dB attenuation was enough, though with just one RC element that distorts the pulse signal also, since the corner frequency has to be very low (0.24 Hz), below the 1–2Hz of the pulse.  If I do low-pass filtering in both stages, I could use 3Hz cutoffs, which preserves the interesting part of the signal.

Doing two stages of low-pass filtering with 1.06Hz and 1.17Hz cutoffs provides enough suppression of the 60Hz interference that I did not need digital filtering. In medium light, I got a signal large enough to saturate the third stage (so I’d need to redesign with lower gain).  With 1.6Hz and 2.6Hz cutoffs, the signal is still much larger than the 60Hz noise, and less distorted by the filters.  The pulse shape is still more dependent on the filters than on the actual signal from the transimpedance amplifier, which is almost a sawtooth (hence having high-frequency components that are removed by the filter).

If the 60Hz interference is small enough that the amplifiers don’t saturate, I can eliminate it by aliasing (sampling at 60Hz, so I’m at the same place in the interference waveform on each sample). But if the 60Hz interference is too large, then the signal is clipped and aliasing can’t recover the pulse signal.  So digital filtering is definitely optional here—students can get good results with just analog  filters and aliasing.

Alternatively, we could look at just the transimpedance amplifier output, and use digital filtering to clean up the baseline shifts and 60Hz interference.  The biggest problem is that the PteroDAQ sparkline looks like a constant—the fluctuation of a few mV is only visible once the data has been plotted on a larger scale.

Next Page »