# Computing stuff tied to the physical world

## Pretty good triangles

In Hardware on Oct 9, 2012 at 00:01

As of yesterday, we now have a “triangular wave generator”. Whee!

Except that I don’t want a voltage between 1.25V and 3.75V but slightly lower. One way to accomplish this is to lower the reference “1/2 Vcc” voltage used by the comparator and integrator circuits. So I added a 22 kΩ resistor in parallel on one half of the voltage divider:

``````+5V  <=>  10 kΩ  <=|=>  (10 kΩ and 22 kΩ in parallel)  <=>  GND
|
TAP
``````

This online calculator says that the parallel value is 6.875 kΩ. Here’s the resulting signal:

It’s now slightly asymmetric (we’re discharging faster than we’re charging), but more importantly, the signal now runs from about 0.6V to 3.0V, which is more in sync with what I need (more on that in an upcoming post).

Notice that on these screen shots, the waveforms look very nice and straight, although it’s hard to see just how linear those ramps really are.

This is where a scope with good math functions comes in. If you recall from mathematics, the derivative of a straight line is a constant. Or to put it differently: the straighter the line is, the closer its derivative should be to a constant value. Positive for upward slopes, and negative for downward slopes. Let’s zoom in a bit:

(the red line’s origin is centered vertically, the yellow line is at 1 division from the bottom)

That red line is the scope’s calculated derivative of the yellow line (it’s really just a matter of calculating differences between successive points). As you can see, the upward slope is pretty straight from 1.3V to 2.9V. The downward slope less so, IOW the capacitor discharge is not quite as linear. The signal was averaged over 128 samples in this last screen.

Excellent. I now have the signal I need to perform my experiment. Stay tuned.

1. Aha – the differential tells the story. Well actually two. Note the scale for signal MA1 – 2kV/division ! That’s because the output is changing through several volts in hundreds of uS – measuring the ramp by eye, the expectation is 3.4KV/sec for the upslope and a larger negative value for the downslope.

Switching to more usual units, that is ~5V/uS rate of change, or “slewing”. But that chip is only spec’ed at ~6V/uS – the limiting can be seen at the positive to negative slope transition. The ideal waveform there has a point of inflection (the first differential is heading off to infinity and oblivion) which any practical amp can only approximate by ‘ringing’ it’s output. The most likely cause of that little yellow whisker.

The second part? Notice the behaviour of MA1 near the transition, bumbling along at ~3.6 until an upward, then downward off-the-screen spike just before the slope transition. How can that be? As JC alludes, the differential estimate is some numerical processing of successive samples. Several successive differences with weightings are used to damp out noise, but typically these are forward difference formula to be responsive to up and coming events.

Unfortunately, as a side-effect, they are sensitive to large changes just ahead – and that is what we have. The little yellow whisker is fooling the differential estimator into reporting a fraction early.

• Wow, delightful insights – thanks, as always, for explaining the deeper stories!

• Thanks martynj – like jcw said, your comments and remarks are highly appreciated. I’m currently low on “round tuits”, hopefully getting a fresh supply this autumn/winter – can’t wait to spend more time with my JeeNodes and some analog electronics…

2. You can end up with a passionate love/hate relationship with op-amps. Love the simplicity and elegance of building quite complex functions with them, but then hate the divergences from ‘ideal’ that can really take you by surprise.

Who would have thought that a 5.5MHz device hits an edge of its performance envelope creating a 1kHz waveform?

3. Curious about what the causes of the noise in the slope plot is, and how it could be reduced. Was thinking somehow it ought to be smoother, maybe with some nonlinearity but not as much noise. Would be interesting to compare to something like an AD9833 generator, eg. http://dangerousprototypes.com/2012/10/08/usb-function-generator/

4. @JBeale, yes – puzzling. Quantizing error from the ‘scope ADC’s should smooth out with #128 sample averaging. The switching waveform has some fuzz that will feed through, but surely not periodic.

The mismatch between upslope and downslope performance is I think the clue. The non-linearities of the class B totem-pole complementary output stage are usually masked by strong negative feedback. Here there is none; the op amp is operating open loop.

Perhaps a high value R (or T equivalent) across C will tame the beast without bowing the ramp too much? Also, let’s not forget that C is not ideal either. Depending on the dielectric, actual capacitance can be a strong function of applied V. Those little cheap chaps from China are meant for rail decoupling, not precision timing circuits!

5. P.S. Interesting to keep integrating – integral of a triangle is hyperbolic, integrate again and its not a bad approximation to a sine wave… Low pass filter et voilà, a synthesised signal generator, all in analogue.

• A capacitor integrates, which is why an RC filter does that too. Multiple filters in series attenuates harmonics, until only the base sine wave remains. It all clicks together :)