Problems with Sound Sensors, Leds and Wifi together

Hello everyone,
Just bought an Arduino kit, and was able to set a few leds (red yellow and green) to work using a very standard sound sensor. Accuracy is not the best, but it works.
Now I was trying to send this info to a webpage using a WiFi connector.
Wifi connects fine, few ok messages, but I have done something not quite right.

Leds get crazy with the Wifi module. But if I disconnect the Wifi everything works again sound and lights.
Schematics attached

The "sample" on the code bellow seems to be the problem, tried to set a few prints to see what was wrong.
Monitor With WiFi:
12:00:13.792 -> SENSOR_PIN 14
12:00:13.792 -> sample 343
12:00:13.839 -> db 81
12:00:13.839 -> 729
12:00:13.839 -> 5
12:00:13.839 -> 724.00
12:00:13.839 -> SENSOR_PIN 14
12:00:13.839 -> sample 676
12:00:13.885 -> db 66
12:00:13.885 -> 731
12:00:13.885 -> 345
12:00:13.885 -> 386.00

Monitor without the Wifi:
12:00:53.261 -> SENSOR_PIN 14
12:00:53.261 -> sample 347
12:00:53.307 -> db 52
12:00:53.307 -> 401
12:00:53.307 -> 302
12:00:53.307 -> 99.00
12:00:53.307 -> SENSOR_PIN 14
12:00:53.354 -> sample 351
12:00:53.354 -> db 51
12:00:53.354 -> 395
12:00:53.354 -> 312
12:00:53.354 -> 83.00

Bellow the code I am using:

#include "SoftwareSerial.h" //para a placa de WiFi
SoftwareSerial ESP_Serial(9, 8); // RX, TX  para a placa de WiFi
String rede = "2.4G_CASA";   //para a placa de WiFi
String senha = "6543212345"; //para a placa de WiFi
String resposta = "";        //para a placa de WiFi
 
const int sampleWindow = 50;// Sample window width in mS (50 mS = 20Hz)
const int pinoLEDverd = 10; // pino onde o LED verde    está conectado
const int pinoLEDamar = 11; // pino onde o LED amarelo  está conectado
const int pinoLEDverm = 12; // pino onde o LED vermelho está conectado
unsigned int sample;

#define SENSOR_PIN A0
#define PIN_QUIET pinoLEDverd
#define PIN_MODERATE pinoLEDamar
#define PIN_LOUD pinoLEDverm 

void setup ()  
{   
  Serial.begin(9600);                                              //Para o Farol
  ESP_Serial.begin(9600);                                                                                              //Para o WiFi
                                                             //Para o WiFi
  Serial.println("Inicializando...");                                                                                  //Para o WiFi
  delay(1000);                                                                                                         //Para o WiFi
                                                             //Para o WiFi
  Serial.println("Chamando atencao do modulo com AT...");                                                              //Para o WiFi
  sendCommand("AT");                                                                                                   //Para o WiFi
  readResponse(1000);                                                                                                  //Para o WiFi
                                                             //Para o WiFi
  Serial.println("Mudando o modo com CWMODE=1...");                                                                    //Para o WiFi
  sendCommand("AT+CWMODE=1");                                                                                          //Para o WiFi
  readResponse(1000);                                                                                                  //Para o WiFi
                                                             //Para o WiFi
  
  pinMode(SENSOR_PIN,   INPUT); // Set the signal pin as input  
  pinMode(PIN_QUIET,    OUTPUT);
  pinMode(PIN_MODERATE, OUTPUT);
  pinMode(PIN_LOUD,     OUTPUT); 
 
  digitalWrite(PIN_QUIET,    LOW);
  digitalWrite(PIN_MODERATE, LOW);
  digitalWrite(PIN_LOUD,     LOW);

//  digitalWrite(pinoLEDverd, LOW);
//  digitalWrite(pinoLEDamar, LOW);
//  digitalWrite(pinoLEDverm, LOW);

  Serial.println("Conectando a rede...");                                                                              //Para o WiFi
  String CWJAP = "\"AT+CWJAP=\"";                                                                                      //Para o WiFi
  CWJAP += rede;                                                                                                       //Para o WiFi
  CWJAP += "\",\"";                                                                                                    //Para o WiFi
  CWJAP += senha;                                                                                                      //Para o WiFi
  CWJAP += "\"";                                                                                                       //Para o WiFi
  sendCommand(CWJAP);                                                                                                  //Para o WiFi
  readResponse(10000);                                                                                                 //Para o WiFi
                                                             //Para o WiFi
  delay(2000); //espera de seguranca                                                                                   //Para o WiFi
                                                             //Para o WiFi
  if (resposta.indexOf("OK") == -1) { //procura na resposta se houve OK                                                //Para o WiFi
    Serial.println("Atencao: Nao foi possivel conectar a rede WiFi.");                                                 //Para o WiFi
    Serial.println("Verifique se o nome da rede e senha foram preenchidos corretamente no codigo e tente novamente."); //Para o WiFi
  } else {                                                                                                             //Para o WiFi
    Serial.println("Sucesso! Conectado a rede WiFi.");                                                                 //Para o WiFi
  }
}  
   
