Project tutorial
Network VU Sound Level Meter using C#, Arduino and ESP8266

Network VU Sound Level Meter using C#, Arduino and ESP8266

Network Sound Level meter is capable to send UDP packet over laptop existing WiFi. These packets received by Arduino through ESP8266.

  • 1,246 views
  • 0 comments
  • 3 respects

Components and supplies

Necessary tools and machines

09507 01
Soldering iron (generic)
Hy gluegun
Hot glue gun (generic)

Apps and online services

About this project

My most favorite project.

I got the idea to have sound visualization in my room, but I hate connecting any more wires to my laptop. For that purpose, I made this project!

This project consist of two parts.

First, C# form application extract the sound numberic value from Naudio dll. After numeralization of the value, the app sends a UDP packet of a IP address.

In the second part, the ESP8266 receives that UDP packet and sends that data to an Arduino Pro Micro which processes and makes decision to turn on and off its digital I/O. Darlinton pair is used to turn on the LED strips connected to wooden stick.

C# Application

With the help of naudio.dll, sound numeric value can be extracted by the PC.

Mechanical Structure

LED strips are pasted on the wooden stick and connected with the board. The wooden stick is 100inch long. 9 differnt color led strips are used.

Arduino Part

ESP8266 can be easily interface by the UART with Arduino Pro Micro, the UDP Packets which are sent from the C# form application will be processed and base on some conditions, digital I/O are turned on and off. As Arduino is not capable to supply current to the LED Strips, Darlington Pair is used to relaying the current. LED strips turn on and off with respect to the peak value of the sound, played on the laptop or PC.

Code

Windows Form ApplicationC#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Sockets;

namespace SoundLevelMeter
{
    public partial class Form1 : Form
    {
        int temp = 0;
        int max_value = 0;
        double divisor;
        String data = "";
        long low_value_counter = 0;
        String packet = "BBBBBBBBBB";
        Socket sock = null;
        System.Net.IPEndPoint endpoint = null;
        String style = "B";
        
        public Form1()
        {
            InitializeComponent();
            NAudio.CoreAudioApi.MMDeviceEnumerator enumerator = new NAudio.CoreAudioApi.MMDeviceEnumerator();
            var devices = enumerator.EnumerateAudioEndPoints(NAudio.CoreAudioApi.DataFlow.All, NAudio.CoreAudioApi.DeviceState.Active);
            comboBox1.Items.AddRange(devices.ToArray());
            try { comboBox1.SelectedIndex = 0; }
            catch { }
            timer2.Start();
            sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            System.Net.IPAddress serverAddr = System.Net.IPAddress.Parse("192.168.1.202");
            endpoint = new System.Net.IPEndPoint(serverAddr, 11000);
            textBox1.Visible = false;
            comboBox1.Visible = false;

        
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem != null)
            {
                var device = (NAudio.CoreAudioApi.MMDevice)comboBox1.SelectedItem;
                // abc = NAudio.CoreAudioApi.;
                temp = (int)(Math.Round(device.AudioMeterInformation.MasterPeakValue * 100 + 0.5));
                progressBar1.Value = temp;
                if (max_value < temp) { max_value = temp; divisor = max_value / 10; }
                textBox1.Text = temp.ToString();
                
                    if (temp >= 11 * divisor) packet = "AAAAAAAAA";
                    else if (temp >= 10 * divisor) packet = "AAAAAAAA";
                    else if (temp >= 9 * divisor) packet = "AAAAAAAAB";
                    else if (temp >= 8 * divisor) packet = "AAAAAAABB";
                    else if (temp >= 7 * divisor) packet = "AAAAAABBB";
                    else if (temp >= 5 * divisor) packet = "AAAAABBBB";
                    else if (temp >= 4 * divisor) packet = "AAAABBBBB";
                    else if (temp >= 3 * divisor) packet = "AAABBBBBB";
                    else if (temp >= 2 * divisor) packet = "AABBBBBBB";
                    else if (temp >= 1 * divisor) packet = "ABBBBBBBB";

                if (temp < 3) { packet = "BBBBBBBBB"; }

                    data = packet+style;
                    udp_send();

                
            }
        }

        public void udp_send()
        {
            string text = data;
            byte[] send_buffer = Encoding.ASCII.GetBytes(text);
            sock.SendTo(send_buffer, endpoint);


        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            max_value=0;

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void Style1_Click(object sender, EventArgs e)
        {
            style = "B";

        }

        private void Style2_Click(object sender, EventArgs e)
        {
            style = "A";
        }

        private void Style3_Click(object sender, EventArgs e)
        {
            style = "C";
        }

        private void Style4_Click(object sender, EventArgs e)
        {
            style = "D";
        }
    }
}
Arduino Pro MicroArduino
#define DEBUG true
#define dbg Serial  // USB local debug
int led0 = 2;
int led1 = 3;
int led2 = 4;
int led3 = 5;
int led4 = 6;
int led5 = 7;
int led6 = 8;
int led7 = 9;
int led8 = 10;
String ssid = "ssid";
String pass = "your Password";
String ip = "192.168.1.202";    //ESP 8266 Static IP
String remoteip="192.168.1.4";   // Computer IP
String contype= "UDP";             // Protocal of sending Data

