MSGEQ7 Frequency Analyzer

Hi,

I’m looking into making a sort of visual EQ for my next Arduino project. I’ve come across the MSGEQ7 (the part is avaliable on sparkfun)

I’ve been reading this website:

http://skoba.no-ip.org/msgeq7/

and it seems very interesting. My question seems a little simple…but I don’t have much programming knowledge.

I see that the code provided at the website prints the analog values between 0-1023 within an array. How would I write a code to read these values to control the brightness of an LED?

Here’s the code provided at the website:

int analogPin = 0; // read from multiplexer using analog input 0
int strobePin = 2; // strobe is attached to digital pin 2
int resetPin = 3; // reset is attached to digital pin 3
int spectrumValue[7]; // to hold a2d values

void setup() 
{
  Serial.begin(9600);
  pinMode(analogPin, INPUT);
  pinMode(strobePin, OUTPUT);
  pinMode(resetPin, OUTPUT);
  analogReference(DEFAULT);

  digitalWrite(resetPin, LOW);
  digitalWrite(strobePin, HIGH);

  Serial.println("MSGEQ7 test by J Skoba");
}

void loop() 
{
  digitalWrite(resetPin, HIGH);
  digitalWrite(resetPin, LOW);

  for (int i = 0; i < 7; i++)
  {
    digitalWrite(strobePin, LOW);
    delayMicroseconds(30); // to allow the output to settle
    spectrumValue[i] = analogRead(analogPin);

    // comment out/remove the serial stuff to go faster
    // - its just here for show
    if (spectrumValue[i] < 10)
    {
      Serial.print("    ");
      Serial.print(spectrumValue[i]);
    }
    else if (spectrumValue[i] < 100 )
    {
      Serial.print("   ");
      Serial.print(spectrumValue[i]);
    }
    else
    {
      Serial.print("  ");
      Serial.print(spectrumValue[i]);
    }
    
    digitalWrite(strobePin, HIGH);
  }
  Serial.println();
}

How would I write a code to read these values to control the brightness of an LED?

Assuming that you have an LED connected up to one of the PWM pins (through a resistor) then simply take the value, divide it by 4 and do an analogue write to the LED pin.

analogWrite(LEDpin, spectrumValue[0]); // will set the LED output to the value of the zeroth index of your array.

How would I write a code to read these values to control the brightness of an LED?

How ever you want. There are tons of examples of projects with flashing LEDs. Your two choices for brightness are: 1) create a loop that turns the LED on and off, or 2) Use PWM.

Ah makes sense now. Thanks!

I seem to be getting a bit of noise with the chip. I'm using 5 LEDs with the Arduino, and even with no signal from my computer (like music) the LEDs are very dim because the chip is outputting some noise. Can anyone help me with this? Or can this be a programming issue?

Video below:

http://www.youtube.com/watch?v=71kW44AkAAg&feature=player_profilepage

What value of current limiting resistor are you using on your LEDs?

Without a schematic (or block diagram) of how you hooked up your chip and without the code, it really isn't possible to determine which is causing problems.

What exactly does that mean?
For it to be valid you would have to connect the input of the MSEQ7 to ground.
Else, you are just measuring ambient noise and what you are seeing is normal.

I am using my computer as an input to a 3.5mm jack, connected to the MSGEQ7. While I am not sending a signal (playing music) into the chip, the LEDs remain slightly lit.

What value of current limiting resistor are you using on your LEDs?

Without a schematic (or block diagram) of how you hooked up your chip and without the code, it really isn’t possible to determine which is causing problems.

I connected the chip using the schematic in my first post:

http://skoba.no-ip.org/msgeq7/

For an input I am using a 3.5mm jack connected to my computer.

Also, for my LEDs I am using a 220 ohm resistor. Do you think this is too low of a resistance?

I have also provided my code below:

int analogPin = 0; // read from multiplexer using analog input 0
int strobePin = 2; // strobe is attached to digital pin 2
int resetPin = 3; // reset is attached to digital pin 3
int spectrumValue[7]; // to hold a2d values
int otherPin = 5;
int otherPin2 = 6;
int redPin = 9;
int greenPin = 10;
int bluePin = 11;

void setup() 
{
  Serial.begin(9600);
  pinMode(analogPin, INPUT);
  pinMode(strobePin, OUTPUT);
  pinMode(resetPin, OUTPUT);
  analogReference(DEFAULT);

  digitalWrite(resetPin, LOW);
  digitalWrite(strobePin, HIGH); 
  delayMicroseconds(35);
}