void loop ()  
{ 
   unsigned long startMillis= millis();                   // Start of sample window
   float peakToPeak = 0;                                  // peak-to-peak level
 
   unsigned int signalMax = 0;                            //minimum value
   unsigned int signalMin = 1024;                         //maximum value
 
                                                          // collect data for 50 mS
   while (millis() - startMillis < sampleWindow)
   {
      sample = analogRead(SENSOR_PIN);                    //get reading from microphone
      if (sample < 1024)                                  // toss out spurious readings
      {
         if (sample > signalMax)
         {
            signalMax = sample;                           // save just the max levels
         }
         else if (sample < signalMin)
         {
            signalMin = sample;                           // save just the min levels
         }
      }
   }
 
   peakToPeak = signalMax - signalMin;                    // max - min = peak-peak amplitude
   int db = map(peakToPeak,20,900,49.5,90);             //calibrate for deciBels
 
  if (db <= 59)
  {
    digitalWrite(PIN_QUIET,    HIGH);
    digitalWrite(PIN_MODERATE, LOW);
    digitalWrite(PIN_LOUD,     LOW);
  }
  else if (db > 59 && db < 69)
  {
    digitalWrite(PIN_QUIET   , LOW);
    digitalWrite(PIN_MODERATE, HIGH);
    digitalWrite(PIN_LOUD    , LOW);
  }
  else if (db>=69)
  {
    digitalWrite(PIN_QUIET   , LOW);
    digitalWrite(PIN_MODERATE, LOW);
    digitalWrite(PIN_LOUD    , HIGH);
   }
Serial.print("SENSOR_PIN ");
Serial.println(SENSOR_PIN);
Serial.print("sample ");
Serial.println(sample);
Serial.print("db ");
Serial.println(db);
Serial.println(signalMax);
Serial.println(signalMin);
Serial.println(peakToPeak);
}

void sendCommand(String cmd) {
  ESP_Serial.println(cmd);
}

void readResponse(unsigned int timeout) {
  unsigned long timeIn = millis(); //momento que entramos nessa funcao é salvo
  resposta = "";
  //cada comando AT tem um tempo de resposta diferente...
  while (timeIn + timeout > millis()) {
    if (ESP_Serial.available()) {
      char c = ESP_Serial.read();
      resposta += c;
    }
  }
  Serial.println(resposta);
}

What do those leds do when they get crazy?

Rule #1 A power Supply an Arduino is NOT! It would be much easier to follow your circuit if it were a schematic instead of this frizzy thing. I will take a SWAG and assume the 3V power supply is collapsing when the WiFi is transmitting andor it also is not properly decoupled.

wvmarle:
What do those leds do when they get crazy?

ok, by crazy I mean the leds just randomly blinks.
If I remove the GND from the ESP8266, leds responds to the HIGH and LOW Expected:

  if (db <= 59)
  {
    digitalWrite(PIN_QUIET,    HIGH);
    digitalWrite(PIN_MODERATE, LOW);
    digitalWrite(PIN_LOUD,     LOW);
  }
  else if (db > 59 && db < 69)
  {
    digitalWrite(PIN_QUIET   , LOW);
    digitalWrite(PIN_MODERATE, HIGH);
    digitalWrite(PIN_LOUD    , LOW);
  }
  else if (db>=69)
  {
    digitalWrite(PIN_QUIET   , LOW);
    digitalWrite(PIN_MODERATE, LOW);
    digitalWrite(PIN_LOUD    , HIGH);
   }

Whenever I plug the Wifi ESP8266 (Even on the 5v now), I get this "sample" with a huge amplitude, seems link somewhere here the problem.
Got a tip to use the basic example project ReadAnalogVoltage
and got some answers:

ESP8266 + Sound adapter

Nothing connected

Only ESP8266 (it decreases until it flats like the above print)

Only Sound adapter

A small video below shows this strange behavior with and without ESP8266
Behavior on removing ESP8266

gilshultz:
Rule #1 A power Supply an Arduino is NOT! It would be much easier to follow your circuit if it were a schematic instead of this frizzy thing. I will take a SWAG and assume the 3V power supply is collapsing when the WiFi is transmitting andor it also is not properly decoupled.

On that matter, thanks for the feedback, this is really my first project, it seems like I offended somehow with my frizzy thing, tried to do something I can actually read. I wouldn´t know to do better then this draw.
Tried to connect on the 5v and got the same result. I didn't get the Rule #1 tip. I am guessing I connected to many things on the Arduino? Sound Leds and Wifi are to much? I dont feel abusing Arduino capacities, but again my first project.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.