String num = "5";
char output[10]="";
String sendData(String command, const int timeout, boolean debug) {
  String response = "";
  Serial1.print(command); // send the read character to the esp8266
  long int time = millis();
  while ( (time + timeout) > millis()) {
    while (Serial1.available()) {
      // The esp has data so display its output to the serial window
      char c = Serial1.read(); // read the next character.
      response += c;
    }
  }

  if (debug)
  {
    Serial.print(response);
  }

  return response;
}





void setup() {

  // Set baud rates
pinMode(led0, OUTPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
pinMode(led7, OUTPUT);
pinMode(led8, OUTPUT);
  Serial1.begin(115200);
  dbg.begin(115200);
  dbg.println("DEBUG: Running Setup");



  sendData("AT+RST\r\n", 2000, DEBUG); delay(10000);// reset module
  sendData("AT\r\n", 2000, DEBUG); delay(4000);// reset module
  sendData("AT+CWMODE=1\r\n", 1000, DEBUG); delay(4000);// configure as access point


sendData("AT+CWJAP=\"" + ssid + "\",\"" + pass + "\"\r\n", 1000, DEBUG);delay(1000); 
sendData("AT+CIPSTA=\""+ip+"\"\r\n",1000,true); //delay(2000);
sendData("AT+CIPMUX=1\r\n",5000,DEBUG); //delay(4000);

sendData("AT+CIPSTART=4,\"" +contype+ "\",\"" + remoteip + "\",12000,11000,0\r\n",3000,DEBUG);//delay(4000);

}


void loop()
{

 
  
  if(Serial1.available()) // check if the esp is sending a message 
  {

    if(Serial1.find("+IPD,")){
    delay(1000);
    if(Serial1.find(":")){

    for(int i=0;i<=9;i++){
    int inByte = Serial1.read();
    output[i]=inByte;
//    Serial.write(inByte);
    }
      
    }
    
    
    Serial.print(output);
    Serial.println(" ");
if(output[9]=='A'){style1();}
if(output[9]=='B'){style2();}
if(output[9]=='C'){style3();}
if(output[9]=='D'){style4();}



  }
}}

void style1()
{
  //  led0  led1  led2  led3  led4  led5  led6  led7  led8   total-9-leds   >>>>>>>>>>
  //  0     1     2     3     4     5     6     7     8
  if(output[0]=='A'){digitalWrite(led0,HIGH);} if(output[0]!='A'){digitalWrite(led0,LOW);}
  if(output[1]=='A'){digitalWrite(led1,HIGH);} if(output[1]!='A'){digitalWrite(led1,LOW);}
  if(output[2]=='A'){digitalWrite(led2,HIGH);} if(output[2]!='A'){digitalWrite(led2,LOW);}
  if(output[3]=='A'){digitalWrite(led3,HIGH);} if(output[3]!='A'){digitalWrite(led3,LOW);}
  if(output[4]=='A'){digitalWrite(led4,HIGH);} if(output[4]!='A'){digitalWrite(led4,LOW);}
  if(output[5]=='A'){digitalWrite(led5,HIGH);} if(output[5]!='A'){digitalWrite(led5,LOW);}
  if(output[6]=='A'){digitalWrite(led6,HIGH);} if(output[6]!='A'){digitalWrite(led6,LOW);}
  if(output[7]=='A'){digitalWrite(led7,HIGH);} if(output[7]!='A'){digitalWrite(led7,LOW);}
  if(output[8]=='A'){digitalWrite(led8,HIGH);} if(output[8]!='A'){digitalWrite(led8,LOW);}
    
  }

  void style2()
{
  //  led0  led1  led2  led3  led4  led5  led6  led7  led8   total-9-leds       <<<<<<<<<<
  //  8     7     6     5     4     3     2     1     0
  if(output[8]=='A'){digitalWrite(led0,HIGH);} if(output[8]!='A'){digitalWrite(led0,LOW);}
  if(output[7]=='A'){digitalWrite(led1,HIGH);} if(output[7]!='A'){digitalWrite(led1,LOW);}
  if(output[6]=='A'){digitalWrite(led2,HIGH);} if(output[6]!='A'){digitalWrite(led2,LOW);}
  if(output[5]=='A'){digitalWrite(led3,HIGH);} if(output[5]!='A'){digitalWrite(led3,LOW);}
  if(output[4]=='A'){digitalWrite(led4,HIGH);} if(output[4]!='A'){digitalWrite(led4,LOW);}
  if(output[3]=='A'){digitalWrite(led5,HIGH);} if(output[3]!='A'){digitalWrite(led5,LOW);}
  if(output[2]=='A'){digitalWrite(led6,HIGH);} if(output[2]!='A'){digitalWrite(led6,LOW);}
  if(output[1]=='A'){digitalWrite(led7,HIGH);} if(output[1]!='A'){digitalWrite(led7,LOW);}
  if(output[0]=='A'){digitalWrite(led8,HIGH);} if(output[0]!='A'){digitalWrite(led8,LOW);}
    
  }
  void style3()
{
  //  led0  led1  led2  led3  led4  led5  led6  led7  led8   total-9-leds        <<<<<>>>>>>>
  //  7     5     3     1     0     2     4     6     8
  if(output[7]=='A'){digitalWrite(led0,HIGH);} if(output[7]!='A'){digitalWrite(led0,LOW);}
  if(output[5]=='A'){digitalWrite(led1,HIGH);} if(output[5]!='A'){digitalWrite(led1,LOW);}
  if(output[3]=='A'){digitalWrite(led2,HIGH);} if(output[3]!='A'){digitalWrite(led2,LOW);}
  if(output[1]=='A'){digitalWrite(led3,HIGH);} if(output[1]!='A'){digitalWrite(led3,LOW);}
  if(output[0]=='A'){digitalWrite(led4,HIGH);} if(output[0]!='A'){digitalWrite(led4,LOW);}
  if(output[2]=='A'){digitalWrite(led5,HIGH);} if(output[2]!='A'){digitalWrite(led5,LOW);}
  if(output[4]=='A'){digitalWrite(led6,HIGH);} if(output[4]!='A'){digitalWrite(led6,LOW);}
  if(output[6]=='A'){digitalWrite(led7,HIGH);} if(output[6]!='A'){digitalWrite(led7,LOW);}
  if(output[8]=='A'){digitalWrite(led8,HIGH);} if(output[8]!='A'){digitalWrite(led8,LOW);}
    
  }
  void style4()
{
  //  led0  led1  led2  led3  led4  led5  led6  led7  led8   total-9-leds        >>>>        <<<<<
  //  0     2     4     6     8     7     5     3     1
  if(output[0]=='A'){digitalWrite(led0,HIGH);} if(output[0]!='A'){digitalWrite(led0,LOW);}
  if(output[2]=='A'){digitalWrite(led1,HIGH);} if(output[2]!='A'){digitalWrite(led1,LOW);}
  if(output[4]=='A'){digitalWrite(led2,HIGH);} if(output[4]!='A'){digitalWrite(led2,LOW);}
  if(output[6]=='A'){digitalWrite(led3,HIGH);} if(output[6]!='A'){digitalWrite(led3,LOW);}
  if(output[8]=='A'){digitalWrite(led4,HIGH);} if(output[8]!='A'){digitalWrite(led4,LOW);}
  if(output[7]=='A'){digitalWrite(led5,HIGH);} if(output[7]!='A'){digitalWrite(led5,LOW);}
  if(output[5]=='A'){digitalWrite(led6,HIGH);} if(output[5]!='A'){digitalWrite(led6,LOW);}
  if(output[3]=='A'){digitalWrite(led7,HIGH);} if(output[3]!='A'){digitalWrite(led7,LOW);}
  if(output[1]=='A'){digitalWrite(led8,HIGH);} if(output[1]!='A'){digitalWrite(led8,LOW);}
    
  }

Schematics

BreadBoard
Fritzing breadboard bb jvihkjm5ad

Comments

Similar projects you might like

Water Level Meter

Project showcase by Fanatic Series

  • 6,955 views
  • 6 comments
  • 22 respects

Sound Sensor Activated LEDs with LCD for Sound Level Data!

Project tutorial by Heathen_Hacks-v2

  • 1,415 views
  • 2 comments
  • 8 respects

Personal Weather Station (Arduino+ ESP8266 + Thingspeak)

Project tutorial by Jayraj Desai

  • 51,574 views
  • 32 comments
  • 108 respects

The Cheapest ESP8266 Programmer!

Project tutorial by Gustavo Reynaga

  • 4,429 views
  • 0 comments
  • 15 respects

Wireless weather station (Arduino + ESP8266 + Thingspeak)

Project tutorial by Alin-Constantin Paun

  • 9,180 views
  • 2 comments
  • 15 respects
Add projectSign up / Login