# Gas station without pumps

## 2013 July 18

### Improved rectifier with Schottky diodes

Filed under: Circuits course,Data acquisition — gasstationwithoutpumps @ 22:32
Tags: , , , ,

In the Improved rectifier post, I gave the following circuit for an inverting rectifier and showed traces of its performance using diode-connected S9018 NPN transistors as diodes:

Only one of D1 and D2 can be conducting.

With a constant amplitude triangle wave input (about 2.6v peak-to-peak),  the circuit had some pretty serious glitches:

frequency positive glitch negative glitch
3kHz 40mV 40mv
10kHz 80mV 80mv
20kHz 120mV 100mv
30kHz 160mV 140mv
40kHz 200mV 180mv
50kHz 220mV 210mv
60kHz 250mV 260mv
70kHz 260mV 300mv

I claimed that I could reduce the glitches  by replacing the NPN transistors with 1N5817 Schottky diodes.  The diodes arrived today, and I tried them out with the same 10kΩ resistors and 30kHz triangle wave as before:

(click to embiggen) With the 1N5817 Schottky diodes, the glitches at 30kHz are much reduced—only about 68mV of overshoot when turning off, which is half of the glitch with the S9018 NPN transistors as diodes.

I noticed that there was a bit of phase shift for the 30kHz signal, as well as the small overshoot. I tried adding capacitors in parallel with the resistors to improve the performance at 30 kHz (both to correct the phase shift and to keep the gain at -1).

This circuit works well up to 30kHz, and is still somewhat functional at 100kHz

C2 seems to adjust the overshoot, and C1 then needs to be set to fix the phase and gain.  I had the best results at 30kHz with C1=330pF and C2=220pF:

(click to embiggen) With capacitors in parallel with the feedback resistors, the phase shift is mostly corrected and there is less than 20mV of overshoot—the turn-on and turn-off corners are softened somewhat.

Unfortunately, there is no easy way in the BitScope software to set the offset of the traces precisely. You can do a lot of range changing and clicking the left or right sides of buttons (and start all over if you accidentally hit the middle of the button), but the offset is only displayed to 2 decimal points, but can be adjusted somewhat finer, making it hard to guess exactly what it is set to. As result, I’ve not been able to measure the overshoot or undershoot when it is less than 10mV—I’m never sure exactly what I’m measuring with respect to, and visually similar settings result in ±10mV in the estimate. In any event, the errors in this version of the improved rectifier are at least 5× better than in the version with the S9018 diode-connected transistors.

The circuit works well throughout the audio range, and can be pushed to 100kHz, though the “corners” have gotten soft enough that clipping to the threshold voltage is no longer very precise at (about 60mV off @ 80kHz—undershoot, not overshoot). At 100kHz, the output signal is still pretty good, but there is about an 85mV error in the threshold, and the corners are so rounded that the output almost looks like a sine wave:

(click to embiggen) Waveform at 100kHZ (sine wave input), showing the soft corners at that frequency. The output does not get down to the threshold voltage, but only to about 85mV above threshold.

I can get better performance at 100kHz with smaller capacitors (100pF and 220pF, instead of 220pF and 330pF), but at the cost of some overshoot at 20kHz and 30kHz.  I suspect that the right values for the capacitors depend heavily on what op amp is used (especially its slew rate), but since I only have MCP6002 (and the equivalent MCP6004) op amps, I’ve not tested this suspicion.

## 2013 July 17

### Improved rectifier

Filed under: Circuits course,Data acquisition — gasstationwithoutpumps @ 00:54
Tags: , , ,

In the Precision rectifier post, I gave the simplest circuit for making a precision rectifier:

This circuit is both a log amplifier and a precision rectifier. If Vb is set to a constant voltage, then Vout1 varies as log(Vb–Va). Vout2 is max(Va,Vb).
The diode can be connected in the opposite direction, to get Vout2=min(Va,Vb) and Vout1 varying with log(Va–Vb).

And I showed the problem with this circuit at “high” frequency, as the slew rate limitations of the op amp limit the turn-on time (to about 8 µs):

(click to embiggen) The S9018 NPN transistor with a 10kΩ resistor and a 15kHz input signal. The overshoot as the rectifier turns off is about 50mV, and the turn-on delay is about 8µsec. The turn-on delay does not vary much with the input resistance, unlike the turn-off overshoot.

I also promised, “There are standard solutions that limit the voltage swing, but I think I’ll leave that for a later blog post.”  This is that post.

The textbook standard solution is to add another diode and resistor, and to configure the rectifier as an inverting amplifier (rather than a unity-gain one) when it is following the input:

Only one of D1 and D2 can be conducting.

This circuit has an input impedance of R1 (not the very high input impedance of the previous circuit). In this circuit, if Vin is more than Vthreshold, the output of the op amp goes low until diode D1 conducts and the negative input of the op amp is held at Vthreshold, as is Vout (with an output impdeance of R2).  If Vin is less than Vthreshold, the output of the op amp rises until D2 conducts, and the feedback circuit makes an inverting amplifier with $V_{out} -V_{threshold} = \frac{-R_{2}}{R_{1}}(V_{in} - V_{threshold})$.  The output impedance is very low.  Note that the difference in the output impedance for the two states is similar to the situation for the simpler circuit, and can cause problems if the output of the rectifier is fed directly to an RC filter, unless the R value for the RC filter is much larger than R2.  For the loudness circuit, we want a very large RC time constant to smooth out the ripples of the rectifier, so a large R value is not a problem.

