Noise detector


Description

In this tutorial is shown how to realize a noise detector, that shows the results on a webpage. using an Arduino UNO WiFi.

Warning:
Please, it's important to upgrade to the latest ESP firmware, using the Arduino UNO firmware updater tool, before to continue this tutorial.

 

Hardware 

For more details visit our Store.

Circuit

Usually a noise detector module has three pins: Vcc, GND and Signal.
Connect Vcc at 5V, GND at Ground and Signal at the A0 pin, as shown in the below image:

noise detector circuit

Code

Now connect the board to PC, via USB cable, and upload the code below, using the Arduino IDE (click here to download the sketch):

Warning:
If you are using the Arduino IDE 1.7.x then continue to use the ArduinoWiFi.h library.
Instead if you are using the Arduino IDE 1.8.x then download the UNOWiFiDev.Edition library from Library Manager and modify the code, replacing #include <ArduinoWiFi.h> with #include <UnoWiFiDevEd.h>

/*
Noise detector:
Upload the code and open your borwser on http://<IP>/arduino/webserver/ or http://<hostname>.local/arduino/webserver/
for more information:
http://www.arduino.org/learning/tutorials/boards-tutorials/noise-detector

created in November 2016
by sebba[at]arduino[dot]org
*/


//Import libraries
#include <Wire.h>
#include <ArduinoWiFi.h>// if you are using the Arduino IDE 1.8.x then: //#include <UnoWiFiDevEd.h>

const int micPin = A0;
int micValue = 0;


//SET UP
void setup() {
pinMode(13,OUTPUT);
Wifi.begin();
Wifi.println("WebServer Server is up");
}

//LOOP
void loop() {
while(Wifi.available()){
process(Wifi);
}
delay(50);
}


/* FUNCTIONS */

//process()
void process(WifiData client) {
// read the command
String command = client.readStringUntil('/');

// is "digital" command?
if (command == "webserver") {
WebServer(client); // call WebServer() function
}

else if (command == "update") {
updateValues(client); // call updateValues() function
}
}

/* updateValues() function
Get noise from microphone and return a raw value and a mapped one
*/
void updateValues(WifiData client){
micValue = analogRead(micPin);
int micValueMap = map(micValue, 0, 800, 0, 6);
client.println(F("HTTP/1.1 200 OK\n"));
client.print(micValue);
client.print(F("#"));
client.print(micValueMap);
client.print(EOL);
}


//WebServer()
void WebServer(WifiData client) {
client.println(F("<html><head><style>"));

//Stylesheet parameters for the button
client.println(F(".bar{border-style:solid;border-color:#000;width:5%;margin:1px;display:inline-block;}"));
client.println(F(".graph{width:100%;height:30%;margin:20%;}"));
client.println(F("</style><script>"));

/*
JavaScript functions that creates an Ajax request to the URL
URL+'/arduino/digital/update"
where URL is the IP of the Uno WiFi
*/

//Call update function every 3 seconds
client.println(F("setInterval(function(){ update();},3000)"));

/*
JavaScript functions that creates an Ajax request to the URL
URL+'/arduino/digital/update"
where URL is the IP of the Uno WiFi
*/
client.println(F("function update(){"));
client.println(F("var colors=['ffc','ff6','fc0','c90','f30','c00']"));
client.println(F("var URL_array=document.URL.split('/')"));;
client.println(F("var URL=URL_array[0]+'//'+URL_array[2]"));
client.println(F("url=URL+'/arduino/digital/update'"));
client.println(F("xmlhttp=new XMLHttpRequest()"));
client.println(F("xmlhttp.onreadystatechange=function(){"));
client.println(F("if(xmlhttp.readyState==4 && xmlhttp.status==200)"));
client.println(F("var resp=xmlhttp.responseText"));
client.println(F("if(typeof resp==='undefined')return"));
client.println(F("var i=resp.split('#')[1],a=0"));
client.println(F("for(a;a<6;a++){"));
client.println(F("var e=document.getElementById('l'+a)"));
client.println(F("e.style.background='#'+((a<i)?colors[a]:'fff')}"));
client.println(F("}"));
client.println(F("xmlhttp.open('GET',url,true)"));
client.println(F("xmlhttp.send()}"));
client.println(F("</script>"));
client.println(F("<head>"));

//Print the HTML Code, with the level bars.
client.println(F("<body>"));
client.println(F("<div class='graph'>"));
client.println(F("<div id='l0' class='bar' style='height:10%'></div>"));
client.println(F("<div id='l1' class='bar' style='height:20%'></div>"));
client.println(F("<div id='l2' class='bar' style='height:30%'></div>"));
client.println(F("<div id='l3' class='bar' style='height:40%'></div>"));
client.println(F("<div id='l4' class='bar' style='height:50%'></div>"));
client.println(F("<div id='l5' class='bar' style='height:60%'></div>"));
client.println(F("</div>"));
client.println(F("</body></html>"));
client.println(DELIMITER);
client.flush();
}

If you want to use an external webserver, for example Apache, then use this other code.

OUTPUT

It will be shown a graphic that changes color depending of noise
zero level

four level
five level

six level


Note: 
If you haven't configured your board then click here.