# Computing stuff tied to the physical world

## Triangular waves

In Hardware on Oct 7, 2012 at 00:01

For an upcoming experiment, I’m going to need a slowly rising voltage, and with my signal generator currently out for a check-up, it’s time to dive into some electronic circuitry again. Let’s try to generate a sawtooth or triangular wave signal with a few basic components. After my search for a simple sine wave generator, I’m happy to report that generating “ramp voltages” is actually a lot simpler.

The reason for this is that all you need to generate a linear ramp, is a constant current into a capacitor. This automatically produces a linear voltage ramp. The electrical notation for such a circuit is:

The voltage over that capacitor will rise linearly over time. It looks so simple! (well, apart from figuring out how to build a constant current source, perhaps)

But that’s only half the story. What to do once the capacitor has been fully charged up? We need to discharge it again, clearly. One way to do this, is to put a transistor or MOSFET across the cap and periodically make it turn on (briefly!) to discharge the circuit again. Ok, so now we need a periodic pulse as well. Hmmm…

There is another solution: op-amps. The op-amp is a truly amazing little building block. What we need here, is to use two op-amps in different configurations: one as an integrator and one as a comparator.

Let’s start with the integrator, because that explains how we can get a linearly varying voltage out of the circuit:

An op-amp has two properties, both crucial for explaining this little 3-component circuit:

• both inputs are very high resistance, so virtually no current flows in or out
• the output is constantly adjusted to try and keep both inputs at the same voltage

That second property can also be described as: when “+” is higher than “-“, the output will go up (towards Vcc, the supply voltage), when it’s lower, the output will go towards 0 (the ground voltage in a single-supply setup).

Here we’re tying “+” to half Vcc, in this case 2.5V, so you can think of the op-amp as trying to do whatever it can to keep the “-” pin at 2.5V as well.

Let’s start with everything in perfect balance, then “+”, “-“, and “OUT” will all be at +2.5V, and the capacitor will have no charge. Now let’s take the input to +5V:

• a constant current starts flowing into the resistor, since the other side is +2.5V
• this current drives the “-” pin up, so the output will go down
• how far down? well as much as needed to cancel out that incoming current
• IOW, the same current is going to go into the capacitor, which starts to charge up
• as the charge builds up, less current starts to flow into the capacitor
• “no way” says the op-amp, and so it pulls its output lower
• so a constant current flows into the cap, and the output drops lower and lower
• at some point near 0V the op-amp reaches its limit, and the mechanism breaks down

To give a water analogy: think of pouring water into a glass at a constant rate while trying to keep the surface of the water the same. You have to gradually lower the whole glass to make this work. As you do, the glass (cap) will contain more and more water (voltage). You’ve integrated (collected) the water flow into the glass!

If we keep the input voltage high, nothing more will happen: the cap will end up being fully charged, and the op-amp can no longer do anything to keep the “-” input from rising all the way to the input voltage.

When we now drop the input voltage to 0V, the reverse happens. Current flows through the resistor in the other direction, and the cap starts discharging. Again, the op-amp will do whatever it can to maintain that “-” at 1/2 Vcc. It does this by raising its output pin causing the capacitor to discharge with that same constant current rate. And sure enough, the voltage over the capacitor drops linearly. Until we hit the limits, and the process stops.

Here’s the effect in action, as seen on an oscilloscope (R = 4.7 kΩ, C = 0.1 µF, and the op-amp is an OPA2340):

The blue line is the input signal, the yellow line is the triangular wave output. Neat, eh?

Tomorrow, I’ll add an op-amp as comparator to make this circuit oscillate all by itself.

1. [Bragging mode=on] have a look at my posting http://forum.jeelabs.net/node/1207 it might be a useful alternative approach for generating slowly varying signals. [/Bragging]

• Ah, thanks. that looks interesting. I’d like to try this soon, but note that PWM-generated analog signals always have a bit of a ripple. Need not be an issue here, and it’s definitely a very neat way of generating such signals under s/w control. It’s be neat to create an “arbitrary waveform generator” based on the Slave Plug. It’d be even neater if we can make it listen to I2C so that we could set some parameters on the fly.

2. Interesting output blip on the slope change cusp. Symmetric, matching the input trigger transition. Can’t Can be slew rate limiting, possibly ground bounce.

Non-ideal cap with some inductance? Hmm – long path to reach it on the breadboard? The op amp is fast enough to reverse the current in C very quickly, but any series inductance can only protest by creating an opposing V until the stored energy hits zero.

Comments are closed.