Go Down

Topic: Music reactive LED String (Read 527 times) previous topic - next topic

seebaer

Hey, I want to create a music reactive LED String with arduino.
 Now I got the question, which sensor I should use to identify the different frequencies in the music. I'm not quite sure, if the sound sensor by arduino is able to detect different frequencies or just noise "Yes" or "No". That's why I'm asking...

Wawa

Google "Arduino MSGEQ7" or "Arduino spectrum analyser".
Leo..

seebaer

Hey, at first thanks for the fast reply!
So I can't use the standard arduino sound sensor for this project? I have heared, that if I combine it with the fft library it could work as well.

wvmarle

There are various sound senors out there. Some will indeed give a simple on/off signal for "sound/no sound", others will give an analog signal for the volume, yet others (plain microphone w/pre-amplifier) may send out the actual sound signal.

So it depends on what sensor you're looking at, which you didn't specify.

The MSGEQ7 is a commonly used chip for sound visualisations, giving you the volume of a number of frequency bands.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

seebaer

Ok, I ment the arduino grove - sound sensor. The one you can buy on this page.

Does he give the volume of different frequencies?

Idahowalker

Using a module with the MSGEQ7 on board a DFRobot Audio Analyzer, the 7 frequencies produced are a sample of sound intensity as detected by the module.

I use, for the Audio Analyzer module the AudioAnalyzer.cpp - Library from DFRobot. Works good. If you are going to use an ESP32 better performance can be realized by modifying the library to use the ESP32 A:D API.

Using a WS2812B RGB LED Strip, I run 24 LEDS in 6 segments to display flashing lights as detected by the DFRobot module. Works great.

seebaer

For sure this would work, but I'd like to use only the grove sound sensor, if possible, because I don't want to spend a lot of money, and everything should be small...

So still, does the grove sound sensor give the volume of different frequencies (if I use the fft library)?

Idahowalker

I used the words "arduino grove sound sensor" in an internet search engine to get a number of relevant results.

seebaer

Idahowalker, when I ask here for something, its because I didn't find a suitable answer online. So please tell me why you can't simple answer my question if its so easy to find the answer.

Wawa

What did you try, and what is your experience
I would start with a simple sound sensor, volume only, and make that work,
before venturing into the much harder (code) fft world.
Arduino is all about trying things, and building bigger with the experience gained from simple projects.
If you're looking for a easier hardware and copy/paste solution, then spend the money and buy an MSGEQ7.
Leo..

seebaer

At this moment I'm just planning my project, because I just want to buy what I really need. I don't have a lot experience with arduino, my first project was a LED cube (worked well). I just wanted to know if I should/have to buy a MSGEQ7. As you said MSGEQ7 should be my way to go...

SteveMann

I am also interested in using sound to modulate a LED string- please keep us posted on your progress.
I am usually so far out of the box that most people don't know what I am talking about.

Please do not ask for help by PM. I will not respond.
If you need help, post a question on the appropriate forum.

Idahowalker

