sound sensor not reading sound levels

Hi!

I have been trying to get my sound sensor to work properly but i can’t seem to figure it out.
Currently i am working on a project where an audio file plays when a button is pressed. IN that button is a LED so it gives visible feedback. That first part is working.
Now i am trying to change the volume of the audio automatically using a sound sensor.
Honestly i have no idea how to do this and at this moment i am not even able to figure out how to make the sensor read the data. It reads 242 non stop. The power light is on the sensor light is not on.

The sensor is attached to this post.

Does someone maybe know how to do this?

this is the code for the music player:

/*************************************************** 
  This is an example for the Adafruit VS1053 Codec Breakout

  Designed specifically to work with the Adafruit VS1053 Codec Breakout 
  ----> https://www.adafruit.com/products/1381

  Adafruit invests time and resources providing this open source code, 1
  please support Adafruit and open-source hardware by purchasing 
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.  
  BSD license, all text above must be included in any redistribution
 ****************************************************/

// include SPI, MP3 and SD libraries
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>

// These are the pins used for the music maker shield
#define SHIELD_RESET  -1      // VS1053 reset pin (unused!)
#define SHIELD_CS     7      // VS1053 chip select pin (output)
#define SHIELD_DCS    6      // VS1053 Data/command select pin (output)

// These are common pins between breakout and shield
#define CARDCS 4     // Card chip select pin
// DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
#define DREQ 3       // VS1053 Data request, ideally an Interrupt pin

Adafruit_VS1053_FilePlayer musicPlayer = 
  // create shield-example object!
  Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);

int buttonRedInput = 2;
int buttonRedLed = 10;
bool redPressed = false;

int buttonGreenInput =8;
int buttonGreenLed = 11;
bool greenPressed = false;

int buttonBlueInput =5;
int buttonBlueLed = 9;
bool bluePressed = false;

  
void setup() {
  Serial.begin(9600);
  Serial.println("Adafruit VS1053 Simple Test");

  pinMode(buttonRedInput, INPUT);  
  pinMode(buttonRedLed, OUTPUT);

  pinMode(buttonGreenInput, INPUT);  
  pinMode(buttonGreenLed, OUTPUT);

  pinMode(buttonBlueInput, INPUT);  
  pinMode(buttonBlueLed, OUTPUT);
  
  if (! musicPlayer.begin()) { // initialise the music player
     Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
     while (1);
  }
  Serial.println(F("VS1053 found"));
  
   if (!SD.begin(CARDCS)) {
    Serial.println(F("SD failed, or not present"));
    while (1);  // don't do anything more
  }

  // list files
 // printDirectory(SD.open("/"), 0);
  
  // Set volume for left, right channels. lower numbers == louder volume!
  musicPlayer.setVolume(20,20);

  // If DREQ is on an interrupt pin (on uno, #2 or #3) we can do background
  // audio playing
  musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);  // DREQ int
  
  // Play one file, don't return until complete
  //Serial.println(F("Playing track 001"));
  //musicPlayer.playFullFile("/track001.mp3");
  // Play another file in the background, REQUIRES interrupts!
  //  Serial.println(F("Playing track 002"));
  //  musicPlayer.startPlayingFile("/track002.mp3");
}

void loop() {

  // lees de huidige stand van de knop uit
  bool curRedPressed = digitalRead(buttonRedInput);
  // vergelijk deze met de vorige waarde
  // is er een verschil, doe dan dit:
  if(curRedPressed != redPressed)
  {
    if(curRedPressed) // knop ingedrukt
    {
      Serial.println("Start playing");
      musicPlayer.startPlayingFile("/track002.mp3"); 
     
      Serial.println("Red LED turned on");
      digitalWrite(buttonRedLed, HIGH);
    }
    else // knop uitgedrukt
    {
      Serial.println("Stop playing");
      musicPlayer.stopPlaying();
      
      Serial.println("Red LED turned off");
      digitalWrite(buttonRedLed, LOW);
    }
  }
  // sla de huidige waarde op om met de volgende meting te vergelijken.
  redPressed = curRedPressed;
  

// lees de huidige stand van de knop uit
  bool curGreenPressed = digitalRead(buttonGreenInput);
  // vergelijk deze met de vorige waarde
  // is er een verschil, doe dan dit:
  if(curGreenPressed != greenPressed)
  {
    if(curGreenPressed) // knop ingedrukt
    {
      Serial.println("Start playing");
      musicPlayer.startPlayingFile("/track002.mp3"); 
      
      Serial.println("Green LED turned on");
      digitalWrite(11, HIGH); 
    }
    else // knop uitgedrukt
    {
      Serial.println("Stop playing");
      musicPlayer.stopPlaying();
      
      Serial.println("Green LED turned off");
      digitalWrite(11, LOW);
    }
  }
  // sla de huidige waarde op om met de volgende meting te vergelijken.
  greenPressed = curGreenPressed;


 // lees de huidige stand van de knop uit
  bool curBluePressed = digitalRead(buttonBlueInput);
  // vergelijk deze met de vorige waarde
  // is er een verschil, doe dan dit:
  if(curBluePressed != bluePressed)
  {
    if(curBluePressed) // knop ingedrukt
    {
      Serial.println("Start playing");
      musicPlayer.startPlayingFile("/track002.mp3");  

      Serial.println("Blue LED turned on");
      digitalWrite(buttonBlueLed, HIGH); 
    }
    else // knop uitgedrukt
    {
      Serial.println("Stop playing");
      musicPlayer.stopPlaying();

      Serial.println("Blue LED turned off");
      digitalWrite(buttonBlueLed, LOW);
    }
  }
  // sla de huidige waarde op om met de volgende meting te vergelijken.
  bluePressed = curBluePressed;
  
//  delay(100);
}


