# Designing a Triangle/Square wave VCO part 1!

Its rare that I ever really dabble in any form of analog circuit design nowadays but I was a bit bored at work during my break and decided that it’d be fun to try and make a triangle/square wave VCO. I’ve been studying a lot about PLL’s lately and VCO’s are extremely important in the working of them so I had been having a go at designing them and eventually I got to here!

Theoretically, creating a triangle wave is somewhat simple. A triangle wave for those who don’t know consists of two edges, a rising edge and a falling edge, looking somewhat like: It might sound relatively easy but other than resistors, most electronic components do not follow linear rules. The only linear component I can think of is the current through an inductor with a constant voltage. Sudden changes in current cause massive changes in voltage which can disrupt electronic systems, not forgetting that inductors are generally large components!

A lot of the simple components within electronics follow some form of exponential law. For example, the simple diode or transistor has an exponential current response for a linear voltage. A simple RC circuit also has a reverse exponential current response and an 1-exponential voltage response with a constant resistance and voltage.

So how does one create a linear ramp without consulting the internet and off the top of their head? Well if you cast your mind back to probably GCSE or A Level physics, you will remember that Q = IT and Q = CV. These are two very important equations stating that charge is equal to the current multiplied by time and charge is also equal to the capacitance multiplied by the voltage. Equating these two equations leads to the equation IT = CV. Some simple rearranging of this equation making V the subject leads to the equation V = IT/C. As you can see here, with respect to time,the voltage is proportional to the current and inversely proportional to the capacitance. This makes sense as for a given current, a large capacitor will take longer to charge as opposed to a smaller capacitor. This however also proves a vital point of my triangle wave generator: If you keep the current and capacitance constant, V will linearly increase with time. I hope that this point rings home because this in its self is a brilliant aspect of an ideal capacitor! If you apply a constant current across a capacitor of any value, the voltage will linearly increase with time until limited by the maximum voltage applicable by the current source. Go on! Test it in any simulator and you will see this relationship! Infact, you don’t have to because I’ve done it for you. In this whole document, I will be using my beloved LTSpice (http://goo.gl/1FTdd) which is both free and really easy to use. As you can see, A 1A current source will charge a 1F capacitor to 1V in 1s, as defined by V = IT/C (1 = 1*1/1). To run this simulation, ensure the initial operating point solution is skipped, otherwise the voltage across the capacitor will already be equal to the maximum achievable voltage by the current source! This is defined by the uic command.

So how do you turn this into a triangular VCO I hear you say? Well lets start at the basics. If we can charge and discharge a capacitor with a constant current, with a constant time period between charging and discharging, we will get a triangle wave who’s voltage depends on the time period (longer time period = longer time charging = larger voltage swing). That’s no good! A VCO should have a constant output voltage regardless of frequency so that option is no good!

Instead, cast your eyes back to your potentially amateur electronics days using a 555 timer. A 555 timer is an utterly brilliant device which contains two comparators, a set and presettable flip flop, along with various other gubbins like the 3x 5k resistors in series and a discharge transistor.

The magic of the 555 timer comes across when using it like a schmitt trigger. By connecting the trigger and threshold pins, the 555 timer will act as an inverting schmitt trigger. What this means is that when the voltage present on the trigger and threshold pins goes above 2*Vs/3, the output will go low and remain low until the voltage present on the trigger and threshold pins goes below Vs/3 Once the voltage goes below here, the output will go high and remain high until the voltage on these two pins goes above 2*Vs/3. This as you can see is a schmitt trigger as the output will not change if the voltage is < 2*Vs/3 and > Vs/3. For your eyes, I’ve simulated this circuit too! As you can see, with a sine wave between 0 and 5v present on the trigger and threshold pins of the 555 timer in schmitt trigger configuration, the output only changes once the Vs/3 and 2*Vs/3 bands are passed where Vs is 5v too.

It is this beautiful little chip that allows my triangle/square VCO to work.by utilizing this schmitt trigger behaviour and by knowing the switch points, I can use this data to design my VCO.

Now in a stock 555 timer oscillator circuit, the capacitor is repeatedly charged and discharged with a constant voltage and resistance. This unfortunately means that the voltage across the capacitor will be 1-exponential during the charge period and reverse exponential during the discharge period. This doesn’t have the lovely odd harmonics that a normal triangle wave does and will merely sound like a heavily filtered square wave, yuck! For the ease of imagining this waveform, I have simulated this too. As you can see, the capacitor voltage bounces between the two voltage ranges. With a 5v supply, these ranges are 1.666…v and 3.333…v (ellipses representing infinite places). You can also see the non linear charge and discharge curves of the capacitor as when the output is high, the capacitor is charged through R1 with a constant voltage and when the output is low, the capacitor is discharged through R1 to ground.

So how does one solve this problem then? The capacitor is charging and discharging alright but not linearly! Well, you can utilize the two voltage switching points of a 555 timer to change the direction of a constant bidirectional current source/sink with respect to the capacitor. As you can see above, the output changed from high to low once the capacitor voltage reaches 2*Vs/3 and changes from low to high once the voltage reaches Vs/3. This means: when the 555 output is high, the bidirectional current source should be pumping current into the capacitor and when the 555 output is low, the bidirection current source should be sinking current from the capacitor. This all seems somewhat trivial as proven earlier, if you charge and discharge a capacitor with constant current, the voltage across it will linearly increase with time. So the problem faced here now is how on earth do you get a bidirectional current source/sink?

One of the most used circuits in analog circuitry is the bipolar junction transistor current mirror (http://goo.gl/ioZvNF – I won’t go into the massive detail here on the workings though I will explain partially within the mathematical explanations). A BJT current mirror can both sink and source current dependent on whether you’re using a PNP or NPN product and which way it is wired up etc. I simulated both a BJP current sink and current source with the load as a capacitor so you can understand how the capacitor voltage reacts to the current. Using two transistor current mirrors in a sinking and sourcing manner can charge a capacitor with a constant current linearly with the current dependent on the current flowing through the resistors R1 and R2. Sorry about the behavioural voltage source producing the voltage CapV_Source, I feel a bit stupid because I don’t know how to set initial circuit conditions! Initially, C1 should be wired differently and have a voltage a 5v across it so you can see it discharge at a constant current but nevermind! As you can also see, the maximum voltage is limited by the supply voltage (5v).

So as you can see, transistor current mirrors can be bidirectional depending on if its based on NPN or PNP transistors. NPN transistor current sources can sink current and PNP transistor current sources can source current. Sorry about the usage of the name “current source” it probably seems confusing that a current source can source or sink current, I just can’t think of a better name!

So far: We have a means of charging and discharging a capacitor between two voltage points thanks to the 555 timer. We can make a bi-directional current source using BJT current mirrors. A constant current through a capacitor will produce a constant voltage with respect to time as defined by V = IT/C.

Another cool thing about current mirrors is that the current through the current mirror is proportional to the resistor current. In a NPN BJT current sink, the current will be dependent on the voltage across the resistor, divided by the value of resistance (as defined by ohms law – V = IR, I = V/R). By redrawing the circuit with just an NPN BJT current mirror, I can change the current through the current source by varying the voltage across the current setting resistor. As you can see, I’ve added a sine wave voltage source to the other side of the resistor terminal as opposed to tying it to V+. The sine wave produces a voltage between 0.5v and 4.5v. I purposely kept it 0.5v above ground as this ensures that the transistor Q1 will always be above the forward biased junction voltage, meaning current will always be flowing through R1. At small currents, the forward biased junction voltage will be approximately 0.57v for the BC337 in the simulation here. It is this approximation that later on will increase the error in the output frequency calculated compared to experimentally obtained and I shall explain this later.

So to add to the previous conclusions, the current through the current mirrors can be varied by varying the voltage across the current setting resistor. As stated earlier too, the rate at which voltage linearly increases across a constant valued capacitor is dependent on the current. A larger current will cause the capacitor to charge faster and a smaller current will cause the capacitor to charge slower. It is this phenomenon that will be used to create a constant voltage output across the capacitor, independent of frequency by just changing the current at which the capacitor is charged and discharged. For those of you who are fast minded, charging and discharging at different rates will change the slew rate of the circuit, allowing the VCO to also produce sawtooth waves by merely setting the charge and discharge current differently!

So now we have gone through the logistics of using current mirrors, you’re probably asking how a current mirror can can both sink and source, to which I say its simulation time! A bi-direction current sink/source/mirror… whatever!

Whew! I know its a pretty complex looking circuit but its relatively simple really (Honest!). If you look to the left, there is a square wave voltage source producing 5v when high (supply voltage) and 0v when low (ground voltage). As you can see, if the square wave voltage source is high, no current can flow through R1 is the voltage difference will be V+ – Vsqr (the transistor isn’t forward biased so no voltage difference is present across it), or in laymans terms, 0v! This says that the current flowing through the top current mirror is also 0A as current can’t come from nowhere. As the square wave voltage source is high, current can now flow through the bottom current mirror as the voltage across the bottom resistor R2 is VSqr – VFwd(Q2) which is roughly equal to a current of (5-0.6)/20k = 220uA.

Conversely, if the square wave voltage source is now low, no current is flowing through R2 and current can now flow through R1, disabling the sinking current source and enabling the sourcing current source. The direction of the current source is now defined by the voltage present at this resistive point between R1 and R2! For future reference, the voltage at this point needs to be pretty low impedance and needs to have fast switching times to ensure that both current sources transition from sinking and sourcing relatively fast.

So now that we have designed a bi-directional current source/sink, you may be asking how we make the current through the capacitor variable! Now this took a bit more thought. So far we can conclude with this circuit: The direction of the current is dependent on whether the top or bottom current mirror is enabled. If the top is enabled, current will be sourced. If the bottom is enabled, current will be sinked. Enabling the sources can be done by modifying the voltage across the current resistor though a variable voltage across this current resistor will also vary the current delivered to the load.

With these conclusions, how does one manage to be able to change the current direction and vary the overall current sourced/sinked by each section? This was the part that required a bit more thought. I came to the decision that the voltage across the resistor needed to be varied to be able to vary the current, therefore directly affecting the rate at which my capacitor will charge. To sort out the directional problem associated by a non constant voltage, across these resistors, I used another approach. It came to me that if I tied the bases of the bottom current mirror to ground, I could essentially disable it and only source current. The same applied if I tied the bases of the top mirror to V+. This solved my problem! By adding two extra transistors, I could vary the current through the current mirror and switch the direction of the current.

As with every circuit I’ve mentioned, I think a simulation is in hand! Before I get to that however, I need to stress an important point with the variable current control. The current through the current sink (NPN BJT current mirror) is roughly equal to: (V(R2)-0.6)/R2, which is relatively simple. The same however isn’t true for the current source (PNP BJT current mirror) where the current is equal to (V+ – 0.6 – V(R1))/R1, meaning that the voltage will need to be inverted for a given current! Let me explain this practically:

Say you want a current of 100uA to be sinked and sourced, working out the current sink first, with a constant resistance of 10k, the voltage present at the point between R1 and R2 will need to be: 20k*100uA – 0.6 = 1.4v. This however will not mean that the current source will source this same current. Calculating the current through the source will be: (V+ – 0.7 – 1.4)/20k, assuming V+ is 5v, this current will be: 2.9/20k = 145uA! This will not cause a symmetrical wave as the capacitor will charge nearly 1.5x faster than it will discharge! The way to solve this is to reflect the voltage present across R2 and ground, around the V+ midpoint and have this present at the non transistor side of R1 (this will make more sense in the schematic!). Therefore, if you have 1v present at the non transistor side of R2 and V= is 5v, the voltage at the non transistor side of R1 will need to be 4v (5v – 1v). When I said it would be easier with a schematic, I did of course lie! Its hard to see what’s happening here realistically. You can see that the VSource is equal to V+ – VSink as explained above ans you can see that for a constant switching rate (defined by CurrentMirrorDir), changing the current changes what voltage the capacitor charges and discharges to. This won’t make too much sense as the current mirror direction is not related to the current voltage across the capacitor as it is in the final version.

So, you can see the problem with having a switching voltage unrelated to current capacitor voltage: it produces a completely incomprehensible trace like shown here! The voltage across the capacitor is all over the place. However what the above simulation DOES prove is that you can use my funky additional two transistors to set the direction of the current source! Then the furthest top PNP transistor is pulling the top current mirror bases close to V+, this current source is disabled and current is sunk as opposed to sourced. When this PNP transistor is disabled by bringing its base up to a V+ potential, the top current mirror returns to normal operation and the bottom current mirror is disabled at the transistor right at the bottom of the schematic is enabled, pulling the bases of the bottom current mirror down to ground.

Finally! The moment you’ve all probably been waiting for, the final circuit! I’ll get the picture of the full schematic before the explanation. Tada! The final circuit, integrating the bidirection current source/sink, a control voltage inverter (the op amp to the right), along with the 555 timer in schmitt trigger configuration. The circuit outputs both square and triangle waves as can be seen on the left.

Wahoo! If you’ve managed to read this far down (or skipped the theory), you can see the final circuit! Its probably a lot more complex than other circuits on the internet but if you’ve read through the theory of how it works, its really quite simple and the math for calculating the frequencies works out really nice because it can all be crudely assumed as linear. To get a really accurate estimation of the frequency, the shockley equation needs to be taken into consideration to calculate what the voltage across the resistors will be with respect to the voltage present at the control point (V2 in the above schematic). The main frequency range is depicted by the capacitor, C1, and the current set by the resistors R5 and R6. The symmetry of the triangle wave is depicted by the ratio of R5 and R6. Theoretically, if you set R5 to 0R (DO NOT do this in real life as it will short your voltage source), you will get a rising sawtooth wave. if you set R6 to 0R, you will get a falling sawtooth wave. This is not practical and small resistors (100R or so) can be used instead of setting directly to 0R.

As with everything, I’ve simulated this circuit. The control voltage varies from 0.5v to 4.5v. In the simulation traces, you can see that the voltage across the capacitor is constant at the 555 timer output only changes once the capacitor voltage has reached either 2*Vs/3 or Vs/3. As can be seen, the control voltage is the sine wave and the frequency of the triangle wave changes with this voltage.

As you can see however, these current sources/sinks aren’t perfect and at low voltages and therefore low frequencies, the output isn’t completely linear. Once the math has been covered, the relationship between voltage and frequency can be established. To do this, I will be using the excel curve fitting tool to create an equation to calculate the output voltage without having to do the chore of calculating total rise and fall times etc.

That however is for part two! I hope you’ve enjoyed/learned at least something useful in this ramble of mine.

EDIT: After posting this blog on the EEVBlog for some feedback, it was made clear to me that I should include emitter degenerating resistors to help with balancing non-matched transistors so I’ve modified the schematic and here below is the new one! (Thanks to Jay_Diddy_B) The newly modified circuit with emitter degenerating resistors R7 and R8.