Logic Noise: Sweet, Sweet Oscillator Sounds

By Elliot Williams

Welcome to part one of a series taking you down the rabbit hole of DIY electronic synthesizers based on (largely) CMOS logic chips. Instead of synths being commodity gear made by large corporate enterprises, we’ll be building with the cheapest available parts, using and misusing digital logic. In short, don’t expect pre-packaged smooth tones, because we’ll be making creative noise machines.

If you’re the chiptunes type, you’ll probably find something you like here. If you’re the circuit bender or electro-noise-punk type, this is gonna be right up your alley. If you just like to see CMOS chips wriggle and squirm in unintended ways, feel free to look over my shoulder. If you’re the type who insists that a screwdriver can’t be used to pry open a paint can, then maybe you’d better move along. There’s a thin line between the glitch as bug and the glitch as interesting discovery, and we’ll be dancing all over it.

To give you a taste of what we’re up to this session, here is a quick demo.  Have a look and then we’ll get down to it.

It All Starts With Squares

single-oscillator-breadboardWe’ll start off with one oscillator (yawn!) and then turn it into something much more interesting really quickly. Making this circuit playable takes a little more experimentation, but that’s the whole point. And along the way, we’ll be laying the groundwork for much more complicated circuits later on. We’re starting at the beginning, but the curve is steep.

Our simple oscillator circuit is based around a logic inverter. An inverter is a chip that outputs the opposite logical voltage level from what’s put in: if the input sees the high voltage level, it sets the output low (and vice-versa).

To see how to make a quick and dirty oscillator out of an inverter, think about what would happen if you connected an inverter’s output back into its input. If the input starts off low, the output goes high. But since they’re connected, the input is now pulled high which in turn sets the output to low, and we’re back where we started. This high-low-high-low feedback circuit gives us a basic oscillator, only there’s a couple more nuances we’ll need to look into.

40106_pinoutThe chip we’ll be using for our oscillator is the CD40106BE; also called the HEF40106, depending on who makes it (datasheet, PDF) which is a hex inverter with hysteresis. It’s a great chip for our purposes because it’ll work on a wide voltage range so that we don’t have to stress about powering it. 5V is a good minimum, but 9V batteries are no problem and 12V is just peachy. Hex just means that there are six inverters in the same chip. You can see how they’re laid out below. Pin 1 is the input of the first inverter. Pin 2 is its output, and so on.

Hysteresis is just Greek for: state dependence. In our particular case, it means that the threshold value that the chip uses to determine whether the input is high or low depends on whether it’s currently high or low. The 40106 chip has two threshold values: a lower value that’s active when the input is already high and a higher value that’s used when the input is low. For the traditional use, this provides a degree of noise immunity; if the input is currently high, but fluctuating around a bit, it has to drop lower than the lower threshold to change state.

hysteresis

Here, we rely on hysteresis to make our oscillator run. If you substituted an inverter without hysteresis, its output would sit at the (single) voltage threshold. Why? When the output voltage gets a tiny bit above the threshold, it pulls the input up with it, and it switches the output low. When the output goes a tiny bit below the threshold, it switches back the other way. Instead of an oscillator, you end up with the chip thrashing back and forth internally just to hold a constant middle voltage level on the output.

If you add hysteresis into the mix, you get an oscillator. Instead of wiggling imperceptibly around one switching threshold, we have two thresholds. This means that the chip won’t switch its output low until the input rises at least up to the higher input level. Now, that happens pretty fast: when I build the circuit with straight feedback, I get a square wave that oscillates at 4.3 MHz, a few orders of magnitude too fast for human hearing. We need to slow the feedback down.

To lower the pitch down into the audio range, we run the feedback through a resistor to limit the current in the feedback path, and then charge up a capacitor with this current. The time it takes the capacitor to charge up from the lower threshold voltage to the upper depends on the current that it’s supplied. That means that the frequency is determined by the size of the resistor and the capacitor. A larger resistance limits the current, slowing the cycle down. A larger capacitor requires more charge in order to reach a given voltage, which also results in a slower cycle and thus lower pitch.

40106_just_osc

So look over the oscillator circuit for a second and we’ll recap. Imagine that the input voltage just crossed the lower threshold. Because the input voltage is low, the output is set high. The high output and low input causes current to flow through the resistor which slowly charges up the capacitor until the input voltage is higher than the high threshold, when the logic switches state and the output goes low. The low output then slowly discharges the capacitor until it drops below the low threshold and the cycle repeats. Tadaa! A square wave on the output bouncing between the two logic voltage levels.