We expect this circuit to have problems when neither D1 nor D2 is conducting—that is, as the circuit makes transitions between the rectifier being on or off.  The simple rectifier circuit only had problems with turning on (as the op amp had to slew from a rail to a diode-drop past Vthreshold), but this improved circuit has to swing two diode drops when turning on or when turning off.  The two-diode-drop swing is smaller than the

Here is an example of the output with a 30kHz clock, using S9018 transistors as diodes and R1 and R2 both at 10kΩ:

(click to embiggen) Output (yellow) for the improved rectifier with a 30kHz triangle wave as input (green). The glitches are about 140mV–160mV and last for about 4 µsec.

The duration of the glitches is always about 4µs, but the magnitude of the glitches depend very much on frequency.  With a 2kHz triangle wave signal, I can’t see the glitches with the BitScope USB oscilloscope (so less than about 20mV).  The magnitude of the glitch seems to be proportional to the input magnitude. Using a constant amplitude triangle wave input (about 2.6v peak-to-peak),  I measured the glitches for some higher frequencies:

frequency positive glitch negative glitch
3kHz 40mV 40mv
10kHz 80mV 80mv
20kHz 120mV 100mv
30kHz 160mV 140mv
40kHz 200mV 180mv
50kHz 220mV 210mv
60kHz 250mV 260mv
70kHz 260mV 300mv

To understand where the glitches come from, it helps to look at the op-amp output and the negative feedback input:

(click to embiggen) The output of the op amp (green) is either a diode drop above or a diode drop below the output of the rectifier circuit (yellow) depending which diode is conducting.  The transitions between these states are limited by the op amp slew rate.  I measured about 600 mV/µsec, which is what the MCP6002 op amp I’m using is specified to have as a slew rate (I measured before looking it up, to keep from being biased by the “correct” answer).

(clcik to embiggen) The negative input of the op amp (green), which the feedback circuit is trying to keep at Vthreshold, has glitches when the op amp output (yellow) is ramping between its two states.

The glitches in the improved circuit are smaller than for the simpler circuit, and can be further reduced by using Schottky diodes (to reduce the size of the diode drop, and hence how far the op amp must swing to change states) or a faster op amp (to reduce how long the op amp takes to slew the two diode drops).  I expect that with the Schottky diodes, the glitches should be reduced to 2(450mV)/(600 mV/µs)=1.5µs.  Since the glitches are basically triangular pulses, reducing the duration by a factor of 2–3 should reduce the amplitude by as much, and the total energy by 8–27.

To test the rectifier circuit with better diodes, I’ve ordered some 1N5817 Schottky diodes from Digi-key. I like dealing with that company, as they have a lot of components I need, are always very fast in processing orders, and have not yet messed up an order.  They were once out of stock on something that I had ordered, and called me up to apologize profusely for the mistake in their inventory database (normally they notify you before you order if something is out of stock).  For today’s order, they sent me notice that they had shipped the order less than an hour and a half after I had placed the order.  Because they offer first-class US mail as a shipping option, their shipping charges tend to be much less than most of the places I deal with.  (UPS ground is cheaper for big things, but no one is beating the Post Office prices on small lightweight objects.)

Disclaimer: neither Digi-key nor the Post Office has offered me anything for my endorsement—I’m just feeling pleased with them right now.

## 2013 July 16

### Precision rectifier

Filed under: Circuits course,Data acquisition — gasstationwithoutpumps @ 11:36
Tags: , , , , , ,

The log amplifier that I’ve spent the last several days understanding (posts a, b, c) is not the only non-linear circuit needed for a loudness detector.  We also need to convert the audio input signal into a slowly changing amplitude signal that we can take the logarithm of.  As I discussed in the first post on log amps, I had rejected true-RMS converter chips as too expensive for the application (though the original application has gone away), and decided to try a rectifier and low-pass filter.

[Incidentally, my son is now looking at a different processor chip, the KL25 from Freescale (an ARM Cortex chip), which has a 16-bit ADC that is much faster than the ATMega’s, so the entire loudness-detector could be done in software, except for a one-op-amp preamplifier.  With a  16-bit ADC, we can get almost 90dB of range without needing a log amplifier. We’re planning to order a development board that is compatible with Arduino shields (but has lots more I/O pins available) and that has an accelerometer on the board.  Amazingly, the development board is only \$13, about half the price of an Arduino.]

A single diode does not work for our rectifier needs in the loudness circuit, because diodes don’t turn on until the voltage across them is at least a “diode drop” (about 0.7v for silicon diodes and 0.45v for Schottky diodes).  However, the simple circuit for the log amplifier is also the circuit for a precision rectifier:

