Introduction: Function Generator (AD9833 Based)

About: Thank you, Instructables!! I have removed many of my Instructables as along with being cringy :P they lacked proper documentation (thus difficult to replicate) and some didn't even work properly. Please enjo…

Hi, I am Gursimran Singh

In this Instructable, I will guide you through on how to make a "functional" function generator, which can be very helpful when working with electronic circuits. A function generator can generate different kinds of signals like sine wave, square wave or even a triangular wave at a particular frequency set by the user.

The design explained in this ible uses AD9833 which can ideally generate frequencies up to 12.5 MHz as per the datasheet. I came across AD9833 when reading this project by Cezar Chirila.

I will explain the problems I encountered and how I got rid of them in upcoming steps. Also, you'll learn how to fabricate your own PCB at home.

This project is powered by AC Mains Supply (220 Volts), playing with electricity is not a good idea. If you are interested in making this project ,then please make it under the supervision of a professional, Electricity can be lethal!

Step 1: Demonstration

The 3 LEDs indicate the wave to be synthesized (Red for Triangle, Green for Sine and Blue for Square Wave), each of them can be selected by the horizontal black rectangular switch.

The first knob (potentiometer) from the left controls the amplitude of output, maximum is 12 volts.

The middle knob (potentiometer) controls the offset voltage of output.

The Red knob is attached to a rotary encoder which is used to set the frequency, basically scroll the encoder clockwise and anti-clockwise to move the pointer (Decimal on 7-Segment :P) right and left respectively to a desired place.

