Function generator with DDS
Build it step by step
0 of 7 doneWire the module
SPI and power.
Set a frequency
Write the three words.
Check on a scope or sound card
Confirm 1 kHz.
Add the encoder
Change the frequency.
Add the step and shape buttons
Click and long press.
Add the display
Show frequency and shape.
Add the level pot
Output through a capacitor.
How do you build a function generator with DDS?
A DDS module makes sine, square and triangle waves from 1 Hz to 1 MHz, set with an encoder. You need Arduino Nano, Function generator (DDS), Rotary encoder module (KY-040), SSD1306 OLED Display, Potentiometer 10 kΩ and Electrolytic capacitor 10 µF. It takes 7 steps and about 150 minutes.
- Level
- Intermediate
- Time
- About 150 min
- Parts
- 6
Wiring
| From | To | Why |
|---|---|---|
| AD9833 VCC and GND | Nano 5V and GND | Power |
| AD9833 FSYNC | Nano D10 | Chip select |
| AD9833 SCLK | Nano D13 | SPI clock |
| AD9833 SDATA | Nano D11 | SPI data |
| AD9833 OUT | 10 uF to the top of a 10 k pot | Output (wiper is the signal) |
| Encoder CLK and DT | Nano D2 and D3 | Rotation |
| Encoder SW | Nano D4 | Button, to GND when pressed |
| OLED SDA and SCL | Nano A4 and A5 | I2C |
Code
Direct digital synthesis builds a wave by counting through a table of numbers very fast, and the number you write sets how quickly it counts, which sets the frequency. The AD9833 chip does this with a 25 MHz clock, so the Nano only has to send three 16-bit words over SPI to change the frequency. The sine and triangle come out around 0.6 V peak to peak, and the square wave swings the full supply; the pot after the capacitor lets you turn the level down, and the capacitor removes the small DC offset. The output is weak and can only drive a high-impedance input such as an oscilloscope or an amplifier; a speaker will not work directly. A proper buffer needs an op-amp on a dual supply, which is left out here. Frequencies over a few hundred kilohertz show the limits of the 25 MHz clock: the wave has fewer points per cycle, so check it on a scope. Mind that the module is for lab use, not for connecting to mains or a mains-powered device. Arduino IDE, board "Arduino Nano" (try the Old Bootloader option if the upload fails).
Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
// DDS function generator: AD9833 over SPI, KY-040 encoder to set the frequency, click to change the step, long press for the shape.
// Libraries: Adafruit SSD1306, Adafruit GFX. Board: Arduino Nano. Most AD9833 modules have a 25 MHz crystal.
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
const byte FSYNC = 10, CLK_PIN = 2, DT_PIN = 3, SW_PIN = 4;
const float MCLK = 25000000.0;
long freq = 1000;
const long STEPS[] = {1, 10, 100, 1000, 10000, 100000};
byte stepIdx = 3;
byte wave = 0; // 0 sine, 1 triangle, 2 square
const char* NAMES[] = {"Sine", "Triangle", "Square"};
volatile int delta = 0;
void write16(uint16_t w) {
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE2));
digitalWrite(FSYNC, LOW); SPI.transfer16(w); digitalWrite(FSYNC, HIGH);
SPI.endTransaction();
}
void setOutput() {
uint32_t word = (uint32_t)((double)freq * 268435456.0 / MCLK); // 2^28 / MCLK
uint16_t ctrl = 0x2000; // B28: two writes make one frequency word
if (wave == 1) ctrl |= 0x0002; else if (wave == 2) ctrl |= 0x0028;
write16(ctrl);
write16(0x4000 | (word & 0x3FFF));
write16(0x4000 | ((word >> 14) & 0x3FFF));
}
void isr() { delta += (digitalRead(DT_PIN) != digitalRead(CLK_PIN)) ? 1 : -1; }
void show() {
oled.clearDisplay(); oled.setTextColor(SSD1306_WHITE);
oled.setTextSize(2); oled.setCursor(0, 0); oled.print(freq); oled.print(" Hz");
oled.setTextSize(1); oled.setCursor(0, 30); oled.print(NAMES[wave]);
oled.setCursor(0, 44); oled.print("Step "); oled.print(STEPS[stepIdx]); oled.print(" Hz");
oled.display();
}
void setup() {
pinMode(FSYNC, OUTPUT); digitalWrite(FSYNC, HIGH);
pinMode(CLK_PIN, INPUT_PULLUP); pinMode(DT_PIN, INPUT_PULLUP); pinMode(SW_PIN, INPUT_PULLUP);
SPI.begin();
write16(0x0100); // reset
attachInterrupt(digitalPinToInterrupt(CLK_PIN), isr, FALLING);
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
setOutput(); show();
}
void loop() {
static unsigned long pressedAt = 0;
noInterrupts(); int d = delta; delta = 0; interrupts();
if (d != 0) { freq = constrain(freq + d * STEPS[stepIdx], 1L, 1000000L); setOutput(); show(); }
if (!digitalRead(SW_PIN)) { if (pressedAt == 0) pressedAt = millis(); }
else if (pressedAt) {
if (millis() - pressedAt > 600) { wave = (wave + 1) % 3; setOutput(); }
else stepIdx = (stepIdx + 1) % 6;
pressedAt = 0; show(); delay(50);
}
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| 1 kHz sine | Scope shows 1 kHz within 0.1 percent |
| Click | Step changes |
| Long press | Shape changes |
| Turn to 100 kHz | Frequency follows |
If it does not work
No output
Check FSYNC and that the module has power.
Wrong frequency
Check the module crystal frequency.
Distorted wave
Use a high-impedance load.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the function generator with DDS?
Arduino Nano, Function generator (DDS), Rotary encoder module (KY-040), SSD1306 OLED Display, Potentiometer 10 kΩ and Electrolytic capacitor 10 µF. Function generator (DDS): Or an AD9833 module.
How long does the function generator with DDS take to build?
About 150 minutes for a intermediate maker, in 7 steps.
How do I know the function generator with DDS works?
1 kHz sine: Scope shows 1 kHz within 0.1 percent. Click: Step changes. Long press: Shape changes.
No output: what should I check?
Check FSYNC and that the module has power.
Wrong frequency: what should I check?
Check the module crystal frequency.
Distorted wave: what should I check?
Use a high-impedance load.
