Monday, November 17, 2025

Psuedo Spectrum Analyzer

Earlier this year I acquired two Elegoo MEGA2560 kits through Facebook market place. These kits are excellent entry points into the Arduino maker space. They come with boards and enough components and modules to keep the average nerd busy for awhile. Lately I've been looking at some of the modules that I didn't have strong thoughts on what to utilize them for and have started brainstorming ideas build some unique things. One of the modules in particular, the sound sensor module, got my attention. This module has a built in microphone and can be used for a variety of applications, such as a sound activated trigger or to capture audio for processing. After reading up on the module (this is one of the odd ball modules included in the kit that doesn't have a readily available board number associated with it), I decided to pair it with an old favorite of mine, the MAX7219 8x8 LED display. I used the MAX7219 to great effect in the cable box I built a couple of weeks ago and I love this one, in particular because it's super easy to get it working with an Arduino board and looks super cool in action. My goal was to create a sort of "spectrum analyzer" like you might see in older hi-fi stereo equipment racks. When I was a kid I had a spectrum analyzer program that I ran on my beloved Tandy Color Computer 2 that accepted input via a set of stereo RCA cables and displayed a dancing set of colorful bars that pulsed to the audio it was fed. This version would be much simpler. I would use the microphone from the sound sensor to listen for audio from the room it was setup in, and process the audio via a UNO R3 board, segmenting the sound into degrees of volume, and then display these lines of differing audio strength signals on the MAX7219.


 

Wiring up the components (the sound sensor and the MAX7219 display) was very straightforward. Both components are labeled clearly which pins do what (i.e. VCC, ground, etc.) unlike a couple of modules I've encountered in these kits (looking squarely at you DHT11 Temperature and Humidity Module). One everything was wired up it really was just a matter of getting the sketch pulled together to get the setup to capture the audio, "rank" the volume levels, and then display the results in an understandable way. There are essentially 6 steps in this process:

1. Read sound from microphone.

The sound sensor outputs and analog voltage that changes with the sound. It can output digital, but I ran into some issues getting that to reliably be read, so I opted for the easy route with analog output. The sound ranges in value from 0-1023.

 int raw = analogRead(micPin);

2. Compare the sound to a dynamic baseline.

A baseline is used to represent the normal quite noise in the room. This gives us the difference between the current sound and the background. The sketch allows the baseline to adapt over time, which keeps the analyzer stable, even if the room becomes noiser over time.

 int diff = abs(raw - baseline);

baseline = (baseline * 98 + raw * 2) / 100; 

3. Convert sound level into bar height.

The difference is scale into a value from 0-8, matching the height of the MAX7219's 8x8 matrix.

int level = diff / 8; 

4. Scroll the display to the left.

The entire display is shifted one column to the left. This is meant to mimic the way a real spectrum analyzer scrolls over time.

 scrollLeft();

5. Draw the new bar on the right side.

The new sound level becomes a vertical bar in the far right column. Each LED row is turned on or off depending on the bar height.

 drawColumn(level);

6. Update and refresh the MAX7219 display.

The display is updated all at once for a smooth animation.

 matrix.control(MD_MAX72XX::UPDATE, MD_MAX72XX::ON);

Results 

The result is a series of bars of different heights, continually scrolling to the left, with the bar height continually reacting to the volume of the sound it picks up. I can't really call this an actual spectrum analyzer, since there is only one sensor monitoring the room and not a series devoted to the various frequencies across the audio spectrum, but it still looks pretty cool. 


 

No comments:

Post a Comment

Housing a Linux computer inside a broken Atari 2600 cartridge

 I started thinking pretty soon after completing the custom cable channel project of housing a Raspberry Pi UNO into a repurposed cable box ...