Noise Detector Alarm

by iClaimThisName in Circuits > Arduino

864 Views, 1 Favorites, 0 Comments

Noise Detector Alarm

WhatsApp Image 2022-01-23 at 11.43.27 (1).jpeg

In this instructable I will show you how to make a noise detecter, where an alarm will go off if loud noise is too frequent.

The machine has 3 stages:

-GOOD (green): Not too much noise

-MEDIUM (yellow): Warning: Frequent noise

-BAD (red): Noise is too frequent, alarm will go off

Supplies

-Arduino UNO

-Jumper cables

-Resistors

-3 LEDs: one red, one yellow and one green

-2 Buttons (with caps to your preference. I recommend using red for the "reset" button)

-Piezo buzzer

-Sound sensor module (I used an Iduino 1485297 microphone volume sensor)

LEDs and Buttons

afbeelding_2022-01-23_132309.png

First, we have to assemble the LEDs and buttons as shown. The blue LED will later be replaced by a piezo buzzer (or another noise-maker per your choice)

The buttons will act as "admin tools" to help us with testing the machine.

The code below shows how to set up the LEDs and Buttons for how we will use them. "SwitchState" will make sure that the action happens only after we let go of the button, to ensure that the action will only happen once per button press.

//BUTTONS
const int Button_UP = 4;
int ButtonState_UP = 0;
bool switchState_UP = LOW;
bool previousSwitchState_UP = LOW;

const int Button_RESET = 2;
int ButtonState_RESET = 0;
bool switchState_RESET = LOW;
bool previousSwitchState_RESET = LOW;

//LEDs
const int led_good = 8;
const int led_medium = 12;
const int led_bad = 13;
const int led_alarm = 7; //buzzer later

void setup()
{
  Serial.begin(115200);
  
  //BUTTONS
  pinMode(Button_UP, INPUT);
  pinMode(Button_RESET, INPUT);
  
  //LEDs
  pinMode(led_good, OUTPUT);
  pinMode(led_medium, OUTPUT);
  pinMode(led_bad, OUTPUT);
  pinMode(led_alarm, OUTPUT);
}

Code Prep

To prepare for the rest of the code, we have to create a number of variables.

ST: Sound threshold. This will be used later to indicate how high the sound must be for it to count as "loud noise"

N: Counts how many times ST has been exceeded.

C: Cooldown status. To ensure that one sound does make our counter (N) go up multiple times, we have to add a short cooldown.

L: Current level. We have three levels: Good (0), Medium (1), Bad (2).

LT: Level threshold. This is how high N needs to be to increase the level. If you want the level to increase with every loud noise, set this to 1.

T: Time passed without ST being exceeded. We will need this so after a certain time of silence, N will go down again.

TT: Time treshold. This is how high T needs to be, to decrease N by 1. Make this lower if you want the level to decrease quicker. Make sure it is higher than the cooldown though, or the level will always be "Good".

int ST; //sound threshold -> used for sound sensor later
int N = 0; //number of times ST has been exceeded
int C = 0; //cooldown status
int CT = 10000; //cooldown threshold
int L = 0; //current level
int LT = 3; //how high N needs to be to increase the level
int T = 0; //time passed without ST being exceeded
int TT = 10000; //time threshold; how high T needs to be

Button Code

In void(loop), add the code below.

Like mentioned before, "switchState" will ensure that an action only happens after a button has been pressed AND let go of.

When the "UP" button has been pressed and C is bigger than CT (this checks wether our cooldown is up or not), we reset the cooldown and timer, and increase N by 1.

When the "RESET" button has been pressed, we set N to 0.

  ButtonState_UP = digitalRead(Button_UP); 
  ButtonState_RESET = digitalRead(Button_RESET);
  
  switchState_UP = ButtonState_UP;
  if(switchState_UP == LOW && previousSwitchState_UP == HIGH && C >= CT)
  {
    C = 0;
    T = 0;
    N = N + 1;
    Serial.println("Current N is");
    Serial.println(N);
    Serial.println("Current L is");
    Serial.println(L);
  }
  previousSwitchState_UP = switchState_UP;
  
  switchState_RESET = ButtonState_RESET;
  if(switchState_RESET == LOW && previousSwitchState_RESET == HIGH)
  {
    N = 0;
  }
  previousSwitchState_RESET = switchState_RESET;

Cooldown and Timer Code

The cooldown is extremely simple: Just add to it constantly, until the sound threshold has been exceeded.

The base of the timer works the same way. We just add an if-statement to it that checks wether the timer has exceeded our time treshold (I added *100 because I wanted the threshold to be quite big by default, without having HUGE numbers in my variable list, but you can remove this and just use a bigger number when declaring TT) Then it checks wether N is bigger than 0. If it is, it will decrease by one and reset the timer. If not, the timer will just reset so it can check again later.

  //COOLDOWN
  C++;
  
  //TIMER
  T++;
  if(T >= TT * 100)
    {
      if(N > 0)
      {
        N = N - 1;
      }
      T = 0;
    }

Level Code

The level code is very simple. It checks wether N is high enough to reach that level first.

Then you simply state which lights should be on and off during this level.

//LEVEL 0 (good)
  if(N < LT)
  {
    L = 0;
  }
  
  if(L == 0)
  {
    digitalWrite(led_good, HIGH);
    digitalWrite(led_medium, LOW);
    digitalWrite(led_bad, LOW);
    digitalWrite(led_alarm, LOW);
  }
  
  //LEVEL 1 (medium)
  if(N >= LT && N<= LT*2)
  {
    L = 1;
  }
  
  if(L == 1)
  {
    digitalWrite(led_good, LOW);
    digitalWrite(led_medium, HIGH);
    digitalWrite(led_bad, LOW);
    digitalWrite(led_alarm, LOW); 
  }
  
  //LEVEL 2 (bad)
  if(N >= LT*2 && N < LT*3)
  {
    L = 2;
  }
  
  if(L == 2)
  {
    digitalWrite(led_good, LOW);
    digitalWrite(led_medium, LOW);
    digitalWrite(led_bad, HIGH);
    digitalWrite(led_alarm, HIGH);
  }

