## Saturday, August 11, 2018

### DIY Programmable Function Generator Using Arduino Uno

It is possible to use Arduino Uno as a function generator to produce low frequency waveform. The advantage of the approach is the possibility of producing customized, complex, time varying waveform. On the down side, it cannot produce waveform with very high frequencies. To get smoother waveform, the PWM output frequency of pin 3 of Arduino Uno is increased to approximately 8 kHz. It is also possible to use DAC to achieve better results.

Figure. The output of Arduino function generator measured with an oscilloscope.

To use Arduino as a function generator to produce your customized waveform, I have developed a library. It produces the waveform from the pin 3 of Arduino. It can also read analog inputs, A0 and A1, and print the voltages for the serial plotter with the baud rate of 115200. The steps for using the function generator library in Arduino programs are as follows.

### Step 1

Get the following library file and put it in the program folder.
CE_FGen.h (https://github.com/yan9a/arduino/blob/master/Function_Generator/CE_FGen.h)

### Step 2

Include the header file and declare an instance of the function generator class at the start of the Arduino program.
#include "CE_FGen.h"
CE_FGen fg;


### Step 3

In the setup function, initialize it with a sampling frequency. Sampling frequency of 500 Hz is used in this example. Then, set the waveform with type, frequency, peak to peak voltage, and offset value as follows.
void setup() {
fg.Begin(500); // ( sampling_freq )
fg.Set(CE_SINUSOIDAL,5,1,1); // ( waveform , wave_freq , v_peak2peak , v_average )
}

The types of waveform are listed below.
• CE_HARMONIC
• CE_SAWTOOTH
• CE_SINUSOIDAL
• CE_SQUARE
• CE_TRIANGULAR

### Step 4

Finally, in the loop function, continuously call Poll method to produce the waveform with the defined sampling rate.
void loop() {
fg.Poll();
}


### Test Program

An example test program is shown below.
#include "CE_FGen.h"
CE_FGen fg;
void setup() {
fg.Begin(500); // ( sampling_freq )
fg.Set(CE_SINUSOIDAL,5,1,1); // ( waveform , wave_freq , v_peak2peak , v_average )
}
void loop() {
fg.Poll();
}


Another example that produces all the waveform can be found at the following link.
Function_Generator.ino (https://github.com/yan9a/arduino/blob/master/Function_Generator/Function_Generator.ino)

### Test Circuit

To test the function generator, a small signal N channel MOSFET is used in a common source configuration to amplify the input signal and to produce inverted signal. The input biased gate voltage, vg, and the amplified inverted output voltage, vd, are read by analog inputs A0 and A1. Their volages can then be seen at the serial plotter at baud rate of 115200. The test circuit connection is shown below.

Figure. Test circuit

Figure. Schematic diagram for the test circuit

Before the input signal is applied, the quiescent drain output voltage is adjusted around its mid-point, 3V, by using the potentiometer. When the output of the function generator is connected to the gate input via a coupling capacitor, the amplified voltage can be seen at the serial plotter. If you want to have higher amplification, a bypass capacitor can also be put at the source. The following video demonstrates the function generator and waveform at its output.