## Introduction: Analog Function Generator

Are you sick of 555 timers? I sure am. That's why I decided to build a semi-decent function generator as my term project in my analog electronics course. This design is capable of producing a square, triangle, and approximate sine wave reliably up to a frequency of around 300kHz.

Make It Real Challenge
This project has been entered in the "Make it Real Challenge", which awards a 3D printer to the winner. "To enter, post an Instructable that shows how to turn a virtual item into a tangible object." I believe this project qualifies, since I spent a significant amount of time modeling the circuit on my computer before going on to the build stage. This Instructable does cover how to model a circuit (final step) and it does require the etching of a Printed Circuit Board based on that computer model. If you like my project, please rate it and vote for me!

Yes, there are chips available that achieve MUCH better results with a simpler circuit, but they are essentially "black boxes" that use digital methods to convert a voltage to a frequency. This circuit is fully understandable with algebra and rudimentary calculus and op-amp knowledge. Since this project is for an "analog electronics" course, this fit the bill.

Here's a breakdown of roughly how long it took to complete each stage of the project:
• 10 Hours: Initial design, including EagleCAD entry, board layout, and computer simulation.
• 3 Hours: Part sourcing.
• 5 Hours: Project box design, prep, painting, and labeling.
• 1 Hour: Initial PCB component placement
• 2 Hours: Connecting the PCB to the control panel.
• 20 Hours: Troubleshooting and component revision. Seriously.
• 1 Hour: Calibration.
• 1 Hour: Characterization.
This doesn't include the time I spent thinking about it while engaged in some other activity. I have a lot of experience troubleshooting lab equipment so the amount of time it took me to get this thing working surprised me. All the revisions I made are included in this Instructable, so it should work right off the bat, but be prepared to work a little.

This is nothing you'd want to rely on if you need any kind of precision, it's just a cheap, quick and dirty source of time-varying signal. Another reason I wanted to make a function generator is just to have another piece of ghetto lab equipment worthy of my growing collection.

## Step 1: Safety

This is a relatively complex electronics project that utilizes a lot of simple concepts and methods that all need to come together correctly. If you've never worked with electronics before, you should probably try some other project before delving into this one unless you're extremely ambitious. For those of you with a few years of electronics background, this is an exceedingly simple project.

This project utilizes Mains power of 120VAC, and is therefore EXTREMELY DANGEROUS for anyone that doesn't know what they're doing. Please have someone around that can administer CPR or call for medical help when you are experimenting with mains power.

There are other various risks associated with electronics, such as exposure to harsh chemicals (PCB etching and paint), lead (in solder) and hot tools (soldering irons).

DISCLAIMER:
This Instructable is provided for entertainment only and should not be used as a source of official information by anyone. Any and all damages incurred by the implementation of the information in this publication are the sole responsibility of the end user.

## Step 2: Relaxation Oscillator

This design is based on the simple yet effective relaxation oscillator. A relaxation oscillator is basically a Schmitt Trigger that has been forced into an oscillation based on the rise time of a series resistor and capacitor. A Schmitt Trigger is a configuration for comparators that adds hysteresis, or dependence on recent history, to a comparison operation. Hysteresis is the reason your thermostat doesn't click on and off in exactly the same spot when you turn the dial back and forth. Schmitt triggers are normally used to compare two voltages when the signals are rather noisy, since the low amplitude noise can cause the comparator to trigger on the noise instead of the desired signal. A Schmitt Trigger has a threshold voltage based on the values of two resistors, R1 and R2 in the attached figure. If we have the comparator connected to +/-12V, then the threshold voltages will be +/-12V*R1/(R1+R2). This design uses a trimmer for R1, so thresholding is variable.

