Arduino Sound Meter

 

There are two types of sound sensors, the first type is a ‘knock detector’ – this sensor connects the microphone to a comparator ic and when the sound level exceeds a certain level the Digital output will go High. The second type, shown above, has the same digital output but the analogue output from the microphone is also available.

The second type has four pins AO is the analogue output, G or Gnd, + or Vcc connects to 5volts and DO which is the Digital Output. The Digital Output will either be HIGH or LOW.

The following sketch only uses the Analogue Output to produce a Sound Meter. Six LEDs are use to show the output. The Value of the sound level, together with maximum and minimum values can be seen on the Serial Monitor.

Sensitivity can be set inside the program, or a potentiometer connected between Gnd and 5 volts with the centre pin connected to an analogue port,  could be used to set the sensitivity.


/* Sound Sensor Used as a sound meter
needs a sound sensor with an Analogue Output
connect Gnd to Arduino Gnd
connect + to Arduino 5 volts
connect AO to Arduino Analogue Pin A0
*/

int mic=A0; // connect AO to Analogue Pin A0
float sensorvalue=0,lastsensorvalue=0,lastminsensorvalue=1024;
int i;
int led[]={11,12,7,6,5,4};
float val;
//
void setup()
{
    Serial.begin(9600);
    for (int pin=0; pin < 6; pin++)
    {
        pinMode(led[pin], OUTPUT);
    }
    for (int colu=2; colu < 6; colu++)
    {
        digitalWrite(led[colu], HIGH);
    }
    //
    // light LEDs 1 and 2 up
    digitalWrite(led[0], HIGH);
    digitalWrite(led[1], HIGH);
}
//
void loop()
{
    // Set sensitivity
    // this variable can be changed to potentiometer input
    //
    val=130;
    sensorvalue=analogRead(mic);
    sensorvalue=sensorvalue/val;
    //
    // Get MAX
    //
    if (sensorvalue > lastsensorvalue)
    {
        lastsensorvalue=sensorvalue;
    }
    //
    // Get MIN
    //
    if( sensorvalue < lastminsensorvalue)
    {
        lastminsensorvalue=sensorvalue;
    }
    //
    Serial.print("MAX: ");
    Serial.print(lastsensorvalue);
    Serial.print(" ");
    Serial.print("LAST: ");
    Serial.print(sensorvalue);
    Serial.print(" ");
    Serial.print("MIN: ");
    Serial.print(" ");
    Serial.print(lastminsensorvalue);
    Serial.print("Sensitivity: ");
    Serial.println(val);
    delay(1);
    //
    // Clear led matrix
    //
    if (sensorvalue == lastminsensorvalue)
    {
        for(int i=6;i>1;i--)
        {
            digitalWrite(led[i], HIGH);
            delay(130);
        }
    }
    else
    {
        for (i=2;i<sensorvalue; i++)
        //turn on the leds up to volume level
        {
            digitalWrite(led[i], LOW);
            delay(1);
        }

        for(i=i;i<6;i++)
        // turn off the leds above the voltage level
        {
            digitalWrite(led[i], HIGH);
        }
    }
}

Current sensing with an Arduino


Current sensing is not quite as easy as voltage sensing. A popular sensor for current on the Arduino is the ACS712 And a typical version is shown above.

The ACS712 consists of a precise, low-offset, linear Hall sensor circuit with a copper conduction path located near the surface of the die. Applied current flowing through this copper conduction path generates a magnetic field which is sensed by the integrated Hall IC and converted into a proportional voltage. Device accuracy is optimized through the close proximity of the magnetic signal to the Hall transducer. 

The terminals of the conductive path are electrically isolated from the sensor IC leads and this allows the ACS712 current sensor IC to be used in applications requiring electrical isolation without the use of opto-isolators or other costly isolation techniques.

A sample sketch to test the sensor is shown here. The output voltage from the sensor is proportional to the current flowing in the test circuit and the Arduino reads this voltage and prints it out as current.

/*ACS712 Current sensing board
Connections:
Gnd on sensor to Gnd on Arduino
Vcc on sensor to +5volts on Arduino
Out (or S) to A2 on Arduino
Connect the screw connections to the test circuit in series with the load
*/

int VQ;
int ACSPin = A2;

void setup() {
Serial.begin(9600);
VQ = determineVQ(ACSPin); //Quiscent output voltage - the average voltage ACS712 shows with no load (0 A)
delay(1000);
}

void loop() {
Serial.print("ACS712@A2:");
Serial.print(readCurrent(ACSPin),3);
Serial.println(" mA");
delay(150);
}

int determineVQ(int PIN) {
Serial.print("estimating avg. quiscent voltage:");
long VQ = 0;
//read 5000 samples to stabilise value
for (int i=0; i<5000; i++) {
    VQ += analogRead(PIN);
    delay(1);//depends on sampling (on filter capacitor), can be 1/80000 (80kHz) max.
}
VQ /= 5000;
Serial.print(map(VQ, 0, 1023, 0, 5000));
Serial.println(" mV");
return int(VQ);
}


float readCurrent(int PIN) {
int current = 0;
int sensitivity = 185.0;//change this to 100 for ACS712-20A or to 66 for ACS712-30A
//read 5 samples to stabilise value
for (int i=0; i<5; i++) {
    current += analogRead(PIN) - VQ;
    delay(1);
}
current = map(current/5, 0, 1023, 0, 5000);
return float(current)/sensitivity;
}