But what about the input? Remember that the input sees the capacitor charging and discharging between the two threshold levels. Because the output voltage is constant and the voltage on the capacitor is increasing over time, the current that flows through the resistor drops a little during the cycle, so what we get is a “triangle” wave that’s made out of exponential curves rather than straight lines. (Good enough for gov’t work.) We’ll use this “triangle” waveform in a couple of weeks when we move on to linear-mode logic chip abuse, so just keep it in the back of your head for now. In the mean time, a scope trace is worth a thousand words.

cmos_40106_square_trace

Here in yellow is the inverter’s output; a nice 5V square wave at 321 Hz. The green trace is the input, which shows the slowed-down charging and discharging of the capacitor. You can also see just exactly where the inverter’s lower and upper threshold values are, marked off by the dashed lines. As soon as the voltage on the capacitor reaches the relevant threshold voltage, the output switches state. And that’s the essence of the “relaxation oscillator” — it’s a simple feedback oscillator that takes advantage of the inverter’s hysteresis and is slowed down by charging up a capacitor.

And here’s what it sounds like. I used a potentiometer for the resistor, and I’m twisting the knob during the demo.

Build It

trimpot-oscillator-testsEnough theory. It’s time for you to build yours.

To actually hear this thing, we’ll also need to connect it to an amplifier and speaker. I recommend the cheapest powered “computer” speakers you can find because they have built-in amplification with volume control and you won’t really care if you break them. I’ve even cut off the normal 1/4″ plug on the end of mine and soldered on alligator clips to make them easier to attach to breadboarded circuits.

If you’re going to connect DIY noisemakers up to something valuable, you’re going to want to reduce the output down to line levels: let’s say one volt peak-to-peak. If you’re running on a 9V battery, that means dividing down by a factor of nine-ish. You can do this with a simple voltage divider (see below).
Finally, there’s two details that you almost always have to think about with CMOS chips. First, they make spikey demands on the power supply as the internal transistors are switching. To smooth this out, I usually add a 0.1 uF or 1 uF capacitor just across the chip’s power pins. Second, the unused inputs should ideally be connected to ground (or VCC, your choice). That means grounding pins 3, 5, 9 11, and 13. And don’t forget to hook up power to pins 7 and 14.

A practical circuit would look something like this:

40106_osc_power

Fire up your speakers / amp and give it a listen until you can’t take it anymore. For me that took about five seconds. (For my wife, about three.) Let’s see how quickly we can add a little pitch control and dynamics to make things more musical.

Pitch

First let’s work on changing the pitch up. The frequency is determined by how quickly the capacitor is filled up through the resistor, so we’ve got to change one or the other. Changing the capacitance is hard, so we’ll work on the resistor. Any way that you can put a variable resistance in this circuit is fair game. A potentiometer, as above, is pretty obvious.

A light-dependent-resistor photocell is a great option. You can then use light to control the pitch by waving your hands around, which is kinda cool. But it gets much cooler when you invite the LEDs over — anything that you’ve got that can turn on and off an LED can change the pitch of your oscillator. If you’ve been racking your brain about how you’re going to hook your Arduino up to this circuit: PWM to get different brightnesses and “control” the pitch.

And then there’s DIY resistors. Some old VHS tapes have a nice resistance that increases linearly across their length, but you may have to try a few before you find the right brand. (A really thick layer of graphite laid down by a dark pencil works just about as well.) Test the matte side of a bunch of tapes with an ohmmeter first before you go ripping them out of the cassettes. My copy of “12 Monkeys” has 37 KOhms per inch, which is pretty much ideal using a slightly smaller capacitor. I’ve taped the VHS tape down to a piece of cardboard and connected it to the circuit with alligator clips. I play it by tapping on the tape with another clip. Good vibrations!

"Originalstylophone" by Dhscommtech at en.wikipedia. Licensed under CC BY-SA 3.0 via Wikimedia Commons
Originalstylophone” by Dhscommtech CC BY-SA 3.0

If you want multiple discrete notes, like a piano, you can connect a variety of resistors with one end connected in common to the capacitor. On the other end, connecting back to the output, you attach a wire that you use to select which resistor is in the circuit. See the stylophone for inspiration.

If your DIY resistor doesn’t have the same range as mine, or you just want to play different pitches, you can use whatever size capacitor you need to get the range you want, naturally. The chips don’t like to put out more than a couple of milliamps, so try to keep the minimum resistance above 2 KOhms if you want the chip to run in spec.

