Picture of Arduino Waveform Generator
function generator_schem.png
Screen shot 2012-11-29 at 12.32.37 PM.png
Waveform generators (also called function generators) are useful for testing and debugging circuits.  I often use them to test the frequency response of electronics components like op amp and sensors.  This waveform generator is powered by an Arduino.  It outputs four waveshapes: sine, triangle, pulse, and saw, each waveshape ranges in frequency from 1Hz-50kHz.  The frequency, pulse width, and overall amplitude (gain) of the waveforms is controlled by three potentiometers.  I've also included (optional) indicator LEDs that let you know which type of wave is currently being sent to the output.

Parts List:

(4x) Mini SPST 1.5-Amp Momentary Pushbutton Switch (2 packages) Radioshack #275-1556
(8x) 10K Ohm 1/4-Watt Carbon Film Resistor  (2 packages) Radioshack #271-1335
(9x) 20K Ohm 1/4-Watt Carbon Film Resistor (2 packages)
(1x) 50K-Ohm Linear-Taper Potentiometer Radioshack #271-1716
(1x) 10K-Omh Audio-Taper Potentiometer Radioshack #271-1721
(1x) 10K-Ohm Audio Control Potentiometer with SPST Switch Radioshack #271-215
(1x) 1/8" Stereo In-Line Audio Jack Radioshack #274-274
(1x) 10.01µf 50V Ceramic Disc Capacitor Radioshack #55047551
(1x) 4.7K Ohm 1/4-Watt Carbon Film Resistor Radioshack #271-1330
(1x) 8 Pin Socket Radioshack #276-1995
(1x) LM386 Low Voltage Audio Power Amplifier Radioshack #276-1731
(2x) 220µF 35V 20% Radial-lead Electrolytic Capacitor (or anything between 200 and 300 uF) Radioshack #272-1029
(1x) Arduino Uno REV 3 Radioshack #276-128
(1x) Arduino Proto Shield Radioshack #276-140
(4x) White Super-bright LED Indicator Radioshack #55050633
(4x) 740 ohm 1/4W 5% Carbon Film Resistor (1 package) Radioshack 271-1317
(1x) 300Ohm resistor

Additional Materials:

Heat Shrink Radioshack #278-1611
22 Gauge Wire Radioshack #278-1224
Solder Radioshack #64-013
Hot Glue
Black diffusor material (tissue paper, plastic, etc)
Remove these adsRemove these ads by Signing Up

Step 1: Prepare Arduino Proto Shield

Picture of Prepare Arduino Proto Shield
IMG_1779 1.JPG
IMG_1781 copy.jpg
IMG_1783 copy.jpg
IMG_1782 1.JPG
The Arduino Proto Shields are a convenient way to attach circuits to an Arduino, but I like to trim them down a little bit first so they do not take up so much room in the project enclosure.   Start by trimming the pins down with a pair of wire cutters.  Next, cut off the six pin socket.  Finally, cut the sockets from the top of the board.

Step 2: Enclosure

Picture of Enclosure
Screen shot 2012-04-17 at 4.25.59 PM.png
Screen shot 2012-04-17 at 4.29.59 PM.png
Screen shot 2012-04-17 at 4.25.07 PM.png
I decided to laser cut a custom enclosure for my project.  I designed the enclosure using AutoCAD, Autodesk 123D Make, and Corel Draw, and I've included corel draw and adobe illustrator 2D files as well as the STL, and DWG files from this process below.  If you do not have access to a laser cutter, you can use my 2D files a guide and drill the necessary holes in a project enclosure of some kind.  Figure 4 shows the holes that should be drilled on the front panel:

(3x) 7mm holes for gain, freq, and PWM pots
(3x) 7mm holes for four push buttons- sin, saw, tri, and pulse
(1x) 10mm hole for audio out
I cut out shapes of all four waveforms in the front of the enclosure so that I could backlight them with indicator LEDs, you may choose to just drill four 5mm holes for these LEDs in the front panel of the enclosure, place one LED under each momentary switch.
Also include a rectangular (11mm tall, 12mm wide) cutout somewhere on the side of the enclosure for the arduino's usb port.

I made my project enclosure out of wood, so I had to glue all the pieces (except the bottom) together with wood glue.  I will attach the bottom panel on later in this instructable.


