Moving Multiple Servos To Music Using MSGEQ7 Audio Analyzer

Hello,

I'm working on a project that has multiple servos controlled by an Arduino Uno and a DFplayer which
plays music stored on a SD card.

I'd like to be able to obtain unique patterns of servo movements for each song.

So far, I've tried 2 methods with no good results, i.e. No unique movement pattern for each song.

  1. Connecting the DAC pin of the DFplayer, reading and mapping the values for servo movement.
  2. Using a KA2284 signal meter module which is no good since it works with the loudness.

I'm aware that servos are too slow to follow the music but a facsimile movement will be enough.
I've searched and read about beat detection but I don't know how to implement it ?!?

Is it possible to use a MSGEQ7 audio analyzer to achive my goal or is there another way I'm unaware of ?

I'd really appreciate your guidance.

I can't tell from that description what your goal actually is. "unique patterns" based on what? How many servos and should they all be doing the same thing or all different things?

I'm not sure that beat detection will do anything for uniqueness. Loads of music is in 4/4 with a limited range of beats per minute.

An MSGEQ7 will let you do something based on the amplitude of different frequency bands within the music. If that's your goal then it probably will help.

Steve

Thanks Steve.

I meant unique patterns of servo movement based on the music that is being played.
"How many servos and should they all be doing the same thing or all different things? " all or some, I’ll decide later.

Beat detection could be one way of doing it, I think. If I play a tango, servos will move different than if I play Rock and Roll ?!? Just don’t know how to do it.

Any hint on doing it with MSGEQ7 ? How would you do it ?

Regards.

  1. Using a KA2284 signal meter module which is no good since it works with the loudness.

If you want the frequency information you can use the MSGEQ7 (hardware) or FFT or FHT (software).

Apparently, there are lots of fake/reject/defective MSGEQ7 chips out there so if you go that way make sure to buy from a reliable supplier.

Connecting the DAC pin of the DFplayer, reading and mapping the values for servo movement

Have you "looked at" those values with the serial monitor? And do the numbers look useful for whatever you're trying to do? (i.e. You can run the Analog Read Serial Example, but take-out the delay()?

You should add a bias circuit or protection circuit because to the Arduino's input because it can be damaged by the negative half of the audio waveform. And, you are sampling a waveform so the raw readings will "look random" (within a range depending on volume), even with a constant tone.

The MSGEQ7 takes care of that. It puts-out a varying, positive, DC voltage that "easy" to read.

If you are using FFT/FHT, you'll need the bias circuit because a negative-voltage protection circuit distorts the signal, changing the frequency content.

If you are just looking for amplitude (loudness) the easiest thing is to look for the peaks.

No matter what you do, test your input (audio signals) and output (servo movement) separately before trying to put them both together. I know you're having trouble with the input, but can you "fake it" and control the servos under software control with no real audio input?

Beat detection could be one way of doing it, I think. If I play a tango, servos will move different than if I play Rock and Roll ?!? Just don't know how to do it.

You can't detect the "style" of music, but look on YouTube for "Arduino Spectrum Analyzer" to get some ideas of what you can do. A real spectrum analyzer is a measurement tool that shows you the amplitude and frequency content of a signal, but these Arduino projects are mostly lighting effects. You should be able to get an idea of what you can do using servos instead of LEDs/lights.

...I've made several lighting effects with just the amplitude information (no frequency information) and I've got enough different effects and random options to keep things "interesting" so maybe you could do that with servos.

Thanks Doug.

I'd try FFT or FHT but I don't have the knowledge and couldn't find a tutorial on how to do it.
Any hint ? Can Arduino Uno do that ?

How do I add a bias circuit ?

I'll " .... look YouTube for "Arduino Spectrum Analyzer", will let you know.

I did not mean the " style " of music, I meant beat.

Regards.

Can you picture your desired servo movements for us? What do you wish to happen say for the intro part of "Bohemian Rhapsody"? Each servo responds to its own frequency band loudness?

Hello John,

Please take a look at this project :

I made a similar panel as a gift to a school. Doug, the author was very kind in helping me out.
I was so happy to see the joy the kids had, even more with the special need ones.