With the 0.1 uF capacitor that I’ve chosen 10K Ohms is a good lower limit for the resistance, otherwise the pitches get annoyingly high. Because of this, you might want to toss a 10K resistor in series with whatever variable resistance scheme you’re using. Otherwise, you’ll end up annoying the dog or disrupting bats’ flight paths.

Dynamics

If you’d just like to turn this thing on and off, we’ve got a few options. Easiest is to wire the VCC power supply through a pushbutton. Press the button, the chip gets juice and makes noise. Release, no power and silence. A quick way to improve on this circuit is to add a fairly large (10uF to 100uF) capacitor just after the switch. When you release the button, the capacitor will provide enough charge to run the circuit for a little while, smoothing out the release of the note.

Otherwise you can lean on the LDRs again, and make yourself a light-dependent volume control by adding an LDR to the voltage divider that I suggested above to get the signal down to line levels. Ping this LDR with an LED, and again you’ve got electronic control over the volume. We’ll do volume control more seriously in a few weeks, but for now you can cobble something together with an LDR if you really need one now.

If you’re building along, now is a good time to test some of this out on the breadboard, and maybe try to play a simple melody or whatever. At least try the button in the power supply trick, and try experimenting with different power-fade-out capacitor sizes. Once your familiar with one oscillator, we’ll throw in another.

Timbral Modulation

Now it starts to get interesting. (Oh, this is just the beginning!) We’ve got five inverter gates still unused, any of which could be used to add richness / modify the simple square wave we’re putting out so far. So let’s make an oscillator sync effect. If we build up another oscillator and use it to turn on and off our first oscillator and set things up just right, we’ll get the sometimes nasal, sometimes biting timbres of digital hard sync.

40106_sync_osc

Build up a second oscillator just like the first, and connect the second’s output to the input of the first oscillator through a diode and an approximately 1 KOhm resistor. (The resistor limits the output current to keep the chip in spec.) When the second oscillator is high, it will conduct through the diode and keep the input on oscillator one always high, effectively turning it off. But when the second oscillator goes low, it will release oscillator one to do its normal thing.

The frequency of the first (fast) oscillator acts as a timbre modulator, and the second oscillator controls the overall perceived pitch. Let’s take it to the scope.

cmos_40106_gated_traces

The green trace on top is the second oscillator, set to the frequency that determines the pitch. It’s a boring square wave. But the yellow trace on the bottom is the synced, higher frequency trace that we use as output. You can see that when the green oscillator goes high, it conducts through the diode and forces the input of the yellow oscillator high, and correspondingly its output low. When the green oscillator output goes low, the yellow oscillator is free to do its thing again; the diode only conducts one way. So we see the yellow oscillator start back up at its higher frequency until it gets cut off in mid-cycle by the green oscillator going high again.

This circuit sounds a lot more interesting than the initial boring old square waves, and it’s even more expressive when you can change the frequency of the synced oscillator. A potentiometer is OK, but here’s a great place to use an LDR to give you control over the timbre by waving your hand around over the photocell. The one caveat is that when the frequency of the synced oscillator approaches that of the syncing oscillator, you don’t get any sound anymore. So to play around, it makes a lot of sense to put the LDR in series with a potentiometer so that you can play with the range of the effect.

For extra credit, drive the synced oscillator’s frequency by pointing an LED at the LDR. Now however you control the LED will control the high-frequency component of the timbre. How close you get the LED to the LDR changes the depth of the effect, from subtle to really nuts. The pitch is still controlled with the potentiometer you’ve got running oscillator two, but the timbre is controlled by the LED. Of course, nothing stops you from applying the same trick to the pitch oscillator either.

Conclusion, and Next Week: Sequencers

So by now you’ve got something built up on your desk that’s either making completely autonomous noise with blinking LEDs, or you’ve built an expressive manual electronic instrument. If you get anything good, record it and link to it in the comments section. (And let us know how you did it.)

Next week we’ll add a sequencer to the basic relaxation oscillator framework here, so stay tuned. And if you’d like to pre-order yourself some parts, we’ll need a 4040 counter chip and (my favorite CMOS logic chip in the world?) a 4051 8-way analog switch. You’ll also want an assortment of resistors, probably an LDR or two, and a bunch of diodes on hand. And if you haven’t got those cheap powered computer speakers sorted out yet, get on it.

See you then.