FFT Spectrum analyzer with Arduino

Arduino FFT Spectrum Analyzer FFTASCII

Input mode
Not connected
Baud rate / port
I/O

Capture
Window function

Test signals (50 kHz)

USB throughput0 B/s
📈 Time domain: A1 voltage
Scroll to zoom · drag to pan · Shift+scroll zooms vertically · double-click to reset
📊 FFT magnitude spectrum
Need at least 2 samples
Scroll to zoom · drag to pan · Shift+scroll zooms vertically · double-click to reset
📐 FFT phase spectrumPhase is meaningful only at strong magnitude peaks
Need at least 2 samples
Scroll to zoom · drag to pan · Shift+scroll zooms vertically · double-click to reset

🔧 Sketch reference Use FFTSpectrumAnalyzer_Due.ino for binary/Due  |  the ASCII sketch for Uno/Nano/Mega

⚡ BINARY: Arduino Due (50 kHz, native USB)
// FFTSpectrumAnalyzer_Due.ino
// Connect via the Due NATIVE USB port (near the power jack)
// The baud rate setting is irrelevant for native USB

#define ANALOG_PIN   A1
#define SAMPLE_RATE  50000UL   // 50 kHz -> 25 kHz Nyquist
#define BATCH_SIZE   256       // samples per USB write

// Header:  0xAA 0x55 0xAA 0x55 + uint32 sample rate
// Payload: raw uint16 little-endian, 12-bit ADC values
// Reference voltage: 3.3 V (AR_DEFAULT on Due)

// To change the sample rate:
//   5 kHz  -> SAMPLE_RATE  5000UL
//  25 kHz  -> SAMPLE_RATE 25000UL
//  80 kHz  -> SAMPLE_RATE 80000UL  (max reliable)
//
// Data rate: 50000 x 2 bytes = 100 KB/s
// Native USB capacity is about 1000 KB/s, so ~10% used
📝 ASCII: Uno / Nano / Mega / Due programming port (5 kHz, 500000 baud)
// Upload this sketch, select ASCII mode in the web app,
// set the baud rate to 500,000, then press Connect.

const int ANALOG_PIN = A1;
const int BATCH_SIZE = 50;    // samples per line
const int SAMPLE_US  = 200;   // 200 us -> 5 kHz sample rate
                              // Nyquist = 2.5 kHz

// Text output is about 7 bytes per sample:
//   5 kHz x 7 bytes = 35 KB/s = about 350,000 baud
// So 115200 baud only carries about 1.6 kHz of samples.

void setup() {
  Serial.begin(500000);
  analogReference(AR_DEFAULT); // 3.3 V on Due, 5 V on Uno
}

void loop() {
  for (int i = 0; i < BATCH_SIZE; i++) {
    int raw = analogRead(ANALOG_PIN);
    // Due: 12-bit (0-4095), Uno: 10-bit (0-1023)
    // Due:  float v = raw * (3.3 / 4095.0);
    // Uno:  float v = raw * (5.0 / 1023.0);
    float v = raw * (3.3 / 4095.0);
    Serial.print(v, 4);
    if (i < BATCH_SIZE - 1) Serial.print(',');
    delayMicroseconds(SAMPLE_US);
  }
  Serial.println();
}
Want a higher sample rate? Reduce SAMPLE_US and keep the baud rate above the data rate. At 1,000,000 baud you can use SAMPLE_US=100 (10 kHz sample rate, 5 kHz Nyquist).
Pick the sample rate from your signal: the Nyquist theorem needs a sample rate of at least 2 x f_signal. A 1 kHz input sampled at exactly 1 kHz aliases to DC, so there is no visible sine wave and no FFT peak. Use a sample rate of 5 kHz or more for a 1 kHz signal (5x oversampling is a good rule).

FFT Spectrum Analyzer with Arduino & Microcontrollers

Real-time frequency domain analysis using the Fast Fourier Transform — no software installation required

What Is the Fast Fourier Transform (FFT)?

The Fast Fourier Transform (FFT) is an efficient algorithm that converts a time-domain signal — a sequence of measurements over time — into its frequency-domain representation. Instead of seeing how a signal changes moment to moment, the FFT reveals which frequencies are present and how strong each one is. It is the mathematical equivalent of separating a musical chord into its individual notes.

The FFT achieves this in O(N log N) operations rather than the O(N²) required by the naive Discrete Fourier Transform (DFT), making real-time spectrum analysis practical on modest hardware and in the browser. The output is two spectra: the magnitude spectrum (signal power at each frequency) and the phase spectrum (the phase offset of each component).