#12
Nov 14, 2019, 09:52 pm Last Edit: Nov 14, 2019, 09:57 pm by Idahowalker
I am also interested in using sound to modulate a LED string- please keep us posted on your progress.
Works great!
The code:
Code: [Select]
#include "sdkconfig.h"
#include "esp32/ulp.h"
#include "driver/rtc_io.h"
#include "esp_system.h" //This inclusion configures the peripherals in the ESP system.
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include "freertos/event_groups.h"
#include <Adafruit_NeoPixel.h>
#include "AudioAnalyzer.h"
////
/* define event group and event bits */
EventGroupHandle_t eg;
#define evtDo_AudioReadFreq       ( 1 << 0 ) // 1
////
TickType_t xTicksToWait0 = 0;
////
QueueHandle_t xQ_LED_Info;
////
const int NeoPixelPin = 26;
const int LED_COUNT = 24; //total number of leds in the strip
const int NOISE = 10; // noise that you want to chop off
const int SEG = 6; // how many parts you want to separate the led strip into
const int Priority4 = 4;
const int TaskStack40K = 40000;
const int TaskCore1  = 1;
const int TaskCore0 = 0;
const int AudioSampleSize = 6;
const int Brightness = 180;
const int A_D_ConversionBits = 4096; // arduino use 1024, ESP32 use 4096
////
Analyzer Audio = Analyzer( 5, 15, 36 );//Strobe pin ->15  RST pin ->2 Analog Pin ->36
// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
Adafruit_NeoPixel leds = Adafruit_NeoPixel( LED_COUNT, NeoPixelPin, NEO_GRB + NEO_KHZ800 );
////
int FreqVal[7];//create an array to store the value of different freq
////
void setup()
{
  eg = xEventGroupCreate();
  Audio.Init(); // start the audio analyzer
  leds.begin(); // Call this to start up the LED strip.
  clearLEDs();  // This function, defined below, de-energizes all LEDs...
  leds.show();  // ...but the LEDs don't actually update until you call this.
  ////
  xQ_LED_Info = xQueueCreate ( 1, sizeof(FreqVal) );
  //////////////////////////////////////////////////////////////////////////////////////////////
  xTaskCreatePinnedToCore( fDo_AudioReadFreq, "fDo_ AudioReadFreq", TaskStack40K, NULL, Priority4, NULL, TaskCore1 ); //assigned to core
  xTaskCreatePinnedToCore( fDo_LEDs, "fDo_ LEDs", TaskStack40K, NULL, Priority4, NULL, TaskCore0 ); //assigned to core
  xEventGroupSetBits( eg, evtDo_AudioReadFreq );
} // setup()
////
void loop() {} // void loop
////
void fDo_LEDs( void *pvParameters )
{
  int iFreqVal[7];
  int j;
  leds.setBrightness( Brightness ); //  1 = min brightness (off), 255 = max brightness.
  for (;;)
  {
    if (xQueueReceive( xQ_LED_Info, &iFreqVal,  portMAX_DELAY) == pdTRUE)
    {
      j = 0;
      //assign different values for different parts of the led strip
      for (j = 0; j < LED_COUNT; j++)
      {
        if ( (0 <= j) && (j < (LED_COUNT / SEG)) )
        {
          set(j, iFreqVal[0]); // set the color of led
        }
        else if ( ((LED_COUNT / SEG) <= j) && (j < (LED_COUNT / SEG * 2)) )
        {
          set(j, iFreqVal[1]); //orginal code
        }
        else if ( ((LED_COUNT / SEG * 2) <= j) && (j < (LED_COUNT / SEG * 3)) )
        {
          set(j, iFreqVal[2]);
        }
        else if ( ((LED_COUNT / SEG * 3) <= j) && (j < (LED_COUNT / SEG * 4)) )
        {
          set(j, iFreqVal[3]);
        }
        else if ( ((LED_COUNT / SEG * 4) <= j) && (j < (LED_COUNT / SEG * 5)) )
        {
          set(j, iFreqVal[4]);
        }
        else
        {
          set(j, iFreqVal[5]);
        }
      }
      leds.show();
    }
    xEventGroupSetBits( eg, evtDo_AudioReadFreq );
  }
  vTaskDelete( NULL );
} // void fDo_ LEDs( void *pvParameters )
////
void fDo_AudioReadFreq( void *pvParameters )
{
  int64_t EndTime = esp_timer_get_time();
  int64_t StartTime = esp_timer_get_time(); //gets time in uSeconds like Arduino Micros
  for (;;)
  {
    xEventGroupWaitBits (eg, evtDo_AudioReadFreq, pdTRUE, pdTRUE, portMAX_DELAY);
    EndTime = esp_timer_get_time() - StartTime;
    // log_i( "TimeSpentOnTasks: %d", EndTime );
    Audio.ReadFreq(FreqVal);
    for (int i = 0; i < 7; i++)
    {
      FreqVal[i] = constrain( FreqVal[i], NOISE, A_D_ConversionBits );
      FreqVal[i] = map( FreqVal[i], NOISE, A_D_ConversionBits, 0, 255 );
      // log_i( "Freq %d Value: %d", i, FreqVal[i]);//used for debugging and Freq choosing
    }
    xQueueSend( xQ_LED_Info, ( void * ) &FreqVal, xTicksToWait0 );
    StartTime = esp_timer_get_time();
  }
  vTaskDelete( NULL );
} // fDo_ AudioReadFreq( void *pvParameters )
////
//the following function set the led color based on its position and freq value
//
void set(byte position, int value)
{
  // segment 0, red
  if ( (0 <= position) && (position < LED_COUNT / SEG) ) // segment 0 (bottom to top), red
  {
    if ( value == 0 )
    {
      leds.setPixelColor( position, 0, 0, 0 );
    }
    else
    {
      // increase light output of a low number
      // value += 10;
      // value = constrain( value, 0, 255 ); // keep raised value within limits
      leds.setPixelColor( position, leds.Color( value , 0, 0) );
    }
  }
  else if ( (LED_COUNT / SEG <= position) && (position < LED_COUNT / SEG * 2) ) // segment 1 yellow
  {
    if ( value == 0 )
    {
      leds.setPixelColor(position, leds.Color(0, 0, 0));
    }
    else
    {
      leds.setPixelColor(position, leds.Color( value, value, 0)); // works better to make yellow
    }
  }
  else if ( (LED_COUNT / SEG * 2 <= position) && (position < LED_COUNT / SEG * 3) ) // segment 2 pink
  {
    if ( value == 0 )
    {
      leds.setPixelColor(position, leds.Color(0, 0, 0));
    }
    else
    {
      leds.setPixelColor(position, leds.Color( value, 0, value * .91) ); // pink
    }
  }
  else if ( (LED_COUNT / SEG * 3 <= position) && (position < LED_COUNT / SEG * 4) ) // seg 3, green
  {
    if ( value == 0 )
    {
      leds.setPixelColor(position, leds.Color( 0, 0, 0));
    }
    else //
    {
      leds.setPixelColor( position, leds.Color( 0, value, 0) ); //
    }
  }
  else if ( (LED_COUNT / SEG * 4 <= position) && (position < LED_COUNT / SEG * 5) ) // segment 4, leds.color( R, G, B ), blue
  {
    if ( value == 0 )
    {
      leds.setPixelColor(position, leds.Color( 0, 0, 0));
    }
    else //
    {
      leds.setPixelColor(position, leds.Color( 0, 0, value) ); // blue
    }
  }
  else // segment 5
  {
    if ( value == 0 )
    {
      leds.setPixelColor(position, leds.Color( 0, 0, 0)); // only helps a little bit in turning the leds off
    }
    else
    {
      leds.setPixelColor( position, leds.Color( value, value * .3, 0) ); // orange
    }
  }
} // void set(byte position, int value)
////
void clearLEDs()
{
  for (int i = 0; i < LED_COUNT; i++)
  {
    leds.setPixelColor(i, 0);
  }
} // void clearLEDs()
//////////////////////////////////////////////

Idahowalker

#13
Nov 14, 2019, 09:56 pm Last Edit: Nov 14, 2019, 10:00 pm by Idahowalker
I use a ESP32, a WS2812B RGB LED Strip, 2 X Adafruit Electret Microphone Amplifier - MAX9814 with Auto Gain Control [ADA1713], a DFRobot Audio Analyzer, and, of course, power.


I modified the #include "AudioAnalyzer.h" library to use ESP32 A:D API

Go Up