Sound Sensor and Buzzer

WhatsApp Image 2022-01-23 at 10.54.11 (2).jpeg
WhatsApp Image 2022-01-23 at 10.54.11.jpeg

Now it's time to build everything with a physical arduino, and add your sound module. Connect the module as shown (a jumper cable from A0 on the module, to A0 on your arduino UNO, a cable from G to GND and a cable from + to 5V)

You can also swap out your alarm LED for your buzzer at this point. The code can stay the same, but I recommend changing the variable name to something along the lines of "buzzer" or "alarm" to avoid confusion.

For the sound sensor module, we will need to add some extra code. Add at the top of all your code:

#define soundSensor A0
unsigned long last_event = 0;

then set ST (sound threshold) that you created before, to 51. And in setup, set the pinmode of your sensor to input:

pinMode(soundSensor, INPUT);

Sound Sensor Module Code

We're almost done with our code, we just need to add the code that will make sure things actually happen when sound is detected. The code below reads (analogRead) the output of our sound sensor, and if it's above our threshold (which I set to 51, because that sensitivity worked the best for me. Play around with this until you find a threshold that works best for you and what you want. For example, if you only want to pick up REALLY loud sounds, this threshold can be higher).

Note how the rest of the code is pretty much the same as what we wrote for the "UP" button. The println "sound detected!" is so we can easily tell if it's actually working or not, but you can leave it out if you find it unnecessary.

int output = analogRead(soundSensor);
  if(output > ST && C >= CT)
  {
    C = 0;
    T = 0;
    N = N + 1;
    Serial.println("sound detected!");
  }

Finishing Touches

WhatsApp Image 2022-01-23 at 11.43.27 (1).jpeg
WhatsApp Image 2022-01-23 at 10.54.10.jpeg

Now all that's left is just the finishing touches! You can do this however you want.

I made a box for all the cables and parts, with holes for the sound sensor module, the buzzer, the lights and a cable that will run from the machine to a power source.

Full code:

//BUTTONS
const int Button_UP = 4;
int ButtonState_UP = 0;
bool switchState_UP = LOW;
bool previousSwitchState_UP = LOW;

const int Button_RESET = 2;
int ButtonState_RESET = 0;
bool switchState_RESET = LOW;
bool previousSwitchState_RESET = LOW;

//LEDs
const int led_good = 8;
const int led_medium = 12;
const int led_bad = 13;
const int alarm = 7;

//SOUND SENSOR
#define soundSensor A0
unsigned long last_event = 0;

//OTHER
int ST; 		//sound threshold
int N = 0; 		//number of times ST has been exceeded
int C = 0; 		//cooldown status
int CT = 10000; 	//cooldown threshold
int L = 0; 		//current level
int LT = 3; 		//how high N needs to be to increase the level
int T = 0; 		//time passed without ST being exceeded
int TT = 10000; 	//time threshold; how high T needs to be


void setup()
{
  Serial.begin(115200);
  
  //BUTTONS
  pinMode(Button_UP, INPUT);
  pinMode(Button_RESET, INPUT);
  
  //LEDs
  pinMode(led_good, OUTPUT);
  pinMode(led_medium, OUTPUT);
  pinMode(led_bad, OUTPUT);
  pinMode(alarm, OUTPUT);

  //SOUND SENSOR
  pinMode(soundSensor, INPUT);
}

void loop()
{
  ButtonState_UP = digitalRead(Button_UP); 
  ButtonState_RESET = digitalRead(Button_RESET);
  
  //SOUND SENSOR
  int output = analogRead(soundSensor);
  if(output > ST && C >= CT)
  {
    C = 0;
    T = 0;
    N = N + 1;
    Serial.println("sound detected!");
  }
  
  //BUTTONS
  switchState_UP = ButtonState_UP;
  if(switchState_UP == LOW && previousSwitchState_UP == HIGH && C >= CT)
  {
    C = 0;
    T = 0;
    N = N + 1;
  }
  previousSwitchState_UP = switchState_UP;
  
  switchState_RESET = ButtonState_RESET;
  if(switchState_RESET == LOW && previousSwitchState_RESET == HIGH)
  {
    N = 0;
  }
  previousSwitchState_RESET = switchState_RESET;

  //COOLDOWN
  C++;
  
  //TIMER
  T++;
  if(T >= TT * 100)
    {
      if(N > 0)
      {
        N = N - 1;
      }
      T = 0;
    }
  
  //LEVEL 0 (good)
  if(N < LT)
  {
    L = 0;
  }
  
  if(L == 0)
  {
    digitalWrite(led_good, HIGH);
    digitalWrite(led_medium, LOW);
    digitalWrite(led_bad, LOW);
    digitalWrite(alarm, LOW);
  }
  
  //LEVEL 1 (medium)
  if(N >= LT && N<= LT*2)
  {
    L = 1;
  }
  
  if(L == 1)
  {
    digitalWrite(led_good, LOW);
    digitalWrite(led_medium, HIGH);
    digitalWrite(led_bad, LOW);
    digitalWrite(alarm, LOW); 
  }
  
  //LEVEL 2 (bad)
  if(N >= LT*2 && N < LT*3)
  {
    L = 2;
  }
  
  if(L == 2)
  {
    digitalWrite(led_good, LOW);
    digitalWrite(led_medium, LOW);
    digitalWrite(led_bad, HIGH);
    digitalWrite(alarm, HIGH);
  }
}