How It Works — From Sensor to Spectrum

The analyzer follows a simple pipeline: a sensor measures a physical quantity, a microcontroller samples the signal through its ADC (Analog-to-Digital Converter), the samples are streamed over USB serial to the browser, and the browser computes the FFT and draws the live spectrum — all with no software to install.

  1. A sensor converts a physical phenomenon (distance, vibration, sound, voltage) into an electrical signal.
  2. The microcontroller ADC samples the signal at a fixed rate (e.g. 20 Hz, 1 kHz) and formats readings as comma-separated values.
  3. Samples are sent over USB serial to the browser via the Web Serial API (Chrome / Edge).
  4. The browser applies the Cooley-Tukey FFT, zero-padded to the next power of 2, and plots the magnitude and phase spectra in real time.
Arduino FFT Analyzer


Connecting Sensors — Examples

Any sensor whose output can be read as a voltage or a digital value can feed this analyzer. Common examples include:

  • HC-SR04 ultrasonic sensor — measures distance; reveals vibration and motion frequencies up to ~10 Hz
  • ADXL335 / MPU-6050 accelerometer — detects mechanical vibration and shock up to several hundred Hz
  • Electret microphone + amplifier — captures audio and speech signals up to several kHz
  • ACS712 current sensor — analyzes motor current harmonics and PWM switching frequencies
  • Photodiode / LDR — detects light flicker, LED PWM frequencies, or optical modulation
  • Any analog voltage (0–5 V / 0–3.3 V) — power supply ripple, bio-signals (ECG/EEG with proper isolation), strain gauges, and more

Connect analog sensors to an ADC pin (e.g. A0 on Arduino) and read them with analogRead(). For signals centered around 0 V, add a Vcc/2 bias so the signal stays within the ADC input range.

Not Just Arduino — Any Microcontroller Works

Because the browser simply reads ASCII numbers (or raw 16-bit binary samples) over USB serial, any microcontroller or single-board computer that supports USB CDC can act as the data source — including:

  • Arduino (Uno, Nano, Mega, Due, MKR) — most accessible starting point
  • ESP32 / ESP8266 — Wi-Fi streaming option, 12-bit ADC
  • Raspberry Pi Pico — MicroPython support, 12-bit ADC at up to 500 kSPS
  • Teensy 4.x — 600 MHz CPU, ideal for high-speed audio FFT
  • STM32 Nucleo — professional 12-bit ADC at up to 5 MSPS
  • Raspberry Pi — with an external SPI ADC (e.g. MCP3208), stream data from Python
  • BeagleBone, Nordic nRF52840, and other platforms with USB serial output

Studying Analog Signals with the ADC

The microcontroller's built-in ADC turns this tool into a versatile signal analyzer for any electrical analog signal. Route a voltage signal into an analog pin and the FFT will reveal its frequency content immediately. This makes it possible to study:

  • DC power supply switching ripple and harmonics
  • Motor current waveforms and bearing fault signatures
  • Audio signals, speech formants, and instrument tones
  • Biomedical signals such as ECG / EEG envelopes (with appropriate isolation)
  • Seismic or structural strain signals from geophones or strain gauges
  • Any periodic or quasi-periodic voltage in the 0–Vcc range

What Engineers and Scientists Can Learn from the Spectrum

Frequency-domain analysis with the FFT is a standard tool across engineering and science disciplines. With this analyzer, professionals and students can:

  • Identify resonant frequencies in structures, mechanical systems, or electronic circuits
  • Detect harmonic distortion — peaks at 2f, 3f, 4f reveal nonlinear behavior or fault signatures
  • Characterize noise — distinguish white noise (flat spectrum) from 1/f noise or narrowband interference
  • Diagnose machinery faults — bearing defects, imbalance, and misalignment each produce characteristic spectral patterns
  • Perform modal analysis on bridges, beams, or buildings by exciting them and reading their natural frequencies
  • Verify signal purity — check oscillators, filters, and amplifiers against their expected frequency response
  • Study biological rhythms — heart rate variability, neural oscillations, and respiratory cycles all have spectral signatures

The magnitude spectrum shows dominant frequencies and their relative strength. The phase spectrum reveals timing relationships and system response characteristics — especially valuable at resonance peaks where phase shifts by ±90°.

Bottom line: This FFT Spectrum Analyzer transforms a $5 sensor and any USB microcontroller into a real-time signal processing laboratory — accessible to engineers, scientists, educators, and hobbyists, directly in the browser with zero installation.

Post a Comment

Previous Post Next Post