As you can see, writing code to perform servo pattern movement is pretty tedious and after a while, kids
get tired of same movements. So, I thought about adding music to the panel and .... what if the servos moved to the music.

Let's say I play Beethoven's 5th symphony. It starts by ta ta ta tam, ta ta ta tam ... and some or all servos
(will decide later) go from 0 to 180 degree.
Bohemian Rhapsody's intro could maybe just do 0 to 90 or to whatever comes close to the song.

Of course I need unique patterns for every song otherwise , no fun.

I don't really know if this can be done but I'm trying my best to donate joy to these kids.

Regards.

You can have the sort of thing that you have finally described with just a loudness signal like the one you said you couldn't use. Beat is just changes in loudness.

But the only way you'll get "unique" patterns is by choosing your range of songs carefully or using something other than JUST the beat. E.g. just about every modern dance track has the same 4 beat bar at a fixed beats/minute (BPM). They will all look exactly the same. Lots of pop songs are very similar too.

OTOH you could add in something as simple as a bit of randomness so the movements are based on the beat but they move round to different servos or groups of servos each time you play a new song. Then there are all sorts of possibilities.

Steve

Let's say I play Beethoven's 5th symphony. It starts by ta ta ta tam, ta ta ta tam ... and some or all servos
(will decide later) go from 0 to 180 degree.
Bohemian Rhapsody's intro could maybe just do 0 to 90 or to whatever comes close to the song.

Whatever comes close to the song? What on earth does that mean? How is a simple 0 to 90 degrees turn of a servo horn related to music? Looking at the moving servos while the music is playing, what are we supposed to see that makes us feel the movement is depending on the music? You haven't told us that. How does a 180 degree turn relate to Beethoven and 90 degrees to Bohemian rhapsody?

Thanks John, Steve.

I did try a KA2284 hack described here :

http://buttonbanger.com/?page_id=137

for a talking skull that uses loudness but that gives almost the same movements for every song.
Maybe I could try your suggestion and use some randomness.

".... How is a simple 0 to 90 degrees turn of a servo horn related to music? "

well, let me explain better. in Beethoven's ta ta ta tam you'd have servos going from 0 to 180 to 0 without delay 3 times and on the "tam" go to 180 and stay for a second or 2. Ok, please forgive me, I know this might sound childish and servos are not fast enough, but it was just an example to explain myself better.

Now, given your expertise on the subject, please tell me if FFT or FHT is a better solution. I appreciate your help and guidance.

FFT or FHT adds a lot of complexity but very little value for what you want to do.

If you go back to the MSGEQ7 idea that will allow you to run up to 7 sets of servos each showing the "beat" but as it shows up in different frequency ranges. You probably only want to use 2 or 3 of them maybe so one set reflects what the bass/bass drum is doing, one for the mid-range and another for the very high frequency instruments.

I've never tried it, certainly not with servos which as you said yourself are not fast enough to follow most music, unless they're only doing little twitches rather than big moves. But may be worth getting one just to see what you can get out of it. It's an interesting project.

Steve

Ok. Since you want several servos, I bet you want different frequency bands. I see two ways to go. Either you read the audio data, run some FFT on it to turn it into info about the sound level or pressure or loudness in each frequency band. Or you create a multiple band analog filter and have each filter send analog data to its analog input on Arduino.
And instead of turning the servo horn 180 degrees, have it turn only 18 degrees. That's 10 times faster. With mechanics light enough, you can still have big movement.

Thank you both.

I'll try MSGEQ7 idea but I only have the chip and not the module. From what I've read, to wire the chip
I'll need resistors, capacitors etc. With the Coronavirus lock down here in Italy I can't buy them locally. So I think is better to order the module. Unfortunately, I can't find one here in Italy. It seems like I need to get one from China with 20-30 days for delivery.

While waiting, I'll study FFT.

In the meantime how do I " .... create a multiple band analog filter " ?

Gentlemen, I really appreciate your help and hope you'll walk me through this.
It will be so nice to donate joy to the kids when they'll be back to school after the Coronavirus
nightmare they are living in.

Create several narrow band filters:
https://www.dspguide.com/ch19/3.htm

