Monday, July 29, 2013

Comparators Are Awesome

Ever wonder why people like comparators so much? In various applications they are useful, but being a synth blog, here are some neat ways to use comparators in a synthesizer. BUT FIRST...

~~~WTF is a Comparator?~~~

Simply put, a comparator compares two voltages, an input and a reference voltage. If the input is less than the reference, the comparator will output a voltage near one of its supply voltages. Usually this is performed by an operational amplifier, and the inputs can go in the negative or positive inputs. For our uses, you'll want the reference voltage going to the negative input, and you will want to power the opamp with +-5V.

Now, on to synth uses:

~~~Simple Square Oscillator~~~

You've seen me post about this before, but mate, it's useful. A basic Operational Amplifier oscillator is really a type of feedback-driven Schmitt Trigger. Here is the schematic and output (note that the cap is the general frequency range and the 100k resistor can be varied up or down, I usually use a 1M pot):

~~~Square-Saw-Tri-Ramp-Sine Generator~~~

Now, using the above oscillator, we can derive a few other features using a comparator after an RC lowpass (or highpass, but I'll be using a lowpass) filter. First, filter the output with an RC filter, like this:
The exact resistor value will vary from 10k depending on your frequency range, but make sure you get a generally triangle-like waveform along your whole frequency range. Then, set up a comparator after the filter with a voltage high enough that it only switches near the top of the triangle wave:
And then put another RC filter after that:
You may need an amplifier and DC decoupler stage after this, but once you do you get a saw wave. Now, this section says "Square-Saw-Tri-Ramp-Sine", yet all we have now is square, saw, and triangle. How do we get ramp and sine? Well, one way to get the ramp is to use 2 comparator/filter stages, the second being set to the equal but negative reference voltage as the first. Another is to simply vary the reference voltage, which will sweep the waveform from saw to triangle to ramp (which, by the way, is WICKED FUN).

As for sine, you can simply put a second, low-cutoff filter and amplifier after the triangle wave stage. It won't be perfect, but it will be similar enough for musical purposes... usually. Bear in mind that certain stages may require amplification, decoupling, or buffering, depending on exactly how you process the signals.

~~~Pulse Width/Modulation~~~

I suppose we should start with a couple definitions. Pulse width (also known as duty cycle) is the ratio of time between when a square wave is high or low. For example, a normal square wave is 50% pulse width because half of the time it is high, half of the time it is low. Pulse width modulation is the use of a modulation source to vary the pulse width dynamically.

Now, when I do PWM stuff, I use saw waves for easier control of the pulse width due to having a longer length of wave (from lowest to highest peaks) to work with. For the following pictorial examples I will be using the saw waveform generated using the above generator. All you do is put a comparator (opamp) after it and vary the reference voltage to the comparator.

~~~Unique square waveforms ~~~

If you decide it would be fun to put a comparator on a wavefolder, such as the Lockhart wavefolder, you're in luck. The following image is not mine, however the basic premise is easily replicated:
As you can see, a sine wave (first image) fed into the wavefolder does as you would expect: fold the waveform over itself (second image down) However, adding a comparator to the output (Vref is ground) gives you a unique square waveform. You can alter the reference voltage to get even more unique results, such as PWM. Very fun.

~~~Gate signal from Keyboard/Sequencer~~~

It is always useful to have a gate signal from a keyboard or a sequencer. And, I'm sure you've already figured out how to do it, but if you haven't, it's simple: on the keyboard voltage output, put a comparator referencing ground. If you play legato, you will have a constant gate signal, but if you separate your notes (always a useful thing to learn how to do), you will get a separate gate signal for each note. Cool, huh?

~~~Gating~~~

Sometimes, you need a noise gate or similar system. This is also pretty easy to do:
If you follow the schematic, know that I threw in arbitrary values and your needed values will vary. If you don't want to follow the schematic, don't worry. Here's how it works: Audio splits from the input to a VCA and a diode. The diode cuts out the negative voltages from audio. The diode is followed by a lowpass filter, which smooths out the signal, thus making it easier for the comparator to create a stable gate signal. The comparator (Vref is variable as to compensate for noise) outputs a high gate signal when audio passes and a low gate signal when audio is below the reference voltage. That gate signal feeds the VCA, which turns on and off the volume. Yaaay, gater.

Of course, there are more uses than just these. Play around, see what you end up with!

Wednesday, July 17, 2013

Simple ASR

Firstly, THANK YOU! Today this blog broke 18,000 views. That's pretty epic considering what this blog is about. Now on to the subject at hand...

So in my random bout of wanting to solve electronics puzzles at 3am (which I am finding is happening a lot recently), I did something neat and simple: made a simple, no-nonsense ASR envelope generator. I'll give credit where it's due: this is VERY loosely based on not liking how complex Hex Inverter's Postman EG is and thinking of ways to make it simpler. Simple is kinda what we do over here, if you haven't noticed.

Alas, it turns out there aren't many ways to simplify the Postman EG, mainly because it's already a fairly simple device. So instead, I just standardised some values (aka parts I have on hand), removed features that don't really need to be there, and adjusted things to work right with as few parts as possible, yet still giving the features I wanted. So, I came up with this:

Gate in, Attack, Sustain, and Release controls, LED indicator, and normal and inverted outputs. It doesn't get much simpler. And really, you could even ditch the Sustain control, LED, and inverter, and just end up with a REALLY simple AR generator. Or, you could add a manual gate button, maybe a loop function (pretty simple, just put a comparator and inverter at the end and connect that output to the Gate input), whatever you want.

Just bear in mind that this ASR is fairly well tuned, so modifying it may result in you needing to make a few other changes. But, hey, isn't that why we are in the DIY synth gig anyway?

Wednesday, July 10, 2013

Transistor-Output Optocouplers for Voltage Control

I once took apart an old computer monitor (like the ones with CRTs for screens). Among like a thousand other parts, I got 3 Sharp PC123's - that is, transistor-output optocouplers. With a bit of fiddling, I learned that these can be used for voltage control.

If you do not know, this is a transistor-output optocoupler (well, the inside of one):
How it works is this: you apply a current (voltage through a resistor, because resistors are voltage-to-current transformers) to the anode of the LED, and you ground the cathode. This is how you would normally power an LED. The base of the transistor is light-sensitive, and just like applying a voltage to the base of a normal NPN transistor, the collector and emitter act like a directional variable resistor. I say directional because the direction of the voltage going through the transistor has to be moving from collector to emitter (follow the arrow) to properly act as a resistor. I've never had much luck going the other way.

Now, why use this over a normal NPN, or even a FET? Well, mainly because I can never get transistors to work right, but also because optocouplers prevent high voltages from destroying receiving circuit. And, given that you are controlling an LED instead of the ever-so-scary depletion region in transistors, it is far easier to work with.

Before I move on to circuits, I should point something out: my exact opto's (Sharp PC123's) are hard to get your hands on because you either need to get them in a bulk of over 9000 or from older electronics. Thankfully, there is an easier-to-get equivalent: the Fairchild FOD817C (Mouser: 512-FOD817C). It is the EXACT same thing as the PC123 (just compare specs), but you can get them for like $.40 each and can buy as many or few as you want.