Step 3: Solder Button Leads

Picture of Solder Button Leads
Solder a 10kOhm resistor to one lead of each of the four push buttons.  As shown in the second image, solder a green wire to the junction between the button and the resistor and a red wire to the other end of the resistor.  Solder a black wire the the second lead of the push button.  It's a good idea to cover these connections with a bit of heat shrink to prevent short circuits (fig 2).

Step 4: Install Audio Jack

Picture of Install Audio Jack
Unscrew the plastic casing from the audio jack.  Solder a red wire to the two stereo out pins and solder a black wire to the ground pin (fig 3).  I used hot glue to prevent short circuiting the jack and to give the soldering joints some extra support.  Finally, mount the audio jack in the enclosure with super glue.

Step 5: Install Buttons

Picture of Install Buttons
IMG_3537 copy.jpg
Snap the top of the button off and fit them into the wooden enclosure.  Secure with hot glue.  Once dried, snap the black button tops back on.

Step 6: R2R DAC on Arduino Shield: Part 1

Picture of R2R DAC on Arduino Shield: Part 1
IMG_3538 copy.jpg
Solder eight 20kOhm resistors to the arduino protoshield.  One end of each resistor should connect to digital pins 0-7.

Step 7: R2R DAC on Arduino Shield: Part 1

Picture of R2R DAC on Arduino Shield: Part 1
IMG_3542 copy.jpg
Solder 7 10kOhm resistors to the protoboard so that they bridge the leads of the 8 20kPhm resistors you have just soldered.

Step 8: R2R DAC on Arduino Shield: Part 3

Picture of R2R DAC on Arduino Shield: Part 3
IMG_3545 copy.jpg
Solder a 20kOhm resistor to the protoshield so that one end is connected to the 10kOhm resistor attached to digital pin 0 and the other  end is connected to a jumper wire to ground.

Step 9: IC socket

Picture of IC socket
IMG_3547 copy.jpg
It's a good idea to use sockets for your ICs, this way you won't risk burning the IC with your soldering iron and you can  easily replace the IC if it breaks.  Solder an 8 pin socket to the protoboard as shown in the image.

Step 10: Low Pass Filter

Picture of Low Pass Filter
IMG_3592 copy.jpg
Use a resistor and capacitor in series to create a low pass filter.  Low pass filters let low frequencies pass through and silence (attenuate) high frequencies.  Connecting a low pass filter to the output from the dac will smooth out the steps in the wave.

Here's how I calculated the value of the components in my low pass filter:

corner frequency = 1/(2*pi*R*C)

According to Nyquist's Theorum, signals cannot contain frequencies higher than half their sampling rate.  If I used a sampling rate of 100kHz, then the highest frequency I can produce is 50kHz.

if I use a 300Ohm resistor and I want a corner frequency of 50kHz:

50000 = 1/(6.28*300*C)
C = 1.06*10^-8 F

round this to:
C = 0.01uF

Connect one end of the the 300Ohm resistor to the 10kOhm resistor connected to digital pin 7.  Connect the capacitor to the other end of the 300Ohm resistor.  The other side of the cap should connect to ground.

Step 11: Amplifier: Part 1

Picture of Amplifier: Part 1
IMG_3596 copy.jpg
IMG_3597 copy.jpg
IMG_3600 copy.jpg
Connect the positive lead of the 220uF capacitor to the junction between the resistor and capacitor of the low pass filter.  The other end of the 220uF capacitor connects to a 20kOhm resistor that is connected to pin 3 of the IC socket.  A 4.7kOhm resistor bridges pins 3 and 4 of the IC socket.

Step 12: Amplifier: Part 2

Picture of Amplifier: Part 2
IMG_3602 copy.jpg
Connect ground to pin 4 of the IC socket.

Step 13: Amplifier: Part 3

Picture of Amplifier: Part 3
IMG_3604 copy.jpg
Connect the positive lead of a second 200uF capacitor to pin 5 of the IC socket.  The other end of the cap will be connected to the gain pot in a later step.

Step 14: Amplifier: Part 4

Picture of Amplifier: Part 4
IMG_3607 copy.jpg
IMG_3606 copy.jpg
Connect pin 6 of the IC socket to Vin, pin 2 to ground, and snap the IC into the socket.