... Like I said, I've never done anything with frequency, but I've studied it...

While waiting, I'll study FFT.

In the meantime how do I " .... create a multiple band analog filter " ?

You can build analog filters with [u]op-amps[/u], or there are specialized filter chips. For multiple bands you need multiple bandpass filters. That's WAY more complicated than using the MSGEQ7.

Digital filters (and FFT) are a specialized area of programming called DSP (Digital signal processing).

FFT is similar to a digital filtering.

FFT gives you the amplitude of multiple frequency bands which they call "bins".

Depending on the resolution (and processing power and some other compromises) you can get an almost unlimited number of frequency bands (bins). Usually, you have many bins which are are combined for fewer "filter bands" (for a couple of reasons).

FFT is more mathematically complex and software intensive than filtering, but there are software libraries so FFT should be easier.

With software filters you might be on your own. You can find general examples for digital filters (in C++ etc), but it wouldn't be that easy to "port" to the Arduino. Plus, multiple bandpass filters would present an "interesting" multitasking challenge. :wink:

Beat detection could be one way of doing it, I think. If I play a tango, servos will move different than if I play Rock and Roll ?!?...

...Let's say I play Beethoven's 5th symphony. It starts by ta ta ta tam, ta ta ta tam ... and some or all servos
(will decide later) go from 0 to 180 degree.
Bohemian Rhapsody's intro could maybe just do 0 to 90 or to whatever comes close to the song.

You can't really tell what song is playing by "analyzing" the audio. (There are powerful servers on the Internet that can do it.).

So, I suggested you check YouTube to see what people were doing with lighting effects but if you want "see" the spectrum information for you own music [u]Audacity[/u] can show you the [u]spectrum[/u] of the selected audio or a [u]spectrogram[/u] of the whole file. And, it normally shows you the regular waveform.

There is a 3rd-party [u]Spectrum Analyzer[/u] plug-in that will show the changing spectrum in real time as the audio plays. If you have Windows and you want to try it, download and install the 23-bit VST version (Audacity for Windows is 32-bits). If you need help for that there is an Audacity forum.

Thank you all for your guidance.
Please give me some time to try your suggestions.
Will keep you posted.
In the meantime, please stay safe.

Hi all,

I finally received my MSGEQ7 spectrum module.
Now I need to find the way to implement it for the project
but I'm lost.

Looking around I could only find this module used with LEDs and not servos
and the music is picked up by headphone jack or a sound module.

I'm using a DfPlayer and do not want to use a microphone in order to avoid surrounding sounds.
Then comes the part of translating the data I get from the analyzer to servo movement.

Appreciate your help. Thanks.

So show us some code you've found that looks like it uses the module to do something interesting with LEDs.

Then we can maybe help you convert that to doing something interesting with servos. E.g. if it uses analogWrite() to modulate the intensity of LEDs it's not a big deal converting that to servo.write() to waggle servos.

Steve

Break up your project in at least two parts.

  1. read the MSGEQ7 chip; dump the values to the Serial monitor (you should get 7 values of 0-1023 for each complete reading).

  2. control the servos based on it. The simplest way would be to map() those analog readings to an angle value for the servos.

Read it 2-5 times a second (probably something like this considering the speed of servos), set the servos to those angles.

Remember to have sufficient power supply for your servos! That's easily 1-2A per servo, so 7 servos means a 10-15A 5-6V power supply. Go for the highest voltage your servos can handle, as the higher the voltage the faster they move. Some servos of course are faster than others, too, so select your parts carefully.

Hello and thank you both.

I still have not made the hardware.
Looking around for code that writes the values to the serial monitor I've found the following:

http://nuewire.com/info-archive/msgeq7-by-j-skoba/

Not sure how to map those values for servo angles, it's a bit over my rookie head.

In another project by Adafruit :

https://learn.adafruit.com/piccolo/code

They mention "Using the normal Arduino analogRead() function would be much too slow for sampling audio. Instead, a feature of the microcontroller’s analog-to-digital converter called free-run mode is utilized..... "
..... The raw audio samples are converted into a frequency spectrum using a fast Fourier transform or FFT...."

This is even more complicated for me.

Regards.