Arduino Waveform Generator




In the lab, one often needs a repetitive signal of a certain frequency, shape and amplitude. It may be to test an amplifier, check out a circuit, a component or an actuator. Powerful waveform generators are available commercially, but it is relatively easily to make a useful one yourself with an Arduino Uno or Arduino Nano, see for example:

Here is the description of another one with the following features:

* Accurate waveforms: 8-bit output using R2R DAC, 256-sample shape

* Fast: 381 kHz sampling rate

* Precise: 1mHz steps frequency range. As accurate as the Arduino crystal.

* Easy operation: waveform and frequency settable with single rotary encoder

* Wide range of amplitudes: millivolts to 20V

* 20 pre-defined waveforms. Straightforward to add more.

* Easy to make: Arduino Uno or Nano plus standard components

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: Technical Considerations

Making an analog signal

One shortcoming of the Arduino Uno and Nano is that it does not have a digital-to-analog (DAC) converter, so it is not possible to make it output an analog voltage directly on the pins. One solution is the R2R ladder: 8 digital pins are connected to a resistor network so that 256 levels of output can be reached. Through direct port access, the Arduino can set 8 pins simultaneously with a single command. For the resistor network, 9 resistors with value R are needed and 8 with value 2R. I used 10kOhm as a value for R, that keeps the current from the pins to 0.5mA or less. I guess R=1kOhm could work as well, since the Arduino can easily deliver 5mA per pin, 40mA per port. It is important that the ratio between the R and the 2R resistors is really 2. That is most easily achieved by putting 2 resistors of value R in series, for a total of 25 resistors.

Phase accumulator

Generating a waveform then comes down to repetitively sending a sequence of 8-bit numbers to the Arduino pins. The waveform is stored in an array of 256 bytes and this array is sampled and sent to the pins. The frequency of the output signal is determined by how fast one advances through the array. A robust, precise and elegant way to do that is with a phase accumulator: a 32-bit number gets incremented at regular intervals, and we use the 8 most significant bits as the index of the array.

Fast sampling

Interrupts allow to sample at well-defined times, but the overhead of interrupts limit the sampling frequency to ~100kHz. An infinite loop to update the phase, sample the waveform and set the pins takes 42 clock cycles, thus achieving a sampling rate of 16MHz/42=381kHz. Rotating or pushing the rotary encoder causes a pin change and an interrupt that gets out of the loop to change the setting (waveform or frequency). At this stage the 256 numbers in the array are recalculated so that no actual calculations of the waveform need to be performed in the main loop. The absolute maximum frequency that can be generated is half of this, 190kHz, but then there are only two samples per period, so not much control of the shape. The interface thus doesn't allow to set the frequency above 100kHz. At 50kHz, there are 7-8 samples per period and at 1.5 kHz and below all 256 numbers stored in the array get sampled each period. For waveforms where the signal changes smoothly, for example the sine wave, skipping samples is no problem. But for waveforms with narrow spikes, for example a square wave with a small duty cycle, there is the danger that for frequencies above 1.5 kHz missing a single sample can result in a the waveform not behaving as expected

Accuracy of the frequency

The number by which the phase is incremented at each sample is proportional to the frequency. The frequency can thus be set to an accuracy of 381kHz/2^32=0.089mHz. In practice such accuracy is hardly ever needed, so the interface limits to set the frequency in steps of 1mHz. The absolute precision of the frequency is determined by the precision of the Arduino clock frequency. This depends on the Arduino type but most specify a frequency of 16.000MHz, so a precision of ~10^-4. The code allows to modify the ratio of the frequency and the phase increment to correct for small deviations of the 16MHz assumption.

Buffering and amplification

The resistor network has a high output impedance, so its output voltage quickly drops if a load is attached. That can be solved by buffering or amplifying the output. Here, the buffering and amplification is done with an opamp. I used the LM358 because I had some. It is a slow opamp (slew rate 0.5V per microsecond) so at high frequency and high amplitude the signal gets distorted. A good thing is that it can handle voltages very close to 0V. The output voltage is however limited to ~2V below the rail, so using +5V power limits the output voltage to 3V. Step-up modules are compact and cheap and feeding +20V to the opamp, it can generate signals with voltage up to 18V. (NB, the schematic says LTC3105 because that was the only step-up I found in Fritzing. In reality I used an MT3608 module, see pictures in the next steps). I choose to apply a variable attenuation the output of the R2R DAC then use one of the opamps to buffer the signal without amplification and the other to amplify by 5.7, so that the signal can reach a maximum output of about 20V. The output current is rather limited, ~10mA, so a stronger amplifier may be needed if the signal is to drive a large speaker or electromagnet.

Step 2: Required Components

For the core waveform generator

Arduino Uno or Nano

16x2 LCD display + 20kOhm trimmer and 100Ohm series resistor for backlight

5-pin rotary encoder (with integrated pushbutton)

25 resistors of 10kOhm

For the buffer/amplifier

LM358 or other dual opamp

step-up module based on the MT3608

50kOhm variable resistor

10kOhm resistor

47kOhm resistor

1muF capacitor

Step 3: Construction

I soldered everything on a 7x9cm prototype board, as shown in the picture. Since it got a bit messy with all the wires I tried to colour the leads that carry positive voltage red and those that carry ground black.

The encoder I used has 5 pins, 3 on one side, 2 on the other side. The side with 3 pins is the actual encoder, the side with 2 pins is the integrated pushbutton.On the 3-pin side, the central should be connected to ground, the other two to D10 and D11.on the 2-pin side, one pin should be connected to ground and the other to D12.

It's the ugliest thing I've ever made but it works. It'd be nice to put in an enclosure, but for now the extra work and cost doesn't really justify it. The Nano and the display are attached with pin-headers. I wouldn't do that again if I'd build a new one. I did not put connectors on the board to pick up the signals. Instead, I pick them up with crocodile leads from protruding pieces of copper wire, labelled as follows:

R - raw signal from the R2R DAC

B - buffered signal

A - amplified signal

T - timer signal from pin 9

G - ground

+ - positive 'high' voltage from the step-up module

Step 4: The Code

The code, an Arduino sketch, is attached and should be uploaded to the Arduino.

20 waveforms have been pre-defined. It should be straightforward to add any other wave. Note that the random waves fill up the 256-value array with random values, but the same pattern gets repeated every period. True random signals sound like noise, but this waveform sounds much more like a whistle.

The code sets a 1kHz signal on pin D9 with TIMER1. This is useful to check the timing of the analog signal. That is how I figured out that the number of clock cycles is 42: If I assume either 41 or 43, and generate a 1kHz signal, it clearly has a different frequency from the signal on pin D9. With the value 42 they match perfectly.

Normally, the Arduino interrupts every millisecond to keep track of time with the millis() function. This would disturb the accurate signal generation, so the particular interrupt is disabled.

The compiler says: "Sketch uses 7254 bytes (23%) of program storage space. Maximum is 30720 bytes. Global variables use 483 bytes (23%) of dynamic memory, leaving 1565 bytes for local variables. Maximum is 2048 bytes." So there is ample space for more sophisticated code.

Step 5: Usage

The apparatus can be powered simply through the mini-USB cable of the Arduino Nano. It is best done with a power bank, so that there is no accidental ground loop with the apparatus that it may be connected with.

When switched on it will generate a 100Hz sine wave. By rotating the knob, one of the other 20 wave types can be chosen. By rotating while pushed, the cursor can be set to any of the digits of the frequency, which can then be changed to the desired value.

The amplitude can be regulated with the potentiometer and either the buffered or the amplified signal can be used.

It is really helpful to use an oscilloscope to check the signal amplitude, in particular when the signal supplies current to another device. If too much current is drawn, the signal will clip and the signal is heavily distorted

For very low frequencies, the output can be visualised with an LED in series with a 10kOhm resistor. Audio frequencies can be heard with a speaker. Make sure to set the signal very small ~0.5V, otherwise the current gets too high and the signal starts clipping.

Electronics Tips & Tricks Challenge

Participated in the
Electronics Tips & Tricks Challenge