Step 15: Wire Gain Pot

Picture of Wire Gain Pot
IMG_3610 copy.jpg
IMG_3612 copy.jpg
Volume or gain of the audio signal will be controlled with the 10k audio taper pot with switch.  Connect the audio out from the amplifier and ground to either side of the potentiometer as indicated in the picture.  The middle is audio out, it will be hooked up directly to the audio jack.

Also connect a wire to the bottom and left leads on the back of the pot (figure 2).  This is the switch that will be used to connect to power in the next step.

Step 16: Connect to Battery

Picture of Connect to Battery
Connect the black wire from the battery clip to ground on the Arduino Shield.  Connect one lead from the gain pot switch to the red wire  from the battery clip and connect the other gain pot lead to Vin on the Arduino Shield.

Leave the battery disconnected for now.

Step 17: Connect Output to Headphone Jack

Picture of Connect Output to Headphone Jack
Connect the output from the amplifier (the negative lead of the cap connected to the IC at pin 5) to the red wire we attached to the gain potentiometer in an earlier step.  Connect the green wire from the amplitude pot to the red wire connected to the audio jack.  Connect the black wire from the audio jack and the black wire from the pot to ground on the Arduino Shield.

Step 18: Wire Buttons

Picture of Wire Buttons
IMG_3617 copy.jpg
IMG_3618 copy.jpg
Connect all read leads from the button to 5V and all the black wires to ground on the arduino shield (fig 1).  Connect the green wires to analog in 0-3 in the following order:

analog 0   =   pulse
analog 1   =   triangle
analog 2   =   saw
analog 3   =   sine

Step 19: Wire Frequency and PWM Pots

Picture of Wire Frequency and PWM Pots
IMG_3412 copy.jpg
IMG_3619 copy.jpg
Connect a red, black, and green wire to the 10kOhm and 50kOhm potentiometers as shown in the images.  Connect the red lead to 5V and the black leads to ground on the arduino shield.  Connect the center green wires to analog  pins 4 (PWM) and 5 (frequency).

Step 20: Install Pots

Picture of Install Pots
Remove the side tab on all of the pots before installing in the enclosure, this will allow them to sit flush against the wood.  Remove washer and nut from each of the pots, place pot through hole in enclosure, and secure with nut.  Install all three pots in the enclosure.

Step 21: Wire LEDs: Part 1

Picture of Wire LEDs: Part 1
IMG_3710 copy.jpg
Attach a 470Ohm resistor to the cathode of each of the four LEDs.  Solder a black wire to the other end of the resisotr and a red wire to the anode of the LED.  Cover these connections with shrink wrap to prevent short circuiting.

Step 22: Wire LEDs: Part 2

Picture of Wire LEDs: Part 2
IMG_3711 copy.jpg
Solder the black leads from all four LEDs to ground on the arduino shield.  Solder the red leads to digital pins 8-11.

Step 23: Black Diffuser

Picture of Black Diffuser
Glue a light diffusing material behind the wave cutouts in the front panel.  I used a piece of a black plastic garbage bag.

Step 24: Glue LEDs

Picture of Glue LEDs
Glue the LEDs in the enclosure so that they are each pointed towards one of the cutout symbols on the front panel.  Here is a table for reference:

digital 8    =   pulse
digital 9    =   triangle
digital 10  =   saw
digital 11  =   sine

Step 25: Firmware

Picture of Firmware
Upload the code at the bottom of this step onto the Arduino.  The code uses a timer interrupt at a frequency of 100kHz to send new data out to the DAC.  The rest of the code monitors the state of the buttons and knobs and adjusts variables accordingly.  Since the interrupts occur at such a high frequency, I had to keep the interrupt routine, the piece of code encapsulated in the ISR(TIMER1_COMPA_vect){} as short as possible. Time intensive operations like mathematical operations with floats and using the sin() function take too much time to complete.  I used several work around to get by this:

For triangle and saw I created the variables sawByte, triByte, sawInc, and triInc.  Every time the frequency changed I calculated the amount that the triangle and saw function would have to increment at a sampling rate of 100kHz:

triInc = 511/period;
if (triInc==0){
   triInc = 1;
sawInc = 255/period;
if (sawInc==0){
   sawInc = 1;

then all the needed to be done in the interrupt routine was some simple math:

case 1://triangle
if((period-t) > t);
    if (t == 0){
        triByte = 0;
        triByte += triInc;
    triByte -= triInc;
if (triByte>255){
    triByte = 255;
else if (triByte<0){
    triByte = 0;
wave = triByte;

case 2://saw
if (t=0){
wave = sawByte;

For the sine function, I wrote a simple python script which outputs 20000 values of 127+127sin(x) for one complete cycle:

import math

for x in range(0, 20000):
    print str(int(127+127*math.sin(2*math.pi*x*0.00005)),)+str(","),

I stored this array in the Arduino's memory called sine20000[] and recalled the values I needed to send to the DAC.  This is much faster than calculating the values individually.

Step 26: Last Few Connections

Picture of Last Few Connections
IMG_3794 copy.jpg
IMG_3799 copy.jpg
Plug the Arduino into your shield.  Connect a 9V battery to the battery clip.  Secure these items inside the enclosure.  Make sure that the Arduino's usb port is accessible from the outside of the enclosure.  Upon startup you should see the sine wave LED light up.

Step 27: Screw Back Panel

Picture of Screw Back Panel
Drill four holes in the back panel and secure with screws.

Step 28: Add Knobs

Picture of Add Knobs
Screw knobs on the three potentiometers.

Step 29: Test

Picture of Test
Turn up the gain knob to turn on the function generator.  Plug an eighth inch jack into the output and hook up the function generator to an oscilloscope.  Test out each of the waveforms and adjust the frequency and gain to make sure they are working properly.  Switch the output to pulse and check if the pulse width modulation knob works (figs 4-6).

You will notice that the pulse wave is the only wave which truly ranges from 1Hz to 50kHz.  Since the sampling rate is 100kHz, the sine, triangle, and saw waves start to become somewhat unrecognizable at about 25kHz (they are only comprised of 4 samples per cycle- 100kHz/25kHz).  The saw and triangle waves only go down to about 100Hz, this is because the values of triInc and sawInc get so low that they are rounded to zero below this frequency.  The sine wave reaches all the way to 1 HZ but the resolution stays the same for anything under 5Hz, since the Arduino only has enough memory to store about 20 thousand samples.
1-40 of 139Next »
daudahmad2 months ago

Arduino: 1.6.1 (Windows XP), Board: "Arduino Uno"

function_generator.ino:30:8: error: 'sine20000' does not name a type

In file included from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:28:0,

from function_generator.ino:30:

function_generator.ino: In function 'void __vector_11()':

function_generator.ino:227:31: error: 'sine20000' was not declared in this scope

Error compiling.

This report would have more information with

"Show verbose output during compilation"

enabled in File > Preferences.

I get the following error while i compile the code in arduino IDE. I tried alot. but could't solve the problem. Would anybody please help me ?


Hello amanda .. i made this project nut when i upload the given code it gives me following error

from function_generator.ino:30:

error: variable 'sine20000' must be const in order to be put into
read-only section by means of '__attribute__((progmem))'Can yu please help me out

Just now, I compiled the sketch with Arduino 1.0.6 set to Uno, and it didn't have a problem. So I can't prove I've found your answer. What board are you compiling for that gave you this error?

Sometimes I accidently type something into a sketch without knowing it, that causes it to crash. You could try reloading the sketch from the original "untouched" source.

The "30:18" in your error message means it happened on line 30, and the 18th characcter in from the left. The error message states the line should be a "const", so I did that, I added the word "const" so the line starts with "const byte sine20000" instead of just "byte sine20000", and again, it compiled just fine for me.

Give that a try.

"const" means the values in that line will not be changed, which I beleive is correct since it's only read -- over and over again -- to make the sine wave.

PaulS203 months ago

After googling for "Sine Wave Circuit", I was delighted to find this "Arduino" sine wave circuit, because I love working with Arduino and it's compatible modules. Also, the lazer cutter used here was a wonderful discovery for me too! I didn't even know there was such a thing, but now must have one to add a professional look to the projects I sell.

However, the article has room for improvement, which is the reason for this comment. Here are my suggestions:

1. The several places the text refering to "300Ohm", and "470Ohm" need a space to make it "300 ohm" and "470 ohm, so it doesn't look like a value ten times larger than intended.

2. One schematic shows the 300 ohm resistor and cap for the low pass filter. But the other schematic omits this circuit.

3. Although the photography is very clear, no one picture shows all the wiring where it's easy to understand everything at once. Take a look at


Every wire -- and where it connects -- is easy to see in one picture. And like in this picture, I always try out my projects on a white breadboard first; for it's far easier to try out alternatives and correct mistakes where all parts plug and unplug, than it is on a project board where you have already soldered the parts permanently in place.

4. The text says, "A 4.7kOhm resistor bridges pins 3 and 4 of the IC socket." Yet the schematic shows this as being a 2.2k ohm resistor. Which value is correct?

5. It would help to have a note on the pictures, explaining you can mouse-over the rectangles to pop up notes on what's inside the rectangle. I'd nearly finished the project before discovering this.

6. One such rectangle (over a straight section of wire in a schematic) says a resistor should have been here. Why not just add the resistor to the image and reload it? The note is easy to miss (and thus the need for the resistor.)

7. Another mouse-over rectangle says the color code on the resistor shown is wrong. So why not just use an art program to paint the correct colors on the resistor, and re-upload the image?

I think this is a GREAT project; exactly what I needed. But the above seven items make it hard to follow correctly. For the benefit of others, I'd LOVE to see these modifications made.

Hello Paul .. i made this project nut when i upload the given code it gives me following error

from function_generator.ino:30:

function_generator.ino:30:18: error: variable 'sine20000' must be const in order to be put into read-only section by means of '__attribute__((progmem))'

Can yu please help me out

Adi M4 months ago

Is there a point in storing 20000 samples? If our DAC can only represent 2^8 values? Surely 256 samples should be the maximum sample resolution? Im a bit confused. Nice project :)

Cheeseduck Adi M2 months ago

Think of a sine wave in the x-y plane. There are only 256 y values available, because that's the resolution of the arduino's analog output pins. However, we can have as many x values as we need, to some extent.

So the question becomes what is the smallest number of x values you need for it to still look like a sin wave. The author chose 20k, but you could do more or less if you wanted to. As I see it, the only reason to do more is for higher resolution at lower frequencies, and the only reason to do less is to save storage space.

wgrabarz3 months ago

Hey, would it be possible to convert this project to a synthesizer dual DCO? Midi controlled and with one output for each DCO where DCO2 frequency is DCO1*detuneFactor (set by a pot on an analog pin, range +/- 1 octave so detuneFactor would have to go from 0,5 to 2)

hey i have to take the output of the arduino through earphones instead of speaker.. my arduino output is working pretty fine with speaker.. but i have to switch it now to earphones. can you please guide

The problem with earphones is that they put all that power right into your ears! Just connect a resistor in series with each earphone wire. If they are 8 ohm ear phones, a ten ohm resistor would reduce the power about in half, a 33 ohm resistor would reduce it to 1/4th as loud, etc.

Hello Amanda,

can i use arduino atmega2560 in place of uno???

thank you!

If you already have your atmega2560, just try loading the software sketch this instructable provided into your Adruino. If the the Arduino tells you it's happy with it, that should bve your answer.

tony.kupcho4 months ago

Is there any particular reason for the 35 V rating on the 220 microF capacitors? I already have a set of 16V and was wondering if there would be any reason I couldn't use them

No, no reason why you cannot use your 16 V caps. I guess the 35 V volt ones was what the author had available at that moment. As a general rule: as long as the capacitor voltage rating is higher than the supply voltage of your circuit, it will be OK.

pepeSpeculum4 months ago

How can I change the R 2R resistor set for a DAC08 (http://docs-europe.electrocomponents.com/webdocs/0...) in order to save components? What connections do I have to do?

Thank you

Great project

Hi Amanada.

can i use arduino atmega2560 in olace of uno???


MatthewT74 months ago
so im not 100 sure on the math but im short a few 10k resistors.. if I just do 2 4.7k resistors in series will that be close enough? will that just affect the amplitude a small bit?
aplavins4 months ago

Amazing and well laid out! I look forward to making one!

AlxDroidDevA5 months ago

Great work! Very well thought out and quite useful. Using an Arduino instead of a professional arbitrary waveform generator can save us a bunch of money.
I have one suggestion, however: add a button that, when pressed, will send just one cycle of the selected waveform in the selected configuration (one-shot trigger).

Pythag1 year ago
Hi there; this has been a great project so far!

I just wanted to ask about one hiccup im having. For some reason, the signal coming out from my amp is clipped clean in half, only going up to about half the voltage it should be before just flattening off. The signal coming into the amp is nice and clean, and im fairly sure ive got everything wired up with the same components you used, although my amp is an lm386n-1; do you suppose that could be what's making the difference? I couldnt find anything different about it's specs, but I'm really just a beginner, so I wouldnt know for sure.

Anyway, thanks for a brilliant tutorial!
Pythag Pythag1 year ago
Ah, my mistake actually; got confused with the AC readings from my oscilloscope. It actually is getting the full voltage from the amp, but it's still clipping. I put 3 20k resistors in series with the frequency coming from the arduino, and it's fixed the clipping, but it hardly seems an ideal solution. Is there something else I could be doing to lower the gain on the amplifier?
guy gibbs Pythag5 months ago
I too am having a clipping problem ... can you please tell me where to hook to 20k resistors to resolve this ?
amandaghassaei (author)  Pythag1 year ago
what op amp are you using?
amandaghassaei (author)  amandaghassaei1 year ago
oh i see, a lm386. you should wire it up like this:
astein-18 months ago

are the momentary sqitches normaly Open or normally closed?

amandaghassaei (author)  astein-18 months ago
normally open

thnx ur awesome!

astein-18 months ago

that resistor looks like a 10k not a 20k

what do i do??

dimaris9 months ago

Hi Amanda, I made your signal generator.

Why then the output signal from the LM386 truncated vertices, in what could be the reason? Arduino itself gives the right signal and this signal LM386 spoils.

jasper5009 months ago

Hi Amanda, I see that you have an amazing 20,000 values for the sine function. Since you only have an 8-bit DAC with 256 levels, is this actually necessary. Can you explain how this adds resolution?

jasper5009 months ago

Am I right in thinking that because the frequency is set by reading the voltage at one of the analogue inputs, that there will only be 1024 steps in frequency between 1Hz and 50Khz? Is there anyway of being able to set the frequency in code?

lopsan93110 months ago

any suggestions of how to implement a LCD displaying the frecuency ***FREQUENCY METER**

JoshS210 months ago

I needed a function generator for testing circuits, but couldn't afford to spend several hundred (or more) dollars. This one worked perfectly. I upgraded a few of the parts (for more accurate gain and frequency, I have a 10-turn potentiometer), and also have an option to connect an external voltage supply instead of using a battery for projects that I know will take a while. Having the 9v battery available for quick access and portability is very nice. I changed the code around just a bit so that the initial waveform is sine instead of square, and to allow a lower frequency (about 15hz). I changed freqTolerance to 1 since I upgraded the pots. I also used an actual switch for power (no reason other than just person preference). I use a lot of other equipment which has banana plug recepticals, so that is what I used here instead of a phone plug for universality. My enclosure is a 6.5" x 4.5" x 2.5" black plastic box. All of my parts, including the enclosure and arduino were purchased on Newark for about $75.

My only concern is that it doesn't output much current, but I'm sure I can come up with something to work around that. Great project, easy to build, works great! Thanks!

jim_lewis13 years ago
Fantastic instructable and really useful device produced. Any chance you can offer a kit of this?
amandaghassaei (author)  jim_lewis13 years ago
thanks! I was actually just talking about making a shield yesterday. stay tuned, hopefully I can get it done in the next month or so.

and if I wanted to add a more wave signal as would,? I need to make one but I asked to be 5 signs ... please help

AlecD12 months ago

Part number 8 needs a revision. 10.01µf 50V Ceramic Disc Capacitor does not exsist. However your link points to 1000pf. Im going to build with the 1000pf.

ggaldino1 year ago

any suggestions of how to implement a LCD displaying the wave parameters? all these timing interrupts may cause the lib to not function!

hemalchevli made it!1 year ago

Here is my version. I made a standalone system, no shields, blog


1-40 of 139Next »