void loop() 
{
  digitalWrite(resetPin, HIGH);
  digitalWrite(resetPin, LOW);

  for (int i = 0; i < 7; i++)
  {
    digitalWrite(strobePin, LOW);
    delayMicroseconds(35); // to allow the output to settle
    spectrumValue[i] = analogRead(analogPin);

    //comment out/remove the serial stuff to go faster
    // - its just here for show
    if (spectrumValue[i] < 10)
    {
      Serial.print("    ");
      Serial.print(spectrumValue[i]);
    }
    else if (spectrumValue[i] < 100 )
    {
      Serial.print("   ");
      Serial.print(spectrumValue[i]);
    }
    else
    {
      Serial.print("  ");
      Serial.print(spectrumValue[i]);
    }
    
    digitalWrite(strobePin, HIGH);
    
    int otherout = spectrumValue[2]/4;
    int otherout2 = spectrumValue[3]/4;
    int redout = spectrumValue[4]/4;
    int greenout = spectrumValue[5]/4;
    int blueout = spectrumValue[6]/4;
    analogWrite(otherPin, otherout);
    analogWrite(otherPin2, otherout2);
    analogWrite(redPin, redout); // will set the LED output to the value of the zeroth index of your array.
    analogWrite(greenPin, greenout); 
    analogWrite(bluePin, blueout);
  }
  Serial.println();
}

Thanks for all the help.

When using Analog Inputs, it is not necessary to set the Pinmode (unless you are trying to define it as a Digital Input or Output.)

Your line: "pinMode(analogPin, INPUT);" in setup(), actually sets Digital Pin 0 to Input. Probably not related to what you are seeing, but not a good idea to be doing that. Also, instead of the scaling you are doing for the LED brightness, you might want to look at the map() function.

As Richard brings up, what happens when you short the input to ground? Do you still see the flickering? When your computer is connected to speakers can you hear high-frequency noise?

The scalling is right, and using /4 will result in a right shift operation that is very fast, and 1023/4=255, so no problem there, if its 0 the operation will just shift zeros, there are no divisions to throw errors. Your "problem" of having a signal diferent from 0 when no playing music is problem of the sound card and not from your circuit, its called dc offset and every sound card, in fact every device that plays audio have it, so dont worry about it.

Did you not read the part about shorting the input to the MSEQ7 or else you test is not valid? If you don't understand something don't just skip over it or we will assume you aren't serious and we will go and talk to other people who are more interested/interesting.

Sorry about that. I tried shorting the input to ground and I am still getting the LED flickering.

When your computer is connected to speakers can you hear high-frequency noise?

Yes, I do hear a high-frequency tone when I connect it to speakers. This noise is what I assume is the chip.

As for the map() function, I will look into it, although the main problem I am trying to address is getting rid of the chip noise.

Your "problem" of having a signal diferent from 0 when no playing music is problem of the sound card and not from your circuit, its called dc offset and every sound card, in fact every device that plays audio have it, so dont worry about it.

Even with no signal present (thus no sound/music) the LEDs are still flickering.

Here is a picture of my serial monitor when playing no music. I see that it is outputting values. Can I filter these values out using code?

https://www.sparkfun.com/datasheets/Components/General/MSGEQ7.pdf

Look at page 4 and tell us if you used the capacitors and resistors they recommend?

the link to no-ip.org is being blocked as a malware site.

Try the same shorting technique on the Arduino analog input pin.

Just tried this and the LEDs stopped flickering, meaning the output through the serial monitor continuously outputted "0." Does this mean the noise is coming from the Arduino itself?

Ok, good and bad news.

I started fresh and reconnected the chip. And it seems that the chip is not outputting any high frequency noise! :D It must've been a bad wiring issue.

But, I'm still having the same problem: I'm still getting a small output from my LEDs. Trying to short the input or short the Arduino analog pin 0 to ground doesn't work anymore, I'm beginning to think this could be a code issue.

It means the opposite. The noise is coming from the MSGEQ7.

Did you connect all the recommended capacitors as shown by the MSGEQ7 data sheet? Do you have another MSGEQ7 you can try?

I'm actually using a couple different values. I'm using a 270k resistor to 5v (pin 8) instead of a 200k and a 20k resistor to input (pin 5) instead of a 22k. I'm also using a 100 pico farad cap in pin 1 to ground instead of a 33 pico farad (trying to go to the electronics store tomorrow and look for one).

Any ideas?