Let's assume the resistors are chosen so that the threshold voltage Vthres=1V. When the circuit initializes, the voltage across C is zero and the comparator will rail to one of the supply voltages. Let's say it's +12V. Then the voltage at pin 3 is +1V. The comparator sees this and continues to supply +12 since V+>V-. Some current will travel through R3 and R4 and begin to charge C, causing its voltage to rise according to the function VC(t)=12*(1-EXP(-t/RC)). When VC goes just over 1V, then V+<V- and the comparator will change its output state to -12V. When Vout=-12 volts, then Vthres=-12*R1/(R1+R2), or for our scenario, Vthres=-1V. So now the comparator sees V-=+1V V+=-1V. The capacitor will discharge through R3+R4 and reverse polarity until V-=-1V, where the comparator will switch to +12V output again. This process continues over and over, and has a frequency of roughly f=(R1+R2)/(4*R1*C*(R3+R4)). A notable feature of this relation is that it is completely independent of the supply voltage of the comparator. It's solely a function of the passive components you choose.

A benefit of using a relaxation oscillator is that it is capable of producing an approximate triangle wave at the same time that it's making a square wave. This signal is the voltage across the capacitor. As I said before, this signal actually follows an exponential curve. However, if we make Vthres much less than the supply voltage (by around a factor of 10), then the curve can be pretty well approximated by a straight line. For those of you with experience with Taylor Series, we're essentially choosing "t" to be small and taking the first order (linear) approximation of the exponential. In other words, if we make Vthres small enough, we get two signals for the price of one.

You may say, "But this isn't a comparator, it's an op-amp!" but they're essentially the same thing. Op-amps are designed with negative feedback in mind and are typically slower, but they can be used for making voltage comparisons. I chose to use TL08x series op amps because they're cheap, much better than 741s, have a relatively high slew rate (speed), and have JFET inputs (high input impedance), so they're really the perfect hobbyist op-amp. I've tested this basic circuit up to frequencies of about 350kHz with pretty good results, but the signal does begin to break down somewhere around 500KHz. The op-amp just isn't fast enough. The oscillator itself could be improved with a high speed comparator, but this improvement would be lost later on down the line in the function generator so I chose to just use the op-amp.

If you want some extra speed, you could always try to find a high speed op-amp. Manufacturers try to make sure that the pin-outs are about the same for similar devices, so it should just be a matter of plugging a different chip into the IC socket. TL084s have a slew rate of 13V/µs, and a chip with a higher slew rate should have a proportionally higher maximum frequency output. For example, the TLE2144CN has an identical pin-out, costs \$4.90 each and claims to have a slew rate of 45V/µs, and therefore should be able to get up to 300kHz*45/13=1.04MHz reliably. I considered this option, and I may upgrade at some point, but just remember that the higher the frequency, the more likely your signal will be distorted or destroyed by stray impedances in the circuit.
In other words, there's no guarantee that spending the extra money will do anything for you.

## Step 3: Circuit Overview

The relaxation oscillator driven by IC1A is the heart of this project, but the rest of the design is what makes this a function generator.

The power supply is pretty standard. The 30V 15VA center-tapped transformer generates two equal and independent 15 volt sources when the two center pins are tied to ground. This is sent through a bridge rectifier and filtered by two large capacitors. These rough DC voltages are cleaned and set to +12V and -12V by the two voltage regulators.

IC1B is just an inverting amplifier that attenuates the square wave of the relaxation oscillator to a more useful voltage. A 12 volt amplitude is a little high at this end of the circuit. Inverting amplifiers have a gain that is determined by the ratio of the resistors attached to it. In this case, G=-R4/(R3+TR2). The trimmer is used in the calibration stage.

IC1C is a non-inverting amplifier that "picks off" the triangle wave from the capacitor. Non-inverting amplifier configurations are useful because they have extremely high input impedance. The actual charge on the capacitor is very small, and any significant current draw from that point can ruin the operation of the relaxation oscillator. The TL084 has an input impedance of roughly 10MΩ, so it should affect the capacitor very little. This way we can use the output of IC1C as the triangle wave source instead of the capacitor itself. The gain of this amplifier is G=1+TR3A/TR3B where the A and B represent the varying resistance of the trimmer. This is used in the calibration stage.

