Arduino Waveform Generator




About: I'm a Research Engineer at Adobe. Previously, I was a grad student at the Center for Bits and Atoms at MIT Media Lab. Before that, I worked at Instructables, writing code for the website and iOS apps and m...
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)

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Prepare Arduino Proto Shield

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

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

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

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

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

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

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

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

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

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

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

Connect ground to pin 4 of the IC socket.

Step 13: Amplifier: Part 3

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Drill four holes in the back panel and secure with screws.

Step 28: Add Knobs

Screw knobs on the three potentiometers.

Step 29: 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.

LED Contest with Elemental LED

Participated in the
LED Contest with Elemental LED

Hurricane Lasers Contest

Participated in the
Hurricane Lasers Contest

5 People Made This Project!


  • Made with Math Contest

    Made with Math Contest
  • Multi-Discipline Contest

    Multi-Discipline Contest
  • Robotics Contest

    Robotics Contest

183 Discussions


5 months ago

I added some components, that are explained in the text, in the schematic.
I hope is clearer to anyone who wants to make this.


4 years ago on Introduction

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.

3 replies

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


Reply 5 months ago

Do as the error sais:
const byte sine20000[] PROGMEM = { ...
byte sine20000[] PROGMEM = { ...


Question 8 months ago on Step 18

"Wire the buttons". Um, little help? Wire them how? Wiring them from 5v to ground just shorts 5v to ground when you press the button. This is not how you wire a button, what are you talking about? Did you mean to add a pullup resistor but forgot to mention it? Also, you list a 10.01uF cap but you mean 0.01. Oh, and a schematic would be nice...

1 answer

Answer 7 months ago

The schematic is at the top, and it shows tat the buttons are pulled high via 10k resistors on the arduino-side. The other end of the button goes to ground.
yeah, 10.01 was a typo. Is should be a 0.01uF ceramic cap. 10nF, or labelled 103


Answer 7 months ago

Of course. The nano has the same ucontroller on board as an UNO.


10 months ago

Good day.
Excellent instructable, the function generator works great.
However, I tried using a Leonardo, and it is not working as expected, all other parts being identical. Has anybody tried that succesfuuly?


1 year ago on Step 10

Text in step 10 says . . . . use 300 Ohm for low pass filter, but the image shows 10k resistance


1 year ago on Step 8

Text in step 8 is not confirmed by the image, brown/black/orange is 10k and not 20k

Adi M

4 years ago on Step 25

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 :)

2 replies
CheeseduckAdi M

Reply 4 years ago on Introduction

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.


Question 1 year ago

Are you sure of the capacitor value for the anti-aliasing low-pass filter? An R–2R network has an output impedance of R (10 kΩ here) so the 300 Ω resistor is negligible and could be omitted. With a 10 nF capacitor, the cut-off frequency is slightly above 1.5 kHz, which is way too low. IMHO 330 pF is much more suitable, even 1 nF is probably fine.

Another important thing is the accuracy of the R–2R resistors. I see you used 5 % resistor which is too coarse. It may lead to significant errors on the most significant bits, causing large unwanted steps and even creating a non-monotonic curve. In my project I used a set of 1 % resistors, and manually matched a few of the 10 and 20 kΩ with an ohmmeter. These ones were installed on the 3 highest bits to make the MSBs as accurate as possible.

In the end my project ended quite different of yours: I used a cheap Arduino Nano, I get rid of the interrupts (causing a significant overhead) to poll a 200 kHz timer. The higher sampling rate gave me a better waveform accuracy in the high frequencies, as well as more stability. But I had to interleave pin polling, complicating the code (all integer-based, floating point was too costly). I reduced the sine wavetable to 4096 samples, which is enough with 8-bit data (making it larger doesn’t improve the accuracy in a measurable way). I used a more common linear potentiometer (B-type) for the frequency and did the exponential mapping in software. I built a 3rd-order Sallen-Key low-pass filter for the anti-aliasing, and made it bypassable to allow testing sharp edges with the pulse and saw waveforms (leaving just a single pole to filter out HF above a few MHz). I also used two spare 2N5088 to add simple buffers instead of the LM386. And there is just a single button to cycle through the waveforms, so I could add more of them if I want without changing the front panel.



Anyway your design was very instructive (it’s my first Arduino project) and inspiring, and I am grateful for this.


Question 1 year ago

What is the 220uF capacitor connected to the 20k resistor and the output of the opamp and then how to calculate the value of these capacitors for higher frequency eg for 1Mhz because I was inspired by your project and I would also build my own generator with that I would have the frequency set up using buttons and also use the LCD display.

yet evrything is on paper because there is a lot of work on table at work


1 year ago

please anyone can send Waveform Generator full code to my email..
i hope anyone ill send me that code.Thank you very much....


Question 1 year ago on Step 2

I am new to arduino and I would like to restrict the signal amplitude on my function generator to reflect biopotentials in the range of 10-100 microVolts. Does anyone have any suggestions?