Be the First to Share


    • Made with Math Contest

      Made with Math Contest
    • Multi-Discipline Contest

      Multi-Discipline Contest
    • Robotics Contest

      Robotics Contest

    50 Discussions


    Question 10 days ago

    Thank you for posting this, I have built one (to the raw output stage) and it's a nice tool for the hobbyist. I have one small problem which I hope you can help me with :- the frequency on all ranges is some 1.2% low (83 rather than 100). Whilst I can always multiply the output by 1.2 to achieve the desired result I wonder if there is a solution in your software - you mentioned some adjustment in the body of your text - unfortunately I don't have the programming skills to determine what has to be done, can you advise me please?

    2 answers

    Answer 10 days ago

    Glad you made it!

    To calibrate the frequency, you can modify the number in line 144:

    to get frequencies that are 1.2% higher, replace the number 0.268435456 by 1.012*0.268435456=0.271656681.

    I am surprised to see such a large deviation. I have noticed that some cheap nano clones have tiny crystals, but I would have expected them to be still with 1 permille.

    Note that using the raw output will result in a distorted signal if it gets a load that is smaller or similar to the resistors used in the R2R DAC. So the signals may look OK on a scope but can't be used for driving a speaker or a LED


    Reply 9 days ago

    Thank you for your quick reply and for the info. Yes, I had noticed the distortion when applying even a light load and will apply amplification & buffering before using it in practice. For interest I have tried your generator with uno (authentic), nano and pro mini clones successfully. I will now alter your coding as suggested which I am sure will make it 100%. Thanks again.


    Question 18 days ago on Step 5

    how was the speaker connected? and also the capacitor? i wasn't able to see it in the video and in the pdf.

    3 answers

    Answer 14 days ago

    Hi, the speaker was connected straight to the buffered output, without a series capacitor. (The 1muF capacitor in the picture is just to smooth the power supply to the opamp). For high-power audio of course a series capacitor helps to avoid a DC component through the speaker, but for this little demo it also worked well without it.


    Reply 10 days ago

    may i know which part is the buffered output? and could you please send a pic of the connection of the speaker to the board or oscilloscope? just so it would be clearer to me. i'm getting quite confused, sorry. thank you so much for your help!


    Reply 10 days ago

    Hi, you can see it on the picture of the video: both the speaker and the scope are connected with a black alligator clip to a ground point and with a red alligator clip to output 'B' (for 'buffer') of the opamp.


    3 months ago

    hei. ...what is the max frequency of this setup ? can the frequency be increased only from code? if yes can you please show me how?
    And thank you for posting such a great project :)

    5 replies

    Reply 3 months ago

    max 100kHz. There is no point to go higher since the sampling rate is 381kHz, so already at 100kHz it only takes 3 to 4 out of the 256 samples.
    The sampling rate could be increased slightly by using a 16 or 24 bit phase accumulator, but it would be at the cost of a lot of precision, it'd be better to go to different hardware for the MHz range.


    Reply 3 months ago

    ok thanks for your answer.... can you tell me if the output of this generator can can drive a larger mosfet (irfp250)? i started building the generator but I still have a little work to do.


    Reply 3 months ago

    Hmm I have little experience with mosfets. I have used the IRLZ44N
    (in )
    The 'L' in IRLZ44N indicates it becomes fully conducting at logic levels (i.e. +5V)
    Yours has the 'F' so it may not fully conduct at 5V.

    Moreover, this generator has an analog output, you can't just connect it to a MOSFET and expect a high-current analog output. The MOSFETs are great for digital switching, analog DC amplification is a lot more involved!


    Reply 3 months ago

    ok thanks ..but can you give me an idea how to make this work ....perhaps using some transistors for boosting the signal?(i am new to electronics and i am just trying to learn)Thanks


    Reply 3 months ago

    The simplest circuit is a power-transistor inside an op-amp feedback loop, see

    However, it's hard to make it work in practice. I tried and failed. Beware that the buffer needs to dissipate a lot of power.
    Do you really need true analog signals? If you manage with PWM it'll be much easier. If it's for audio, it's better to get an off-the-shelf audio amplifier.


    Question 8 months ago

    my Arduino IDE can't compile the sketch. any idea?


    10 months ago

    This presents an oversimplification of the R2R network requirements for accuracy. The pin outputs need to be well matched, which is
    best done a light loads, so stick with the 10K values. To achieve 8-bit DAC accuracy and be monotonic (google it), the resistors' should be accurate to 1/512 or about 0.2%, so that makes them expensive.
    However, typical 1% or even 2% resistors have a resistor-to-resistor matching within a
    single batch that can be much better than their absolute accuracy. It’s also possible to “hand
    select” resistors using a DVM. Note that DVM accuracy is not as important as
    its repeatability and resolution, since it’s their ratios that is important here,
    but ALL resistors should be within 0.2% of each other! See: "resistor ladder" in Wikipedia. DACs need to be to monotonic to accurately reproduce signals without distortion.


    Question 10 months ago

    Could you please share the fritzing file? I want to create a PCB board using your model and it would be less error prone to get this right using the original design.

    1 answer

    Answer 10 months ago

    Done! beware that there may be mistakes in it! I drew it up after making the device and did not do any tests or simulations (if that is at all possible with Fritzing...)


    11 months ago

    Just a quick update on my purchase of a DSO138 oscilloscope. The eBay seller, cinnamon 1956-5, refunded me the cost of the case (look out for that misleading practice where the Selection box has more than one price, although it didn't when I placed my order, it did later, I'm taking screen prints from now on). The agreement was that I would buy the o'scope from him/her again (at a higher price for certain), which I've done. So I'm now waiting for another 16 days for the scope to get here (USA). I guess the case will serve as compensation for the time lost, but all is well.


    Question 12 months ago

    Thanks, rgco. I didn't spend too much time reading it because I am not really interested in building a waveform generator at this time BUT I am really interested in the oscilloscope off to the right. Did you build it or did you buy it already built? Any info on that would be greatly appreciated. You could probably do an Instructable on that. TIA, -- Ron

    1 answer