IC2A is in an Integrating Amplifier configuration. A good derivation of why this can integrate a signal can be found here. Essentially this converts the triangle wave (lines with alternating positive and negative slope) into a series of parabolas (with alternating positive and negative curvature) that looks an awful lot like a sine wave. The result is not a sine wave, but it is a reasonable approximation. Here again we can justify this with Taylor Series. The approximate sine wave is basically the second order taylor expansion of the desired sine wave. That's not strictly mathematically true, but you get the point. The gain of an integrating amp is G=-1/(2πRCf), where f is the input frequency. This frequency dependence is the reason we need a two-deck coarse frequency switch. We have to compensate the gain term to maintain a useful output voltage. Integrating amplifiers are plagued by a variety of effects that result from the fact that we are dealing with real technology and analog signals. The big thing is drift, which is the result of integrating tiny offset voltages inherent in the op-amp. This is corrected by R6, which brings the drift down to a reasonable level.

IC2B is just an inverting amplifier that we will use to calibrate the sine wave later. There is a low-pass filter with f3dB=600kHz directly before, which attempts to eliminate some of the overtones and make a prettier sine wave. The value of C18 isn't extremely important. As long as it's between 200pF and 500pF it should be fine. If you decide to use a high speed op-amp, you will need to either set C18 to a lower value or remove it entirely, or else you will attenuate your awesome high frequency sine wave. A high pass filter with f3dB=0.1Hz directly after IC2B eliminates any offset remaining after the drift correction.

IC2C is another inverting amplifier. Gain is either G=-POT2/R9 or G=-POT2/R10 depending on the position of an SPDT switch in the control panel. Since R9 and R10 differ by an order of magnitude, one being equal in value to POT2 and the other being POT2/10, the coarse gain can be chosen as 1X or 10X. The output of IC2C is fully processed and is used as the "high impedance output" which has a low amount of distortion but can only output a few tens of milliamps before saturating.

IC2D is a unity gain amplifier that is driving a power transistor push-pull amplifier. This is essentially a tricky little cheat to make my life easier. If I was doing this correctly, I'd use diodes and resistors to bias the transistors properly (transistors are tricky little buggers), but an op-amp in this configuration makes it so I don't have to do that through the magic of negative feedback. This makes the op-amp work pretty hard, and I was expecting that the signal would distort horribly at higher frequencies, but it turns out the op-amp drives the push-pull without any significant problems whatsoever.

## Step 4: Parts

Find attached the schematic, component map and PCB artwork for the circuit. These were made with EagleCAD Free Edition. The EagleCAD files are also attached below so you can modify the design for the parts you have available. I've never made a Gerber file with Eagle, but I'm sure there's some way to do that if you want to have the board etched professionally. Google is your friend.

The part numbers below are to the original parts I ordered from Mouser for this project, but anything that matches the descriptions should be fine, it just might take some work to fit things into the PCB if you try to use the one I've provided here unaltered.

The total cost for this project including all materials listed and implied was approximately \$70.

Power
1ea ~30V ~15VA PCB mount center tapped transformer - 655-4900-8028RD63
4ea 1N4004 or similar rectifier diodes
1ea 7812 voltage regulator - 863-MC7812CTG
1ea 7912 voltage regulator - 863-MC7912BTG
2ea ~470uF electrolytic capacitor
2ea ~270uF electrolytic capacitor

Signal
2ea TL084 op-amp - 595-TL084CN
2ea 14 pin IC socket - 517-4814-3004-CP
4ea 10k trimpots - 652-3319P-1-104
1ea TIP31 NPN transistor - 511-TIP31A
1ea TIP32 PNP transistor - 512-TIP32A
Non-polar Capacitors (ceramic or similar)
-2ea 100pF
-1ea ~270pF
-2ea 1nF
-2ea 10nF
-2ea 100nF
-2ea 1uF
-3ea 10uF
Resistors (metal film or similar)
-3ea 1k
-1ea ~5k
-5ea 10k
-1ea ~50k
-2ea 100k

Interface
1ea I/O mains switch - 611-DA102J3GS215QF8
1ea 100k potentiometer - 858-P160KNP0D20B100K
2ea 10k potentiometer - 858-P160KNP4FC20B10K
1ea 6 position, 2 deck rotary switch - 105-SR2612F-26-21RN
1ea 3 position, 1 deck rotary switch - 611-A10315RSZQ
2ea SPDT toggle switch - 108-1MS1T2B3M2QE-EVX
1ea Dual Binding Post - 565-6883
I got the third binding post from an electronics shop, but look around on Mouser, I'm sure you'll find something similar.