This circuit is both a log amplifier and a precision rectifier. If Vb is set to a constant voltage, then Vout1 varies as log(Vb–Va). Vout2 is max(Va,Vb).
The diode can be connected in the opposite direction, to get Vout2=min(Va,Vb) and Vout1 varying with log(Va–Vb).

The log-amplifier circuit I used in the previous posts had a transistor in place of the diode, so that the crucial voltage that was being exponentiated was referenced to the bias voltage, rather than the input. We also needed a compensation capacitor in parallel with the transistor to prevent oscillation in the negative feedback loop.

For the precision rectifier, we swap the inputs, so that Va is the signal input and Vb is a constant threshold voltage for the rectifier. We do not need (or want) the compensation capacitor, as that would cause the circuit to act as a unity gain amplifier at high frequency, rather than as a rectifier.

Because I did not happen to have any diodes, but had plenty of transistors, I experimented with the rectifier circuit using diode-connected bipolar transistors (collector and base connected together). Because the output of the rectifier is not directly driven by an op amp, I used unity-gain buffers to isolate the test circuitry (Arduino analog inputs or BitScope oscilloscope) from the amplifier:

Test circuit for low-speed testing of precision rectifier circuit. Here the NPN transistor is used as a diode, in the opposite direction as in the first schematic, so Vout should be min(Vin, Vbias)

My initial test setup did not have the unity-gain buffer for Vin, but I found that one of my Arduino analog input pins was quite low impedance and was discharging my capacitor. Switching to a different pin helped, but I eventually decided to avoid that kluge and use a unity-gain buffer instead.

I tried several different values for R2. The most predictable effect of changing the value is a change in the range over which the rectifier operates. Clipping occurs because the output of the op amp is limited to be between the rails of the power supply. The feedback transistor is conducting when the rectifier is following the input, so the op amp output has to be substantially lower than Vout.  The function implemented is max(Vclip, min(Va,Vbias)).  Vclip should go down as R2 is increased (at about 60mV for each factor of 10 in resistance—the same shift as in the log amplifier).

Here are a couple of plots of Vout vs. Vin for the S9018 transistor:

(click to embiggen) With a 1kΩ resistor, the clipping voltage is fairly high, and we have a somewhat limited range for the rectifier.  The offset voltage for the rectifier between the output and the input is much less than the resolution of the Arduino ADC (about 5mV).

(click to embiggen) With a 10kΩ resistor, the clipping voltage is lower, giving us more range for the rectifier.

Using a PNP transistor instead of an NPN has the effect of reversing the diode and producing Vout=min(Vclip, max(Vin, Vbias)):

(click to embiggen) With an S9012 PNP transistor and a 1kΩ resistor, we get clipping at the high end.

(click to embiggen) With a 10kΩ resistor we get a larger range.

So why not go for a very large resistor and maximize the range? From a DC perspective this looks like a win (though it is hard to see in the limit how Vbias would affect the result if the resistance went to infinity).  Of course, the problem is with high-frequency response.  Consider the difference between the S9018 NPN transistor with a 1kΩ resistor and a 330kΩ resistor:

(click to embiggen) With an S9018 NPN transistor and a 330kΩ resistor at 1kHz. Note the overshoot when the rectifier shuts off.

(click to embiggen) Fairly clean signal with a S9018 NPN transistor and a 10kΩ resistor at 1kHz.

(click to embiggen) With a 1kΩ resistor, there is very little overshoot as the rectifier turns off, but we can see a bit of a problem when the rectifier turns back on.

There is a problem with the rectifier turning on slowly, because Vout has to move all the way from the top rail down to the bias voltage, and the op amp has a slew-rate limitation. This phenomenon can be seen more clearly at a higher frequency:

(click to embiggen) The S9018 NPN transistor with a 10kΩ resistor and a 15kHz input signal. The overshoot as the rectifier turns off is about 50mV, and the turn-on delay is about 8µsec. The turn-on delay does not vary much with the input resistance, unlike the turn-off overshoot.

I believe that the overshoot as the rectifier turns off is due to capacitance, as adding a small feedback capacitor in parallel with the diode increases the overshoot substantially:

(click to embiggen) With a 33pF capacitor in parallel with the diode (an S9018 NPN transistor), a 10kΩ resistor, and a 15kHz input, the overshoot goes up to about 290mV from 50mV without the capacitor. The turn-on delay is masked somewhat by the high-frequency feedback.

Note: the S9018 has the best high-frequency response (if you consider 15kHZ high frequency) of any of the transistors I looked, probably because it has the lowest capacitance. For example, with a 10kΩ resistor the S9013 NPN has 120mV of overshoot at 15kHz, instead of only 50mV, and the S9012 PNP has -180mV. With a 1kΩ resistor, I can’t measure the overshoot on any of these three transistors. So the limited range with a 1kΩ resistor is compensated for by the much cleaner turn-off behavior.  I should be able to get better range by using a fast-response Schottky diode instead of diode-connected transistor.

Of course, the turn-on behavior is a bigger problem and one that can’t be fixed by playing with the resistor or the diode, because the problem is with the large voltage swing needed from the op amp in order to turn on. There are standard solutions that limit the voltage swing, but I think I’ll leave that for a later blog post.