harrrrrrrrrrrrrrrry
Published © CC BY

Voice Loudness Monitor

This product alerts the user when they are speaking too loudly via two LEDs. Louder voices spread more viral particles, so quieter is safer.

BeginnerFull instructions provided544
Voice Loudness Monitor

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
5 mm LED: Green
5 mm LED: Green
×1
5 mm LED: Red
5 mm LED: Red
×1
Through Hole Resistor, 560 ohm
Through Hole Resistor, 560 ohm
×2
Male/Female Jumper Wires
Male/Female Jumper Wires
×1
Breadboard (generic)
Breadboard (generic)
×1
SparkFun Electret Microphone Breakout
SparkFun Electret Microphone Breakout
×1

Story

Read more

Code

VoiceLoudnessMonitor.ino

Arduino
No preview (download only).

Code snippet #1

Plain text
// Define constants - these can be tuned to the user's preferences
#define sampleWindow 10 // Sample window width in milliseconds (10 ms = 100 Hz)
#define LEDtime 1000 //time to keep LED on for when noise goes above threshold
#define threshold 40 //mV
#define QTY_SAMPLES 16 // the number of samples to be averaged

//Define pins used on the breadboard
#define MIC_IN A0
#define LED_LOUD PD2
#define LED_QUIET PD3
#define V_MIC 3.3

//Create global variables
int sample;
long startLED;
float values[QTY_SAMPLES];
float sum;
int index;


// This function takes a rolling average of QTY_SAMPLES
// The oldest value is removed from the sum and the newest takes its place in the array. Then the mean of this array is returned.
float rollingAverage(float num) {
  sum -= values[index];
  values[index] = num;
  sum += values[index];
  index++;
  index = index % QTY_SAMPLES;
  return sum / QTY_SAMPLES;
}


//Begin serial output, initialise pins and set global variables
void setup()
{
  Serial.begin(9600);
  pinMode(LED_QUIET, OUTPUT);
  pinMode(LED_LOUD, OUTPUT);
  pinMode(MIC_IN, INPUT);
  float sum = 0;
  int index = 0;
}


//Sample the signal amplitude and calculate a rolling average. If the average loudness is too high, then alert user via LEDs
void loop()
{

  // Reset variables
  int signalMax = 0;
  int signalMin = 1024;
  unsigned long startMillis = millis(); // Set the start of the sample window

  // Record the signal maximum and minimum over the sample window
  while (millis() - startMillis < sampleWindow)
  {
    sample = analogRead(MIC_IN);
    if (sample > signalMax)
    {
      signalMax = sample;  // save the new maximum
    }
    else if (sample < signalMin)
    {
      signalMin = sample;  // save the new minimum
    }
  }

  // Convert the signal maximum and minimum into millivolts from the amplifier
  float amplitude = signalMax - signalMin;  // This calculates the peak-to-peak amplitude
  float millivolts = rollingAverage(amplitude) * V_MIC / 1.024; // This calls the rolling average function and converts the average amplitude to millivolts
  Serial.println(millivolts);

  //Alert the user via the LEDs if the noise is too loud
  if (millivolts > threshold) {
    digitalWrite(LED_LOUD, HIGH); digitalWrite(LED_QUIET, LOW);
    startLED = millis();
  } else if (millis() - startLED > LEDtime) {
    digitalWrite(LED_LOUD, LOW); digitalWrite(LED_QUIET, HIGH);
  }
}

//Further work could be converting the millivolts signal into a Sound Pressure Level (dB SPL), or adding more LEDs to display a noise level scale<br>

Credits

harrrrrrrrrrrrrrrry

harrrrrrrrrrrrrrrry

0 projects • 0 followers

Comments