MSGEQ7 chip to make graphic visualizer

Posted on
  • Hello,
    I've bought a MSGEQ7 chip (https://www.sparkfun.com/datasheets/Comp­onents/General/MSGEQ7.pdf) and wired it up following this http://nuewire.com/info-archive/msgeq7-b­y-j-skoba/

    See my wiring here: http://i.imgur.com/jUXKgNn.jpg?2
    I ported the following code from the same tutorial:

        var strobe = B8, // strobe pins on digital B8
        res = B9, // reset pins on digital B9
        audioIn = A0, // DC out from MSGEQ7 chip
        levels = Array(),
        band=0;   
    
        function onload() {
          digitalWrite(strobe, LOW);
          digitalWrite(res, LOW);
        }
        onload();
    
    
        function readMSGEQ7() {
          // Reset chip
          digitalWrite(res, HIGH);
          digitalWrite(res, LOW);
    
          band = 0;
          var loop = setInterval(function(){
            digitalWrite(strobe, LOW);
            // Get outputted value and store it
            levels[band] = analogRead(audioIn);
            digitalWrite(strobe, HIGH);
        
            // Stop after 7 loops
            band++;
            if(band >= 7) { clearInterval(loop); }
          }, 0.04); // 40 microseconds
        }
    
        function tick(){
          readMSGEQ7();
      
         // Debugging
          console.log(levels[0] + '-' + levels[1] + '-' + levels[2] + '-' + levels[3] + '-' + levels[4] + '-' + levels[5] + '-' + levels[6]); 
        }
        setInterval(tick, 1000);
    

    The console.log() output looks like this: http://pastebin.com/DVMBQQ4Z
    All of the numbers are very low and they don't seem to change when I in-decrease the volume or mute completely. After a few seconds it crashes but I think that's more to do with the console.log getting too big.

    With the same wiring on Arduino and the code from the tutorial it seems work correctly, outputting higher/lower values depending on the music playing or not at all.
    This makes me think it must be the JS code. Can you see any issues with it? I'm a little unsure about my use of the delayed loop to poll the chip but without a microdelay function I'm not sure how else to do it.
    Any help you can offer would be great.
    Thanks

  • The arduino code has a delay between strobe and read. Your code does not. Could that be it? Maybe you want to "offset" your loop, so the delay between iterations falls the same place as the delay the arduino code.

    Though this is terrible practice on the espruino and contrary the event based philosophy... You can do this (i imagine this might be particularly useful for testing in particular so you can see if lack of delay some point is the problem - then if it is, you can go do it the right way):

    function delay (t) { //since it's used with getTime, t is in *seconds*.
        var et=getTime()+t; 
    	while (getTime() < et) { 
    		var x = 5^5; 
    	}
    

    Also, you know that analogRead on espruino returns a value between 0 and 1? I don't know if this is the same as the arduino

  • AnalogRead on arduino returns a value between 0 and 1023.

    Sacha

  • I imagine @DrAzzy/@Sacha are right here - analogRead on Espruino returns a value between 0 and 1. All you need to do to scale it up is to multiply it by 1024.

    The JS code looks good - the interpreter produces a little delay in the execution so the fact that you're not delaying after the strobe probably isn't a big deal.

    That doesn't explain the crash though. I'm trying it here and it's been fine for a few minutes so far... Are you using an up to date firmware? It could be there was a bug in an older version.

  • I've fixed the crash, it was because I was re-declaring loop each time rather than once and then overriding it.

    I understand analogRead returns values between 0-1 but the values I get are so low I tihnk it's just background static. Changing the audio-in volume should change values at least.
    As DrAzzy suggested I don't have a delay after the read so I'll implement that this evening and let you know if it works.

  • Ahh, great - did it come up with any kind of error, or did it just crash without warning?

    I just realised something - try replacing HIGH with 1 and LOW with 0. Espruino doesn't define those constants, so they'll just be being read as undefined which will produce a logic 0.

  • No warning in the console or editor it just crashed after a few seconds.
    I'll change the HIGH/LOW settings and try again.

  • SUCCESS!
    Using DrAzzy's delay() I've got it working! http://pastebin.com/jw361rcN

    The only thing I'm confused about is if I leave the music on mute I see random small flashes on each band from time-to-time. If I wire up my headphones the wrong way around I can hear the static noise from the chip and/or Espruino which match the random flashes perfectly. Any ideas what this could be and how to stop it? Everything is grounded on the Espruino via a breadboard, not sure if this is the cause and solution.

  • The static noise is a bit strange... Have you tried running it all from a battery in case it's some power supply issue? You could try adding a capacitor across the power lines on the breadboard?

    What size delay did you have to add? Personally, I'd try add code that's a bit more like:

          band = 0;
          var loop = setInterval(function(){
            if ((band&1)==0) {
              digitalWrite(strobe, LOW);
            } else {
              // Get outputted value and store it
              levels[band>>1] = analogRead(audioIn);
              digitalWrite(strobe, HIGH);
            }
            // Stop after 7 bands
            band++;
            if(band >= 14) { clearInterval(loop); }
          }, 0.02); // 20 microseconds
    

    Unlikely to solve the static, but it's better practice than adding a delay :)

  • Hi Gordon,
    That interval loop is very similar to what I wrote before, wonder why it didn't work then. Bad chip I think, I bought 5 off eBay and only 2 of them work correctly.
    I fixed the static noise by powering Esruino from the wall rather than the PC.

    Thanks everyone for you help, I've now got a kickass graphic visualizer. Here is a quick videos of it in action: http://youtu.be/oADZs-v-OUE

    (the code)
    If you'd like Gordon, I'd love to write a tutorial on making one for your site.

  • That's great! I bet it's pretty brutal when you change the colour values from 0.05 to 1 :)

    It'd be great if you could write a tutorial for it! There's a bit on how to submit one here if it helps? http://www.espruino.com/Writing+Tutorial­s

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

MSGEQ7 chip to make graphic visualizer

Posted by Avatar for Owen @Owen

Actions