janux
Published © GPL3+

JX Audio Spectrometer

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.

BeginnerFull instructions provided1 hour3,146
JX Audio Spectrometer

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Oled Yellow-Blue Display 128x64 I2C (SH1106 Drivers)
×1
Resistor 4.75k ohm
Resistor 4.75k ohm
×3
Resistor 100k ohm
Resistor 100k ohm
×2
Capacitor 100 nF
Capacitor 100 nF
×2
Capacitor 47 µF
Capacitor 47 µF
×1

Story

Read more

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).

EAGLE Schematics

Breadboard wiring

Stand alone version

Code

JX Audio Spectrometer

C/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();  
}

Credits

janux

janux

3 projects • 18 followers
I have always been a technology enthusiast, at 13 I started using a soldering iron and I already knew what a transistor was.

Comments