/// File listing helper
void printDirectory(File dir, int numTabs) {
   while(true) {
     
     File entry =  dir.openNextFile();
     if (! entry) {
       // no more files
       //Serial.println("**nomorefiles**");
       break;
     }
     for (uint8_t i=0; i<numTabs; i++) {
       Serial.print('\t');
     }
     Serial.print(entry.name());
     if (entry.isDirectory()) {
       Serial.println("/");
       printDirectory(entry, numTabs+1);
     } else {
       // files have sizes, directories do not
       Serial.print("\t\t");
       Serial.println(entry.size(), DEC);
     }
     entry.close();
   }
}

this is the code for the sensor i got so far:

int num_Measure = 128 ; // Set the number of measurements   
int pinSignal = A0; // pin connected to pin O module sound sensor  
long Sound_signal;    // Store the value read Sound Sensor   
long sum = 0 ; // Store the total value of n measurements   
long level = 0 ; // Store the average value   
int soundlow = 40;
int soundmedium = 200;
 
void setup ()  
{   
  pinMode (A0, INPUT); // Set the signal pin as input   
  Serial.begin (9600); 
}  
   
void loop ()  
{  
  // Performs 128 signal readings   
  for ( int i = 0 ; i <num_Measure; i ++)  
  {  
   Sound_signal = analogRead (pinSignal);  
    sum =sum + Sound_signal;  
  }  
 
  level = sum / num_Measure; // Calculate the average value   
  Serial.print("Sound Level: ");

  Serial.println (level-33);  
  
  if(level-33<soundlow)
  {
    Serial.print("Intensity= Low");
  }
  if(level-33>soundlow && level-33<soundmedium)
  {
    Serial.print("Intensity=Medium"); 
  }
  if(level-33>soundmedium)
  {
   Serial.print("Intensity= High");   
   
  }
  sum = 0 ; // Reset the sum of the measurement values  
  delay(1000);

}

A link to information, preferably a datasheet, for whatever sound sensor it is that you're trying to read would be useful. Also details of exactly what results you are getting from all those Serial.prints in the sensor reading program when you run it. Perhaps then we can figure it out.

And it would be really good if your code was in code tags NOT quote tags. How to do that is described in "How to use this forum - please read" which you can find at the top of every forum.

Steve

Please edit your post to use code tags ("</>" button).

Thanks for the feedback! I edited my original post.

Sadly i have no clue what kind of sound sensor i have. I got it from school with a starter kid they made without any specs. While using google i found a ky-038 that looks a lot like mine. And this one: Analog Sound Module for Arduino | MPJA.COM

A picture of the sensor i am using is added as attachment in my original post. I also own a grove sound sensor by seed studio.

The light of the sensor is now turned on. I messed around with the sensitivity on the sensor itself. However it still is not changing the data i get from the sensor. It keeps giving me 242, 243 or anything around that number. Even when it is completly quiet in the room or when i am playing music super loud over speakers. A screenshot of the output i get is in the attachments again.

There are not any obvious problems with your code, except that mystery constants like this make no sense:

  Serial.println (level-33);

Most likely, you have not wired the sensor correctly, or it is defective. You have not posted enough information to make better guesses.

Power the sensor from a 5V power supply and check the output with your multimeter, while making noise.

And just to be sure you have connected to the Analog output of the sensor not the Digital output?

Steve

Novelliia: A screenshot of the output

Why? Is your copy/paste function broken or you really don't want to make it easy on us to read text output?

That sensor has a pot - the blue part - what happens if you adjust this? Does the level change? It may also be a threshold setting for the digital output (that photo is pretty blurry but it seems to have two outputs: one digital and one analog).

Without knowing what exact sensor this is and it's supposed behaviour it's hard to debug things.

The sensor are you using is very deaf. It can only hear strong sound like a clap near the mic.

You have two output, one analog and one digital. The sound is compared using a LM393, the trim can change the comparison level. I believe that there is no amplification.

Get a board that uses a MAX chip as amplifier.