Project tutorial
DIY Audio Signal Spectrum Analyzer

DIY Audio Signal Spectrum Analyzer © GPL3+

Very simple audio analyzer with changeable visual modes.

  • 5,655 views
  • 1 comment
  • 10 respects

Components and supplies

Necessary tools and machines

09507 01
Soldering iron (generic)

Apps and online services

About this project

A spectrum analyzer measures the magnitude of an input signal versus frequency within the full frequency range of the instrument.

In this project is presented a very simple way in which such a device can be made with the help of only a few components:

- Arduino Nano microcontroller

- 16X2 LCD display

- Capacitor 47 nF

- Trimer potentiometer 10 kOhm

The "FHTSpectrumAnalyzer" project was chosen as the source for creating the spectrum analyzer and minor changes were made to the code. The display connection via I2C was changed to 4-bit mode, the ADC reference voltage was switched to internal 1.1 V. You will also need the FHT.h library.

An audio signal directly through a 47 nF capacitor is fed to analog input A1, the spectrum analyzer has an automatic gain of the input signal, which improves the visual image of the spectrum analyzer.

You can also use the button to select one of the six visual modes.

Finally, the device is housed in a convenient box that I also use for multiple devices. Otherwise, the device can be used as an effective visual detail within a DIY amplifier or preamlifier. Schematic, code and libraries are given below.

Code

Arduino CodeC/C++
  #define AUTO_GAIN 1       // автонастройка по громкости
  #define VOL_THR 45        // порог тишины (ниже него отображения на матрице не будет)
  #define LOW_PASS 40       // нижний порог чувствительности шумов (нет скачков при отсутствии звука)
  #define DEF_GAIN 120       // максимальный порог по умолчанию 
  #define FHT_N 256         // ширина спектра х2
  #define LOG_OUT 1
  #include <FHT.h> 
  #include <EEPROM.h>
  #include <LiquidCrystal.h>
  LiquidCrystal lcd(12, 11, 5, 4, 3, 2);// RS,E,D4,D5,D6,D7
      byte posOffset[16] = {2, 3, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30}; // вч выше
      byte maxValue, maxValue_f,www=1,gain_sp = DEF_GAIN;
      float k = 0.1;
      int i1,yyy,spek;
      unsigned long gainTimer;

  void setup(){ 
        Serial.begin(9600);
        ADMUX  = 0b01100000; ADCSRA = 0b11010100; 
        lcd.begin(16, 2);
        pinMode(9,INPUT);
        spek = EEPROM.read(100);
}

  void loop(){
    if(digitalRead(9)==HIGH){spek++;EEPROM.update(100,spek); www=1;if(spek>5){spek=0;}delay(300);}

      if(spek==0&&www==1){
      byte v1[8] = {0, 0, 0, 0, 0, 0, 0, 4};
      byte v2[8] = {0, 0, 0, 0, 0, 0, 4, 4};
      byte v3[8] = {0, 0, 0, 0, 0, 4, 4, 4};
      byte v4[8] = {0, 0, 0, 0, 4, 4, 4, 4};
      byte v5[8] = {0, 0, 0, 4, 4, 4, 4, 4};
      byte v6[8] = {0, 0, 4, 4, 4, 4, 4, 4};
      byte v7[8] = {0, 4, 4, 4, 4, 4, 4, 4};
      byte v8[8] = {4, 4, 4, 4, 4, 4, 4, 4};
      lcd.createChar(0, v1);lcd.createChar(1, v2);lcd.createChar(2, v3);lcd.createChar(3, v4);lcd.createChar(4, v5);lcd.createChar(5, v6);lcd.createChar(6, v7);lcd.createChar(7, v8);
      www=0;}

      if(spek==1&&www==1){
      byte v1[8] = {0, 0, 0, 0, 0, 0, 0, 14};
      byte v2[8] = {0, 0, 0, 0, 0, 0, 14, 14};
      byte v3[8] = {0, 0, 0, 0, 0, 14, 14, 14};
      byte v4[8] = {0, 0, 0, 0, 14, 14, 14, 14};
      byte v5[8] = {0, 0, 0, 14, 14, 14, 14, 14};
      byte v6[8] = {0, 0, 14, 14, 14, 14, 14, 14};
      byte v7[8] = {0, 14, 14, 14, 14, 14, 14, 14};
      byte v8[8] = {14, 14, 14, 14, 14, 14, 14, 14};
      lcd.createChar(0, v1);lcd.createChar(1, v2);lcd.createChar(2, v3);lcd.createChar(3, v4);lcd.createChar(4, v5);lcd.createChar(5, v6);lcd.createChar(6, v7);lcd.createChar(7, v8);
      www=0;}

      if(spek==2&&www==1){
      byte v1[8] = {0, 0, 0, 0, 0, 0, 0, 10};
      byte v2[8] = {0, 0, 0, 0, 0, 0, 10, 10};
      byte v3[8] = {0, 0, 0, 0, 0, 10, 10, 10};
      byte v4[8] = {0, 0, 0, 0, 10, 10, 10, 10};
      byte v5[8] = {0, 0, 0, 10, 10, 10, 10, 10};
      byte v6[8] = {0, 0, 10, 10, 10, 10, 10, 10};
      byte v7[8] = {0, 10, 10, 10, 10, 10, 10, 10};
      byte v8[8] = {10, 10, 10, 10, 10, 10, 10, 10};
      lcd.createChar(0, v1);lcd.createChar(1, v2);lcd.createChar(2, v3);lcd.createChar(3, v4);lcd.createChar(4, v5);lcd.createChar(5, v6);lcd.createChar(6, v7);lcd.createChar(7, v8);
      www=0;}

      if(spek==3&&www==1){
      byte v1[8] = {0, 0, 0, 0, 0, 0, 0, 27};
      byte v2[8] = {0, 0, 0, 0, 0, 0, 27, 27};
      byte v3[8] = {0, 0, 0, 0, 0, 27, 27, 27};
      byte v4[8] = {0, 0, 0, 0, 27, 27, 27, 27};
      byte v5[8] = {0, 0, 0, 27, 27, 27, 27, 27};
      byte v6[8] = {0, 0, 27, 27, 27, 27, 27, 27};
      byte v7[8] = {0, 27, 27, 27, 27, 27, 27, 27};
      byte v8[8] = {27, 27, 27, 27, 27, 27, 27, 27};
      lcd.createChar(0, v1);lcd.createChar(1, v2);lcd.createChar(2, v3);lcd.createChar(3, v4);lcd.createChar(4, v5);lcd.createChar(5, v6);lcd.createChar(6, v7);lcd.createChar(7, v8);
      www=0;}

      if(spek==4&&www==1){
      byte v1[8] = {0, 0, 0, 0, 0, 0, 0, 31};
      byte v2[8] = {0, 0, 0, 0, 0, 0, 31, 31};
      byte v3[8] = {0, 0, 0, 0, 0, 31, 31, 31};
      byte v4[8] = {0, 0, 0, 0, 31, 31, 31, 31};
      byte v5[8] = {0, 0, 0, 31, 31, 31, 31, 31};
      byte v6[8] = {0, 0, 31, 31, 31, 31, 31, 31};
      byte v7[8] = {0, 31, 31, 31, 31, 31, 31, 31};
      byte v8[8] = {31, 31, 31, 31, 31, 31, 31, 31};
      lcd.createChar(0, v1);lcd.createChar(1, v2);lcd.createChar(2, v3);lcd.createChar(3, v4);lcd.createChar(4, v5);lcd.createChar(5, v6);lcd.createChar(6, v7);lcd.createChar(7, v8);
      www=0;}

      if(spek==5&&www==1){
      byte v1[8] = {0, 0, 0, 0, 0, 0, 0, 21};
      byte v2[8] = {0, 0, 0, 0, 0, 0, 21, 21};
      byte v3[8] = {0, 0, 0, 0, 0, 21, 21, 21};
      byte v4[8] = {0, 0, 0, 0, 21, 21, 21, 21};
      byte v5[8] = {0, 0, 0, 21, 21, 21, 21, 21};
      byte v6[8] = {0, 0, 21, 21, 21, 21, 21, 21};
      byte v7[8] = {0, 21, 21, 21, 21, 21, 21, 21};
      byte v8[8] = {21, 21, 21, 21, 21, 21, 21, 21};
      lcd.createChar(0, v1);lcd.createChar(1, v2);lcd.createChar(2, v3);lcd.createChar(3, v4);lcd.createChar(4, v5);lcd.createChar(5, v6);lcd.createChar(6, v7);lcd.createChar(7, v8);
      www=0;}
    
      analyzeAudio();   // функция FHT, забивает массив fht_log_out[] величинами по спектру

      for (int pos = 0; pos < 16; pos++) {  
        if (fht_log_out[posOffset[pos]] > maxValue) maxValue = fht_log_out[posOffset[pos]];
        lcd.setCursor(pos, 0);
        int posLevel = map(fht_log_out[posOffset[pos]], LOW_PASS, gain_sp, 0, 15);posLevel = constrain(posLevel, 0, 15);
        
  while(yyy<2){yyy++;delay(2);
    if (posLevel > 7) {lcd.write((uint8_t)posLevel-8);lcd.setCursor(pos, 1);lcd.write((uint8_t)7);} 
     else {lcd.print(" ");lcd.setCursor(pos, 1);lcd.write((uint8_t)posLevel); }}yyy=0;}

   if (AUTO_GAIN) {
    maxValue_f = maxValue * k + maxValue_f * (1 - k);
    if (millis() - gainTimer > 1500) {   
    if (maxValue_f > VOL_THR) gain_sp = maxValue_f;
      else gain_sp = 150;gainTimer = millis();}
      else {gain_sp = DEF_GAIN;}}
  }// loop