Optional
1ea speaker
1ea SPST switch
1ea Project Box

## Step 5: Frequency Select

I borrowed this idea from this Instructable. Thank you pcbheaven.

This project uses a 6 position, 2 deck rotary switch to select the order of magnitude of the output frequency. Remember, the frequency is inversely proportional to the size of the capacitor in the relaxation oscillator. The switch needs two decks because the sine wave output is the result of an integrating amplifier. Integrating amplifiers have a gain that is inversely proportional to the frequency of the input, and also inversely proportional to the capacitance you use. Therefore, every time you raise the order of magnitude of the frequency, you need to compensate the gain of the integrator by decreasing the integration capacitance by an order of magnitude. The rotary switch does this nicely by changing the value of both the frequency and integrating capacitors at the same time.

Get two each of non-polar capacitors:
Position Capacitance (F) Capacitor Code
one 10µ 106
two 105
three 100n 104
four 10n 103
five 1n 102
six 100p 101

On the back of the switch, there are two pins in the center and 12 pins on the outside. However, your switch may differ from mine, so you'll have to figure this out for yourself. The two central pins will connect to the circuit board, but the 12 outer pins need to connect to capacitors. There are two groups of 6 pins, each group representing the connections of one deck of the rotary switch. You may need a continuity tester to figure out which of the two central pins connect to each group of 6 outer pins. Each group of 6 pins connects to the 6 different capacitor values. The other side of these capacitors need to connect to a common wire, but the common wires of the two groups can't connect to each other.

Also, you want the switch to be intuitive, so the capacitors need to be ordered in a way that the frequency will increase when you turn the knob to the right. Frequency increases when the capacitance goes down, so you want the largest capacitor selected by the left-most position, and the smallest capacitor selected by the right-most position.

I first made a full ring of low gauge wire just big enough to fit around the switch. Then I soldered all the capacitors in. Then I cut notches into the ring to separate the two groups from each other, and soldered wires to the central pins and the two new outer rails. When I was done, I put heat shrink tubing over the capacitors to protect them from shorting on anything.

## Step 6: Project Box

The project box I used is a salvaged ATX power supply case. Those things are gold, I've been collecting them for a while, even dead ones, for parts and the cases. And when you find one that still functions but doesn't work with one of your computers, you can convert it into a pretty nice (and ghetto) lab power supply.

Once you have all your switches and dials, you need to decide on an interface layout for them. Anything nice and user friendly will do. I used the machine shop at my university to punch out holes, but drilling works too.

I went on to make the project look nice, but it's not necessary. I sanded the surface, applied primer to the inside and outside of the case, then painted the outside black. I wanted to keep the inside a lighter color so I could put a bright LED in there and have the light emanate from the vent holes. Once the surface was painted, I used my label maker to make control labels. I cut the labels to size with a razor, applied them, then painted over them using a clear enamel so they'll never come off.

Once you're done drilling holes and painting, mount the switches and dials in the case. Isn't it pretty?

I also used epoxy to mount a small 8 or 16 ohm (I don't remember which, it doesn't matter) speaker into the grating where the PSU fan used to be. This speaker is really handy for troubleshooting, and when the project is done, you can use the speaker to wage psychological warfare on your neighbor's cats.

## Step 7: Main Circuit

I used the photolithography lab at my university to etch the PCB, but not everyone has access to that kind of thing. Radio Shack sells ferric chloride etching kits but they're essentially worthless, especially for double sided boards. If you aren't able to etch a board, you could always use a prefabricated protoboard. You can buy those at Radio Shack for pretty cheap. The frequencies in this project aren't really high enough to warrant NEEDing an etched board anyway, they're just nice to have.

Before you begin installing components, make sure all your traces are isolated from ground. Use a continuity tester to check each trace against the grounding plane. There is no trace on this board that should touch ground directly EVER. DO THIS NOW. It will save you a ton of trouble later.

Solder the components into the PCB according to the component map attached in a previous step. This is pretty straightforward, and easily the most fun step of the project, depending on your taste. Personally, I was ecstatic. If you etched your own board, be sure to apply solder to both the top and bottom planes! Some connections depend on lead wires to connect the planes together. Professionally etched boards will usually have nice metal via holes that make soldering easier.

