Project tutorial
JX Audio Spectrometer

JX Audio Spectrometer © GPL3+

A small gadget that uses an OLED display and the FFT library to generate the audio spectrum. Add a pinch of life to your music.

  • 521 views
  • 0 comments
  • 5 respects

Components and supplies

About this project

Just a simple gadget


Using any Arduino model and the ArduinoFFT library, a 0.96 inch two-color OLED display, you can make this nice spectrometer, an interesting alternative to the classic VU Meter.

After trying it on a breadboard I made a stand alone prototype, burning an ATmega328 as clearly visible in the initial photo.

The problem is being able to fit the code into the Arduino memory, since most of the ram is occupied by the display frame buffer. In this regard, I suggest you use an I2C SH1106 display. I have not tried with the hardware but the SSD1306 model library only apparently takes up less memory at the time of compilation, but once the sketch has been executed this stops, just for the creation of the buffer at runt time.

Be careful as there are identical displays on the market but with the VCC and GND pins reversed.

Observing the code you will notice that the cycle that draws the vertical bars starts from the value 2 instead of zero. This is not an oversight but I did this on purpose for the simple reason that the first two sampled frequency bands contain too much low frequency noise. Probably also because I connected it to the audio output of the PC and there is an impedance matching problem. Furthermore, strangely, the display I purchased has a horizontal resolution of 127 Pixel (from 0 to 126) instead of 128 as stated, if you try to write to column 127 this is plotted on the left side of the display, before column 0. It could be a problem caused by the SH1106 library which is basically a modified Adafruit_SSD1306 library.

Code

JX Audio SpectrometerC/C++
/*
-----------------------------------------------------------------------------
 * JX AUDIO SPECTROMETER                                        (C)Janux 2021
-----------------------------------------------------------------------------
 * This program is opened under license policy of following terms.

   Copyright (C) 2021, Janux, all right reserved.

 * This program is a free software and there is NO WARRANTY.
 * No restriction on use. You can use, modify and redistribute it for
   personal, non-profit or commercial use UNDER YOUR RESPONSIBILITY.
 * Redistributions of source code must retain the above copyright notice.
-----------------------------------------------------------------------------
*/
#include <Wire.h>
#include <arduinoFFT.h>
#include <Adafruit_SH1106.h>
Adafruit_SH1106 display(-1);
#define SAMPLES 64
double vReal[SAMPLES];
double vImag[SAMPLES];
byte peak;
long maxpeak;
char buf[5];

arduinoFFT FFT = arduinoFFT();

void setup() {  
  byte x = 0;
  analogReference(EXTERNAL);  
  display.begin(SH1106_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();   
  display.fillRect(0, 0, display.width() - 2, 11, WHITE);
  display.setTextColor(BLACK);
 
  x =  2; display.setCursor(x, 2); display.print(F("JX"));
  x = 18; display.setCursor(x, 2); display.print(F("AUDIO"));
  x = 52; display.setCursor(x, 2); display.print(F("SPECTROMETER"));

  for (byte i = 0; i < SAMPLES / 2 - 1; i++) {
      display.drawFastHLine(i * 4 + 1, display.height() - 1, 3, WHITE);
    } 
  display.setTextColor(WHITE);   
  display.display();
}

void loop() {  
  for (byte i = 0; i < SAMPLES; i++) {
    vReal[i] = analogRead(A0);
    vImag[i] = 0;    
  }

  FFT.DCRemoval();
  FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
  FFT.Compute(vReal, vImag, SAMPLES, FFT_FORWARD);
  FFT.ComplexToMagnitude(vReal, vImag, SAMPLES);
   
  display.fillRect(0, 12, display.width() - 2, display.height() - 13, BLACK);
  for (byte i = 0; i < SAMPLES / 2 - 1; i++) {        
    peak = map(vReal[i+2], 0, 1024, 0, 52);    
    display.fillRect(i * 4 + 1, abs(52 - peak) + 12, 3, peak, WHITE);
  }  
  maxpeak= FFT.MajorPeak(vReal, SAMPLES, 5000);  
   
  sprintf(buf,"%04li",maxpeak);  
  display.setCursor(72,16);    
  display.print(F("Peak:"));
  display.print(buf);  
  display.display();  
}

Schematics

Video Demo
The background track is a fraction of The Dividing Line from Genesis album Calling All Stations (1997). (All rights reserved by the legitimate owners).
jx_audio_spectrometer_j7FZyhWo8B.mp4
EAGLE Schematics
Jxas it2tlrlraz
Breadboard wiring
Jxas breadboard bb 1qu9aqsbau
Stand alone version
Jxas standalone 4c2r2qzmpt

Comments

Similar projects you might like

RGB 32-Band Audio Spectrum Visualizer

Project tutorial by janux

  • 12,653 views
  • 9 comments
  • 11 respects

FHT Audio Spectrum Visualizer

Project tutorial by janux

  • 8,689 views
  • 27 comments
  • 13 respects

32-Band Audio Spectrum Visualizer Analyzer

Project showcase by shajeeb

  • 130,520 views
  • 172 comments
  • 271 respects

2 x 16-Band Audio Spectrum Analyzer with LCD

by shajeeb and ThomAce

  • 12,586 views
  • 4 comments
  • 12 respects

echoTrek - Digital Delay / Echo - Audio Effects with Arduino

Project in progress by CesarSound

  • 8,911 views
  • 15 comments
  • 29 respects

DIY Audio Signal Spectrum Analyzer

Project tutorial by Mirko Pavleski

  • 5,926 views
  • 1 comment
  • 10 respects
Add projectSign up / Login