All -
I am using a teensy microcontroller to modulate a -10 to +10v dc signal using an LM358 opamp in a circuit that @bbutcher designed and that has worked great. This is for a static Boeing 727 simulator.
I am having an issue where the voltage output from the opamp circuit is not linear ONLY when driven by the teensy. For example, when I drive the opamp on the bench using a good benchtop dc supply, a supplied voltage of 1.28v yields an opamp output of -2.211v, and a bench supply of 1.33v yields an opamp output of -1.96, all of which is appropriate and follows a linear output from -10 to +10 given a voltage of between 0 and 3.3vdc from the power supply.
However, when I drive the same opamp circuit using a teensy, 1.289 volts out of the teensy yields -2.175v from the opamp circuit and 1.313v from the teensy yields -0.745v out of the opamp, nearly a one volt difference from the output generated when the bench supply is delivering the voltage. The teensy uses PWM but I believe the benchtop does as well.
What am I missing here?
THANK YOU!
Hi Jon,
The usual method of getting a variable DC is to use a resistor-capacitor combination on the PWM output to get an average DC value.
Several things can go wrong. The concept is that the capacitor will charge to the average value of the input. For example if the PWM goes from 0 to 3.3 volts with a duty cycle of exactly 50%, the capacitor will charge to 1.65V. It is assumed that the PWM signal is continuous, but the duty cycle changes to get different voltages. One possible problem is that the PWM may not actually swing from 0 to 3.3 volts. The low value may be perhaps 0.3 volts, and the peak value may be 3.15 volts for example due to internal components in the Teensy. This will affect the average value of the voltage at all duty cycles, and will affect both the high and low values.
The voltage will not change instantly when the duty cycle is changed. If you change the duty cycle and do not wait long enough to measure the result, it will read lower than it should. Here is an example of a PWM to DC converter. This assumes the PWM is a low frequency, 10 Hz. The voltage vs time plot is:
There is a time constant associated with resistor capacitor circuits, namely the product of R*C. If the resistance is in ohms and the capacitance is in farads, the time will be in seconds. For the circuit shown this would be 300,000 * 0.00001 = 3 seconds. The capacitor voltage will rise exponentially towards its final value and it is customary to use 5 time constants which will result in the voltage being 99% of its final value. In this case you would need to wait 15 seconds before measuring the voltage. You could use a smaller resistor or smaller capacitor to reduce the time constant, but that would increase the ripple (peak to peak variations) of the signal, leading to a variation in voltage depending on when the voltage was measured. The solution to that problem is to use a higher frequency on the PWM. Increasing it to 1000 Hz for example would allow using 100 times smaller time constant, perhaps 30K and 1 uF for example with the same ripple, but requiring only 0.15 seconds before measuring the voltage.
I suggest that you make a series of measurements of voltage at various duty cycles and plot the data to see what is going on. If the voltage varies with repeated measurements at the same duty cycle, you need a longer time constant (ripple problem).
If the voltage measured is consistent, and the line is straight, you have an offset problem. If the line curves, you have a linearity problem. You can compensate for either of those by using a look up table to adjust the duty cycle as needed.
Bob
Bob - As always...eternally grateful for your input and advice.
I did some measurements on this circuit:
First, I ran it with output from the benchtop power supply, a Siglent SPD3303C, escalating from 0 to 3.3v in 0.1v increments. Then I did the same test using the Teensy, one run in 0.1v increments in 8bit resolution and the other in 0.1v increments in a 12bit resolution. It turns out that the PWM frequency changes with the resolution of the output, with higher frequency at lower resolution. For my application, I prefer 12bit resolution. At 8bits, the frequency is 585937Hz, whereas at 12bit resolution that drops to 36621Hz. Regardless, the results were largely the same:
The blue line represents the input voltage. The red line represents the response from the siglent, the yellow line is the voltage response with the teensy set to 8bit resolution and the green line the same at 12bit resolution. Certain voltages are unattainable, for example -1.7 volts. No amount of resolution or tinkering gets that response.
Any ideas?
Jon
Hi Jon (@jonweisw) & Bob ( @bbutcher85),
I was surprised to see the 'kinks' in the plot, but I probably haven't used the LM358 in a split supply situation either.
I am totally at a loss to understand why the dual op-amp circuit would behave differently when driven by the Teensy, vs the bench top supply. The bench top supply would probably use a buck regulator, which although a buck regulator involves PWM, the effect of the additional inductor(s) and capacitor(s) intrinsic to a buck regulator, the resulting output should be a steady DC value ... not the 'raw' square wave of the PWM output from a microcontroller. The discussion already offered of using a filter, such as the RC filter suggested by Bob, is obviously a valuable starting point.
------------
However, I had a quick look to see what TI, one of the main manufacturers listing the device, had written about it, which left me with additional concerns. I am unclear whether these concerns would only cause a small discrepancy from the desired behaviour, or if the PWM input is 'enhancing' the problem discussed below.
In particular, Application Note AN-116 from
https://www.ti.com/lit/an/snoa662b/snoa662b.pdf
Section 7, on page 5 starts:
7 Balanced Supply Operation
The LM358 will operate satisfactorily in balanced supply operation so long as a load is maintained from
output to the negative supply
then confusingly (to me at least), shows a number of schematics with single supply ...
but starting at the top of the next page (6), there is a split supply schematic, followed by some interesting text ...
Balanced Supply Operation www.ti.com
Figure 5. Split Supply Operation of LM358
The output load to negative supply forces the amplifier to source some minimum current at all times, thus
eliminating crossover distortion. Crossover distortion without this load would be more severe than that
expected with the normal op amp. Since the single supply design took notice of this normal load
connection to ground, a class AB output stage was not included. Where ground referenced feedback
resistors are used as in Figure 5, the required load to the negative supply depends upon the peak
negative output signal level desired without exhibiting crossover distortion. RL to the negative rail should
be chosen small enough that the voltage divider formed by RF and RL will permit Vo to swing negative to
the desired point according to the equation:
RL could also be returned to the positive supply with the advantage that Vo max would never exceed (VS+ −
1.5V). Then with ±15V supplies RL MIN would be 0.12 RF. The disadvantage would be that the LM358 can
source twice as much current as it can sink, therefore RL to negative supply can be one-half the value of
RL to positive supply.
The need for single or split supply is based on system requirements which may be other than op amp
oriented. However if the only need for balanced supplies is to simplify the biasing of op amps, there are
many systems which can find a cost effective benefit in operating LM358's from single supplies rather than
standard op amps from balanced supplies. Of the usual op amp circuits, Table 2 shows those few which
have limited function with single supply operation. Most are based on the premise that to operate from a
single supply, a reference VQ at about one-half the supply be available for bias or (zero) signal reference.
The basic circuits are those listed in AN-20.
I confess I haven't spent any time to check whether this is consistent with the symptoms you describe, or considered the best course of action. Although crossover distortion is a well known problem in other contexts, such as Class B audio amplifier output stage designs, this is the first time I can recall seeing it discussed for an Op-Amp. Perhaps, Bob, you are more familiar with this situation?
I guess, it should be easy to try adding an extra load to each of the op-amps, connected to the -15V rail, to see if the problem disappears.
Alternatively, maybe look for an alternative op-amp.
Sorry, this is only a 'Could this be the problem?' diagnosis ... and apologies if it is a red-herring.
And sorry, I am not proposing a definite fix.
But I am hoping it will be a step in the right direction.
Best wishes, Dave
@DaveE-
Thanks for the advice and for looking into this. TBH, it’s a little beyond my understanding and I’m looking forward to Bob’s response. For what it’s worth, I’ve tried this circuit with an OP2134 and had the same result. I still don’t understand why when the signal is fed to it from the bench power supply the result is perfect, yet it is different and distorted when the teensy supplies it. Makes no sense to me.
If there’s a good drop in Dip-8 version that solves this problem I’ll order it right now!!
Jon
So on the Teensy forum someone posted this link to a TI document that discusses this problem...
https://www.ti.com/lit/pdf/sbaa603
Very useful. Basically, it recommends a low pass filter over the first stage of the opamp over the feedback resistor. In this case, based on the 23.9k resistor and 36600 Hz (which is the frequency at 12 bit resolution) the capacitor works out to 180pF. Ordered it but in the meantime went ahead and bridged it with he lowest one I had lying around - 10nF - and viola - problem solved! See graph - the green and yellow are without the capacitor and the orange represents the response with the capacitor in place. Almost indistinguishable from the benchtop supplied response.
Thank you for all your help. This forum is truly invaluable.