When you're done installing components, solder wires about a foot long each into the places where switches and potentiometers connect. Make sure to use flexible wire for the low voltage stuff since there are so many of them (~25) and low gauge wire for the mains power connections.

## Step 8: PCB to Case Connections

This is really complicated if you've never hooked up electrical things before. Just keep an eye on the schematic and use your head. The middle wire is always the middle wire on potentiometers, and the other wire(s) has a 50% chance of success! None of these connections will fry anything if you hook things up backwards, it just won't be intuitive to use and you'll have to fix it later.

You can also connect the output of the push-pull amplifier (OUT_2) to the speaker you mounted in the case earlier.

The only connection you should be REALLY worried about is the mains power switch. Please, please, please know what you are doing. If you're unsure, find someone who knows for sure and have them check your mains connections BEFORE you go sticking plugs into walls.

In fact, it would be very wise to use an external power source to do all troubleshooting and calibration. Just solder wires into the +12V and -12V rails and one for the ground plane and hook it up to your power supply (even a couple of 9v batteries would work here, it doesn't have to be fancy).

## Step 9: Troubleshooting

Don't throw stuff. Throwing stuff is bad.

BEFORE YOU TURN IT ON:
Make sure all the components are in place, oriented in the correct direction, and the switches and dials are wired up at least close to how they should be. Once again, go through the circuit with a continuity tester and the schematic and test all the traces to make sure they aren't connecting directly to ground. This seems like a pain, but trust me it's best to do this now.

A note on shorts: I know I'm being obsessive here but in my experience the two things that cause the biggest problems are cold solder joints and trace shorts. They can be virtually invisible. A teeny tiny sliver of solder, placed just right, can cause a short. A tiny jagged edge of the copper PCB plating can bridge the gap. If you find a trace that connects to ground, but can't see where the connection is being made, you may have to run a razor blade all the way around the trace just to be sure you got it. Just remember that no trace (at least on this project) should connect directly to ground, and go from there.

DO NOT USE MAINS POWER!

Use an external power supply (or two 9V batteries tied together, the middle connection is "ground"). Turn on the circuit and hopefully you'll hear something coming from the speaker. You might have to play with the frequency and gain knobs before you hear anything. Really, at this point it's good news if you hear anything coming out of the speaker. But in all likelihood, you won't hear anything.

This is where it would really come in handy to have a Ghetto Oscilloscope to help you identify where the signal is being lost. Use the oscilloscope to test each output pin in the circuit. If pin 1 of IC1 is a square wave, then you're set, just follow the path of the signal until the missing link is found. If you have no signal on pin 1, then you probably have a power issue since that should be the origin of the square wave. Make sure your power connections are all connected and aren't shorting to ground. You may also need to adjust the trimmers and other switches to get a signal to propagate to the next stage.

Continue adjusting and testing until you've got a good signal at OUT_1 and OUT_2. Then proceed to calibration.

## Step 10: Calibration

You'll need an actual oscilloscope to calibrate the function generator properly. I hate to say, the ghetto oscilloscope simply is too ghetto for any kind of precision. I used the one in my lab at the University.

Probe pin 2 of IC1. This should register a triangle wave, since you're measuring the voltage across the capacitors in the coarse frequency adjust knob. Adjust TR1 until the amplitude of the triangle wave is about 0.5V. Remember, the smaller this signal is, the closer it will be to an actual triangle wave rather than a series of exponential curves. The amplitude shouldn't be too low or we could get some undesirable effects from the op-amp.

Probe pin 7 of IC1 and you should see a square wave. Adjust TR2 until the amplitude of this signal is 1.0V. This means that when the gain knob is turned up all the way on 1X magnitude (selected by SW3), the final output will be 1.0V, and it will be 10V for 10X magnitude. This is really handy to know in the future if an oscilloscope isn't around.

Probe pin 8 of IC1 and you should see a triangle wave. Adjust TR3 until the amplitude of this signal is 1.0V, for similar reasons to above.

Probe pin 7 of IC2 and you should see a sine wave that has some DC offset as a result of integration. Adjust TR4 while scanning though the entire frequency range of the function generator until the magnitude of the signal is as big as it can be without distorting too much at any point. I found that the signal distorted very badly at the highest possible coarse frequency, so I decided to completely throw out the calibration for that and instead use the next highest setting as my baseline.

Probe pin 8 of IC2 and test that the offset potentiometer is wired intuitively (e.g. the offset goes positive when the offset knob is turned to the right).

## Step 11: Characterization

The function generator works! Actually, it works better than I expected it to work.

Here are the statistics:
 Frequency Ranges (Hz) Sine Amplitude (V) Position Low High one 4.7 490 1.4 0.021 two 56 5.2k 2.1 0.023 three 470 29k 1.9 0.048 four 4.2k 122k 2.4 0.23 five 32k breakdown 4.3 breakdown six 135k breakdown Ugly Useless

The square wave rise time becomes visible at f=10kHz. It becomes weakly trapezoidal at f=20kHz. It becomes strongly trapezoidal at f=100kHz. The square wave decomposes to a triangle wave at f=200kHz. The signal breaks down completely around f=500kHz.

The sine wave is unusable throughout position six due to some kind of non-ideal integration effect. With some work this could likely be fixed, but it's not necessary because the triangle wave output decomposes to an approximate sine wave at f=200kHz, so you can use the "triangle wave" as a sine wave for position six.

The use of the speaker destroys the integrity of the output of the push-pull amplifier. There is some ~3MHz feedback that it introduces. This is likely the result of a second order differential equation being solved by the L of the speaker and the stray C in the PCB and components. I tried placing a decoupling capacitor across the speaker terminals but this just caused a persistent self-oscillation so it was removed. Therefore, don't use the speaker while using OUT_2 for an experiment! However, OUT_1 still functions perfectly even when  the speaker is on.

The offset knob varies the DC offset from -3.0V to 5.7V, but at the time of this writing, the offset introduces an onset ping. This is probably because the resistor and potentiometer values for that function are an order of magnitude higher (100k each) on my build and it's probably interfering with the non-ideal op-amp behavior (non-zero V+ input current). This needs to be fixed on my set, but the updated values are already on the attached schematic, so you don't have to worry about this.

UPDATE March 13, 2012:
I replaced the original 100k values for the offset components with the listed 10k values today. The ping is significantly reduced, I'd estimate by 80%, but it's still there. It could probably be eliminated completely by reducing the order of magnitude once again. Also, the sensitivity of the knob is a little high for my liking. The sensitivity can be reduced by increasing the values of the resistors on either side of POT3, while keeping POT3 the same value.

Also, the use of the drift correcting resistor causes the sine wave to distort slightly at low frequencies around 10Hz, but this probably isn't a big problem.

## Step 12: It's Finished!

Wow! You completed the project! I'm very impressed. Yes, this is self-praise, but considering this is my first major electronics project I designed myself from the ground up (that's a little electronics joke), I think I deserve it dammit.

You can now use your function generator for all kinds of cool stuff. Make a TV Flyback Driver with almost no effort whatsoever. Use the square wave with offset to simulate a TTL signal for testing logic gates. Check to see which of your friends has the best high frequency hearing (I'm virtually deaf, I can't hear anything above 16kHz). I could go on, but I'm sure you already have something in mind...

Have fun kids!

## Step 13: Modeling the Circuit

So what exactly is going on in the circuit? I did explain generally what was going on in there but wouldn't it be nice to be able to actually see it? To design the circuit, I needed to be able to do exactly that. Electronics courses teach building blocks for making useful devices, but until the circuit is actually built, it's almost impossible to know exactly how it will behave. That's where computer modeling comes in. It isn't perfect, but it's close, and more importantly it's free.

Attached below is a text file titled 'function generator.txt" It's full of nonsense. Go to Paul Falstad's Circuit Simulator Website and a java window will open. I use this applet for all my electronics projects, it's pretty nifty. In the "File" menu, click "Import" and a text entry window will appear. Copy and paste the contents of "function generator.txt" into this window and click "import".

The full circuit is a little complicated for this simulator, but it gets the job done. You can adjust POT1 and 2, and TR1-4 to get an idea of what the circuit is actually doing.