void analyzeAudio() { 
 while(i1 < FHT_N){i1++; 
    do{ADCSRA |= (1 << ADSC);} 
    while((ADCSRA & (1 << ADIF)) == 0);fht_input[i1] = (ADCL|ADCH << 8);}i1=0;
    fht_window(); // window the data for better frequency response
    fht_reorder(); // reorder the data before doing the fht
    fht_run(); // process the data in the fht
    fht_mag_log(); // take the output of the fht
}
LibraryC/C++
No preview (download only).

Schematics

Schematic
Untitled sketch bb 8zgnzgywpt

Comments

Similar projects you might like

DIY FFT Audio Spectrum Analyzer

Project tutorial by Mirko Pavleski

  • 20,923 views
  • 2 comments
  • 21 respects

32-Band Audio Spectrum Visualizer Analyzer

Project showcase by shajeeb

  • 118,426 views
  • 168 comments
  • 256 respects

2 x 16-Band Audio Spectrum Analyzer with LCD

by shajeeb and ThomAce

  • 11,859 views
  • 3 comments
  • 12 respects

RGB 32-Band Audio Spectrum Visualizer

Project tutorial by janux

  • 10,610 views
  • 5 comments
  • 9 respects

FHT Audio Spectrum Visualizer

Project tutorial by janux

  • 7,175 views
  • 25 comments
  • 11 respects

DIY Simple Autorange Capacitance Meter (10pF-10000microF)

Project tutorial by Mirko Pavleski

  • 10,868 views
  • 9 comments
  • 20 respects
Add projectSign up / Login