On reaching the desired place, button(Rotary Encoder's Push Button) is pressed, the pointer is locked at that position. Now scroll the encoder to increase or decrease the number value at the place. Pressing the button once again sets the value at the respective place and now the encoder can be scrolled to move on any other place.

I know I couldn't explain the whole value setting process properly :) The above video can help in visualizing the whole process.

Step 2: Circuit and Conceptualization

Signals related Stuff

AD9833 based modules with ability to synthesize signals up to 12.5 MHz (As per datasheet) can be found all over the internet. Fortunately these are damn easy to use, thanks to developers of compatible libraries. Skeleton of project is very straight forward. Atmega328p microcontroller communicates with AD9833 module which spits out desired frequency signal (Sine, Triangle or Square wave), which is obviously not capable of driving enough current and voltage levels too peak out at a maximum of 0.65 volts.

The output of it is fed into inverting terminal of a commonly available Op-Amp (LM386,TL071 etc.) , the offset and amplitude being controlled by two potentiometers. This finally is connected to output port.

A 7-segment display array controlled via MAX7219 IC is used for displaying frequency and what else could be better way for making menu interface other than THE ROTARY ENCODER.

Power Supply

A generic 12-0-12 center-tapped transformer power the whole thing rated at max 500 mA which should suffice for most situations. Rectified voltage is fed into two linear regulators LM7812 (for +12 V rail) and LM7912 (for -12 V rail), -12 V and + 12 V rails are for Op-Amp. For powering Atmega328p, Display, Rotary Encoder etc. 2 yes! two LM7805 regulators have been used. I'll explain why in upcoming steps.

Step 3: Noise Noise....Different Kinds of Noise

Coding part was a piece of cake, removing noise wasn't actually!

Power supply circuit we just made in previous step doesn't really get rid of ripples completely. But when making such a device, one should try to achieve the lowest possible noise.

I ruled out three reasons that could be causing noise issues:

  • Ground Loops! I prototyped everything on breadboards before soldering everything on a PCB and breadboards are notorious noise lovers. Ground loops can be removed by using Star-Grounding and using PCBs.
  • MAX7219 is also known to introduce high frequency noise(see pictures) in power rails.

I am no expert in designing PCBs neither did I have enough budget to get it fabricated from fabrication house. Thank God! I found a video on "Capacitance Multiplier" by Dave Jones of EEVblog (I am his big fan!)

Using this method instantly solved all noise related problems. I was greeted by clean output.

Even then, I tried to eliminate ground loops as much as I could.

I found an interesting read on allaboutcircuits forums about a gentleman who was able to solve noise issues with MAX7219 by limiting current drawn by its data lines simply by using 2 resistors. Linked here.

Step 4: Finally Inner Peace!

The MAX7219 issue was solved by placing 2.2 k ohm resistors in DIN and LOAD pins. This was suggested by helpful folks in this All About Circuits forum post

Also, I used two LM7805 Linear Regulators, one for the microcontroller and the other for the display. This too helped in reducing noise.

One I was able to achieve desired output! I was so delighted by it that I didn't touch it (fearing I would accidentally mess something up), until I was done with designing PCB.

"The Fear" was born when AD9833 decided to stop working for unknown reasons when I was busy removing noise. I had to order another module :(

Step 5: Ki-CAD Files

This was the most fun part of the project! Designing PCB and learning bits about "PCB Design Rules". I have no professional training in this subject (Maybe in the future I will, I am an EE student though) random youtube videos and articles on the web helped me through it.

The schematic of the project is attached below as pdf file, picture shown above isn't very clear though.

I tried using LM358 Operation Amplifier but the output was bugged with noise and pretty unstable, instead after watching GreatScott! video I ended up using TL071 Low Noise Operational Amplifier IC. In fact, the Operation Amplifier Implementation is done identically to that of GreatScott!'s

His channel is just plain awesome, go check it out for more electronics-related videos.

Step 6: Supplies

Capacitors :

  • 1000 uF Electrolytic Capacitors (6x)
  • 470 uF Electrolytic Capacitors (4x)
  • 10 uF Electrolytic Capacitor
  • 100 nF Unpolarized Capacitor (High Voltage Rated) (4x)
  • 100 nF Ceramic Capacitors (Number 104) (3x)
  • 22 pF Ceramic Capacitors (2x)

Resistors :

  • 220 ohm (3x)
  • 1k ohm (2x)
  • 2.2k ohm (3x)
  • 10k ohm (3x)

Potentiometers :

  • 10k ohm
  • 47k ohm

Semiconductor Devices :

  • D2SBA60 Full Bridge Rectifier
  • BD137 NPN Transistor (2x)
  • LM7805 (2x)
  • LM7812
  • LM7912
  • LM358/TL071 Operational Amplifier
  • AD9833 Breakout Board
  • Atmega 328-PU

Connectors and Jacks :

  • Pin Headers (Male and Female)
  • BNC Connectors (Mountable Socket and Jack)
  • Screw Terminal Block (3 terminal)
  • DC Barrel Jack
  • DC Port

Misc. Components :

  • Rotary Encoder Breakout Board
  • Buzzer
  • 16 MHz crystal
  • 7-Segment (8 Digit) SPI Display [Uses MAX7219 Driver]
  • Push Buttons
  • 500 mA Fuse
  • Compatible Fuse Holder
  • 12-0-12 500mA transformer

For PCB Fabrication :

  • PCB Clad (One Sided)
  • Ferric Chloride
  • Glossy Paper
  • Laser Printer!
  • PCB Drill
  • Sandpaper (fine grit)

Step 7: Preparation for PCB Fabrication

It's extremely important to remove stains and oxidation from Copper Clad PCB before proceeding. Isopropyl alcohol is useful, but I used very fine-grit sandpaper. Don't overdo sanding as might just remove too much copper.

Step 8: Transferring Toner

Print the attached pdf file on glossy paper using "Laser Printer". Please make it is Laser Printer only. Actually, this method itself is known as the "toner transfer method".

When printing make sure quality is set to high and "Actual Size"option is selected.

  • Get your iron heated up
  • Place glossy side facing copper
  • Smoothly glide the iron over other side of the paper (where traces are not printed) while applying pressure on board
  • Do it for 10 minutes, making sure each and every corner has been heated and pressed.

Step 9: Peeling Off

Now drop PCB into cold water and let it soak for around 5 minutes this will make removal of paper easy.

Don't forget to turn off Iron

  • Now carefully peel off paper, most probably it won't come off in single try.
  • Paper can be removed from inaccessible areas by rubbing using fingers gently.
  • Although toner sticks quite well to copper, you can use brute force but make sure you don't end up removing traces instead.

Step 10: Preparation for Etching

In upcoming steps you are going to play with corrosive chemical like Ferric Chloride, and it stains really bad! To prevent dying your skin yellow for a week (Or possibly worse situation).Please make sure to wear gloves and if possible use safety goggles too.

Anyways before we do anything permanent. Sometimes toner transfer isn't perfect. If you find broken/missing traces while visually inspecting, use a sharpie/Permanent CD marker to correct it.

These markers hold up pretty well in ferric chloride.

Step 11: Etching

Take spoonful of ferric chloride and add sufficient amount to water so that PCB is fully submerged.

A spoon of FeCl3 (Ferric Chloride) might not suffice, you can add accordingly, fortunately same solution can be used multiple times.

Shaking the container might speed up the process. It took 20-25 minutes to completely eat away unwanted copper.

Again be careful when handling corrosive chemicals.

Step 12: Appreciation of Beauty

After you see no copper except under toner (which you can't see anyways!), remove PCB from solution and rinse it in water, make sure to clean it completely (Remainder chemical on board and corrode traces after toner is rubbed off)

Step 13: Drilling Holes

Here comes the boring part, btw end result is somewhat satisfying. I used a cute hand-drill which is suitable for PCB drilling and other fine work.

0.8 mm bit was used for majority of components and wherever it was too tight for components to fit I drilled with 1 mm bit.

After holes have been drilled use a fine grit sandpaper again to rub-off toner exposing shiny copper traces. Isopropyl alcohol might be a better option as it removes risk of over sanding.

Step 14: Lessons Learnt

Dremel/Rotary tool is widely used for PCB drilling but the problem is without dress press its very inaccurate and finicky to use. If you have a drill press go for it. But I don't have any and ended up destroying one copper pad(Although it wasn't a big deal)

Step 15: Soldering

Aha! The most fun part of build when you see your project coming to life. I spent much of my day meticulously soldering components in place. Please maker sure you are placing components correctly keeping in mind polarity and pinout.

Wrongly placed electrolytic capacitors can end up in disaster!

Please refer to Ki-CAD files provided when soldering. Double check when soldering capacitors and transistors.

Step 16: Finishing Off PCB Build

By evening I was done with soldering. As you can see in the second picture I had reference board file opened in front of me because I don't want to place capacitors in opposite polarity and end up getting hurt. You should also do the same!

Step 17: Burning Bootloader on Atmega328p

This is where I made a mistake in first attempt.

I bought Atmega328P which usually doesn't come with bootloader installed. You have to do it on your own. Using this method I was able to upload code on Microcontroller but ISP communication doesn't require bootloader.

My end goal was to leave a port on PCB from where I can modify code without removing Atmega328P from its socket. ISP protocol uses just too may pins that too common with the pins I was already using for 7-Segment display

When searching on web I found this method on Nick Gammon's website that worked flawlessly I recommend you to do the same. But his setup programs and Arduino UNO being programmed by another Arduino UNO. The Arduino UNO being programmed is our Atmega328p in my setup. But how to wire Atmega328p to the programmer? Use wiring used in this link, you'll need some components like 16MHz crystal and 2x 22pF capacitors. You need another Arduino UNO with sketch (Github link mentioned on Gammon's website) uploaded onto it. This process can be confusi, If you have any problem with this task you can contact me.

Finally bootloader was up and running, I tested blink sketch.

Step 18: Code

The code attached below requires some libraries to be installed, to make your work easy I have mentioned all the links to their respective github pages below




Download them as zipped folders

After downloading them Open Arduino IDE

Go to sketch > Include Library > Add .ZIP Library and navigate to where you have downloaded them

Repeat this process for all three of them.

Math and TimerOne are preinstalled in IDE I guess.

I got some idea about using ClickEncoder.h from this youtube video. Precisely where I implemented the menu kind of function for setting the frequency using Encoder. In fact, the same channel has many great videos about arduino projects.

Step 19: Enclosure Design

I used bakelite sheets to sandwich wooden frame inside as my enclosure.

3-D printed option is probably a better option but I don't have access to any currently.

For making the front plate:

  • Place all components giving enough space for wires etc.
  • Mark points for holes to be drilled
  • These holes will be used for mounting components using nut/bolts or nut/washer combo that comes with potentiometers.

Wooden frame is nothing but a quarter inch thick plywood cut according to make a box with 4 sides (3 attached permanently to one another and the 4th one to be used a lid and held by screws).

This process doesn't require any fixed parameters you can easily make it fitting your requirements.

Rough size of enclosure I made is 15cm X 20cm X 8cm

For making square cutouts, firstly drill large holes and then use file to make cutout square.

Step 20: Fuse and Safety

For safety, I added a fuse between AC Mains Electricity and Primary of our transformer. See above schematics for more detail.

Step 21: Packed Everything In

Used M3 nuts and bolts to mount everything in front and backplates.

Now it was time to test the whole thing.

Please be careful, this project deals with AC mains power supply! Make sure you are a professional or make this project under a professional's supervision! Accidents can be lethal!

Step 22: Final Thoughts

Overall I am very satisfied with the build, after so many years I got to make a project. It is quite functional and so far I haven't encountered any major issues. But this is not by any means perfect. A lot many things can be improved :

  • The enclosure is quite bulky, extruded aluminum enclosure could be better.
  • The size of PCB can be reduced but soldering SMD components require a higher level of skills and special equipment.
  • The transformer heats up a bit after continuous usage, nothing to be worried about. But that can be cooled using a PC fan.
  • It can remain stable at a pinpoint frequency say 345789 Hz, as evident from the above photo frequency can jump around a little.

I hope you liked it:)


STEM Contest

Participated in the
STEM Contest