Now, on to circuits that allow us to use these optocouplers as voltage-control elements!!!

Here is a test circuit. Vary the input voltage with the pot (100k works best for direct connections to voltage sources), and watch the resistance change on the output side. Be sure to measure with the positive (usually red) probe on the collector side.

Here is a square wave VCO. You can swap out the capacitor to smaller values for higher frequencies; I recommend around 22nF for decent low and high frequency response. This exact circuit is more for LFO speeds. Output is the OpAmp output.

Here's a VCA. You can use smaller values for the input resistor. Note that the maximum gain of the amp is 2, and it has a capacitor on the end to even out any DC offset. Always be sure to put the optocoupler nearest to ground when used as a VCA. You can also do without the opamp stage and just have a voltage-controlled attenuator.

Here is a VCF. Neat thing about this circuit: given the large 1uF capacitor, this lowpass filter actually has some resonance! You can't really control it, though...

There are literally hundreds of different uses for this kind of setup. The above are just a few examples (that just so happen to end up making a full synth: VCO, VCF, VCA). A few things to keep in mind when designing circuits using this form of control:
-The 100k pot can be a CV input as well.
-The optocoupler tends to act the opposite of how you would think: increasing input voltage decreases output resistance. You can use an inverter with a DC offsetter to give you a positive voltage reflecting the opposite of the input.
-NEVER use a negative voltage for the optocoupler's input. You can use a diode facing away from the voltage source to protect from this.
-Using ground as the voltage input may cause problems. This may only be my experience, though.

Happy synth building!