Skip to content
Hibiiro

HIBIIRO

Arduino Audio Synthesis with the Mozzi Library: Sine Wave & Potentiometer Vibrato

Introduction

Synthesizers have always fascinated me, and I wanted to try generating synth-like sounds directly on an Arduino Uno using the open-source Mozzi sound synthesis library.

Parts & Environment

  • 1× Arduino Uno R3 (or compatible board)
  • 1× Small Speaker
  • 1× 10 kΩ Trimmer Potentiometer (103 code = 10 kΩ)
  • Jumper Wires
  • Arduino IDE 2.3.3 with Mozzi v2.0.1

1. Speaker Wiring & 440 Hz Sine Wave Sketch

Connect the speaker between Arduino PWM audio output pin D9 and GND:

Arduino Uno speaker wiring on pin D9 for Mozzi
#define MOZZI_CONTROL_RATE 64    
#include <Mozzi.h>
#include <Oscil.h> 
#include <tables/sin2048_int8.h> 

Oscil <SIN2048_NUM_CELLS, MOZZI_AUDIO_RATE> aSin(SIN2048_DATA);

void setup(){
  startMozzi(); 
  aSin.setFreq(440);
}

void updateControl(){
}

AudioOutput updateAudio(){
  return MonoOutput::from8Bit(aSin.next()); 
}

void loop(){
  audioHook(); 
}

2. Adding a 10 kΩ Potentiometer to Control Vibrato Depth

Next, wire a 10 kΩ trim pot across 5V and GND with its center wiper connected to analog pin A0 to modulate the vibrato depth in real time:

Potentiometer wiring to Arduino A0 for Mozzi vibrato
#define MOZZI_CONTROL_RATE 128
#include <Mozzi.h>
#include <Oscil.h>
#include <tables/sin2048_int8.h>

Oscil <2048, MOZZI_AUDIO_RATE> aSin(SIN2048_DATA);
Oscil <2048, MOZZI_CONTROL_RATE> kVib(SIN2048_DATA);

float centre_freq = 440.0;
float depth;

void setup(){
	kVib.setFreq(6.5f);
	startMozzi(MOZZI_CONTROL_RATE);
}

void updateControl(){
  depth = analogRead(0) * 0.01;
	float vibrato = depth * kVib.next();
	aSin.setFreq(centre_freq+vibrato);
}

AudioOutput_t updateAudio(){
	return MonoOutput::from8Bit(aSin.next());
}

void loop(){
	audioHook();
}

Here is a quick video demo of turning the potentiometer knob to sweep the vibrato depth: