ECE 372 · Spring 2024 · University of Arizona · Team of 4 · Team lead

Audio Synthesizer

A granular synthesizer on a bare ATmega2560: five knobs, two decaying grains and six scale modes, with every peripheral driven at the register level. I led the team, designed the software architecture and wrote the sound engine.

  • C++
  • ATmega2560
  • AVR registers
  • Timers & interrupts
  • PWM audio
  • ADC
  • SPI
  • PlatformIO

The box in 3D

A teammate designed a 3D-printed box for the synthesizer, and in 2026 I rebuilt it in CAD. Open it up, look through the walls, or press the mode button to step through the scales.

In use

The finished synthesizer being played: the sync knob sweeps the pitch, and the rings from the speaker on the left close up as it rises. The LCD names the mode and the matrix dances.

Drag to turn the model, scroll to zoom, middle-drag to move it.Drag to turn the model, pinch to zoom, two fingers to move it.

About this model: rebuilt from memory in 2026, sized to fit the parts

No drawing, photo or file of the original box survives, so I rebuilt it in CAD from memory; the printed parts are printable solids and the parts inside are simplified models. From memory: the LCD at the left of the front with the LED matrix above it, a shallow inset for the knobs, clips holding the Arduino, a soldered perfboard joining the wires, shelves for the mode button and the battery switch, a back compartment for the 9 V battery, and a speaker behind a grille (but not which face it was on). Chosen to fit: every size (the box is 170 × 125 × 105 mm), the knob layout, the 40 mm speaker and its place on the left, the type of switch, the battery door, the base plate and its screws, the wiring paths, and the four pinned supports the Mega stands on besides the clips. Inside, the switch sits on the battery's red lead to the Mega's VIN, and every part reaches the Mega through the perfboard. The body prints on its top with no supports, and four M3 screws in heat-set inserts hold on the base plate.

Play it

The firmware's sound engine, ported line for line to JavaScript. Turn the knobs or press the mode button, and the scope shows what the audio interrupt writes to the speaker pin.

Current mode: Anti Log
Sync A4 500
Grain 1 pitch A3 300
Grain 1 decay A2 40
Grain 2 pitch A1 380
Grain 2 decay A0 60
Mode pin 19
new grains
grain 1
grain 2

Drag a knob or use the arrow keys. A higher reading means a lower pitch in this code. Hz assume a 31.25 kHz sample rate.

Serial monitor

About this simulation: the team's code, running in your browser

From the code: the knobs' pins and scaling, the six modes, their tables and button order, the LCD text, the LED matrix's frames and rate, the serial monitor line, and the Timer 3 interrupt's arithmetic, run sample by sample with the same wrapping 16-bit counters. Its quirks are kept as the code has them (the next section lists them). Assumed: the code never sets Timer 3's TOP, so the real sample rate isn't known, and the Hz readouts and Listen use 31.25 kHz, the rate Auduino's pitch tables were made for. Which way each knob turned on the box depends on wiring nobody recorded, the knobs here don't jitter, a press skips the debounce delay, and Listen is quieter and cleaner than the speaker was.

The brief

The ECE 372 final project asks each team to build a system in which a sensor input controls an output device, with at least one part talking over a serial protocol. I led a team of four that built an audio synthesizer on the ATmega2560 of an Arduino Mega, and it worked at the demo.

At a glance

Course
ECE 372, Spring 2024
Team
Four students; I led it
Chip
ATmega2560 on an Arduino Mega
Controls
Five knobs and a mode button
Result
Worked at the demo

Apart from serial debug output, the code does not use the Arduino library. Every peripheral (the ADC that reads the knobs, the timers, PWM audio, the button interrupt, SPI and the LCD) has its own small driver that writes the chip's registers directly.

Block diagram: five knobs feed the ADC and a mode button feeds INT2 on the ATmega2560; the main loop passes pitch and decay values to a Timer 3 interrupt that drives a speaker over PWM; a 16 by 2 LCD shows the mode and an 8 by 8 LED matrix on SPI shows an animation driven by Timer 0
How the parts connect; the audio path is in blue.

How it makes sound

The sound engine is a port of Peter Knight's open-source Auduino, a granular synthesizer, rewritten for this chip's Timer 3 and our own drivers. It is built from 16-bit phase accumulators: counters that add a fixed step every sample and wrap around, so the step sets the frequency. Each time the sync accumulator wraps, two grains restart at full volume. Each grain is a triangle wave at its own pitch that fades exponentially at a rate set by its own knob. The two are summed, scaled to 8 bits and written to the PWM compare register.

Three traces over about a thousand samples: a fast triangle wave that fades quickly, a slower one that fades more slowly, and their sum; dashed lines mark the moments both restart
The engine's own arithmetic: both grains restart at every sync period (dashed).

All of this runs in the Timer 3 overflow interrupt, once per PWM period, so it has to be short: shifts, adds and a few small multiplies, with no division. The main loop does the slow work, reading the five knobs and turning each reading into a phase step or decay rate for the interrupt to pick up.

Modes, button and display

The sync knob can sweep pitch smoothly through an anti-log table or snap to notes: every semitone, major or minor diatonic, or major or minor pentatonic. Each of the six modes is a lookup table of phase steps, and a small mode class holds the current mode and maps the knob reading through its table. The mode button raises external interrupt INT2, which drives a four-state debounce machine (wait for press, debounce, wait for release, debounce); each press moves to the next mode.

The numbers

Modes6
Debounce states4
LCD16×24-bit mode
LED matrix8×8six frames, Timer 0
Diatonic notes reached54 of 76
Highest output126 of 255

A 16×2 character LCD in 4-bit mode shows the current mode's name, and an 8×8 LED matrix on the SPI bus plays a six-frame dancing figure, advanced by a Timer 0 interrupt.

Porting the code to JavaScript in 2026 showed some quirks, kept in the simulation as the code has them. "Minor Pentatonic" reads Auduino's pentatonic table, not the team's minor one. The diatonic modes reach only the first 54 of their 76 notes. A higher knob reading gives a lower pitch, and at the far end of the sync knob the note modes give a step of 0, so no new grain starts and the synth goes quiet. The output never rises above 126 of the PWM's 255.

My part

Besides leading the team, I designed the software architecture: the main loop reads the knobs and hands values to the audio interrupt, and the code splits into a driver library for each piece of hardware plus a class for the mode state. I wrote the sound side: the grain engine in the Timer 3 interrupt, the pitch tables and most of the main loop. I also fixed the scaling of the ADC readings and added the Timer 0 animation. Teammates wrote the LCD, LED matrix and button drivers.

What I took from it

Split the work by how fast it has to happen. The audio interrupt runs every sample and can afford only shifts and adds; anything slower, like reading knobs and looking up tables, belongs in the main loop, handing finished numbers across. That one boundary shaped the whole program.

A driver per peripheral is also a way to split a team. Each driver had a clear edge, so teammates could write the LCD, matrix and button code while I wrote the sound engine.

And sounding right is not the same as being right. The synth worked at the demo with a mode reading the wrong table, scales cut short and a sample rate the code never set. The ear forgives a lot; porting the code line by line is what showed them.

Where it goes next

If I picked it back up, the first pass would fix those quirks: set Timer 3's TOP so the sample rate is known, point each mode at its own table, let the scales reach all their notes, keep the sync knob from stalling the grains, and use the PWM's full range.

The same structure, a phase accumulator stepped in a timer interrupt, is how direct digital synthesis signal generators work, so the code would carry over to a small function generator. It would also take a MIDI input in place of the sync knob with little change to the engine.

Want the details behind this one, or to talk about similar work?