Project Overview
In this tutorial, we are going to teach you how to build your own portable noise sensor that beeps according to how loud your surrounding is. Have you ever encountered a situation where maybe you spoke a bit louder than what you should? Or maybe you made too much noise? This is the solution you are looking for.
One of the main goals behind this is to be able to take the noise sensor anywhere you want, therefore, we will be using small and the least amount of components possible.
Arduino Pro Mini
The Arduino Pro Mini will be the brain for this small project, it will be receiving some data from the electret microphone and it will output a sound from the piezo buzzer that will be attached to it. We will also be using some LED lights that will indicate the status of our device, green being for on and red for activated.
Electret Microphone breakout board
This small breakout board couples a small electret microphone with a 100x Op-amp. This will amplify the sounds (voice, door knocks, etc) loud enough to be picked up by a microcontroller's Analog to Digital converter.The unit comes fully assembled as shown and works with 2.7V up to 5.5V. We won’t be taking advantage of all the properties of the microphone, we will be using it as a sound level sensor.
You won’t be using many materials, so here is a list of what you will need.
Materials required for project
Hardware
- 1 x Arduino or any microcontroller you prefer
- 2 x 220 Ohm resistors
- 1 x Piezo buzzer
- 1 x microphone
- 1 x 9V battery
- 1x 9v battery holder
- 2 x LED Lights (any color)
- Wires and cables
Wiring and connections
This is the schematics that illustrates how the connection is made for our little device.
Schematics for the circuit of noise sensor circuit
For this project, we are powering the Arduino with the RAW pin. From this pin, we can bias the microcontroller with up to 12 V, and the board will internally regulate the voltage and drop it, so you can use your board without any problem. Isn’t that neat?
After the power is out of the way we can focus on our microphone. This has 3 pins, one for the voltage that will be connected to the VCC pin (NOT ON RAW), the AUD pin will be connected to an analog input pin in the microcontroller, and the GND, well … it's connected to ground.
Next, the buzzer’s positive pin will be connected to a digital pin in the microcontroller, we will also include two led lights that will indicate the status of the device, each connected with a 220-ohm resistor to 2 different digital pins. Also remember that the negative legs of the components have to be connected to ground.
The following Fritzing schematic will illustrate better how the whole circuit will look like in real life.
Fritzing schematics of noise sensor circuit
Programming
Connect the Arduino to the computer, using the A to B USB cable and open the Arduino IDE software.
Go to Tools > Serial Port and make sure you have selected the proper serial port. (Ex. COM3)
Go to Tools > Board and make sure you have selected the Arduino Mega 2560 or any Arduino board you are using.
Now it's time to enter the code: You can copy the code I've developed below (feel free to play with and edit this code). If you prefer, go ahead and enter your own code. We acknowledge that there are several ways to achieve results with this project. If you have any suggestions or ideas please let us know in the comment section.
//www.jayconsystems.com
int mic = A2; //Analog pin for the microphone
int LedPin = 8; //LED pin for the red light
int LedPin2=9; //LED pin for the red light
int speakerPin =4; //Pin that will connect to the buzzer
int sensorValue = 0; //Initial value of mic without scale
int sensorValue2 = 0; //Initial value of mic scaled
int noise = 150; //Change this number to set the min value the mic has to read in order to turn the lights on
void loop() {
int sensorValue = analogRead(mic); //Read analog value from mic
sensorValue2 = map(sensorValue, 0, 1023, 0, 255); //Scale down analog readings to have a value between 0 - 255
//Serial.println(sensorValue2);//This is to print
//delay(50); //modify this to change the rate of change of the value
if(sensorValue2 >noise){ //Check if analog value is greater than threshold
digitalWrite(LedPin2,LOW); //making the GREEN led off
digitalWrite(LedPin,HIGH); //making the RED led on
//This is the pattern of tones I used
//This will create 3 beeps
//the tone function needs to be passed a Pin, a frequency and a duration
//which are the parameters that I am passing
tone(speakerPin,500,20);
delay(100);
tone(speakerPin,300,30);
delay(150);
tone(speakerPin,100,45);
delay(500);
tone(speakerPin,500,20);
delay(100);
tone(speakerPin,300,30);
delay(150);
tone(speakerPin,100,45);
delay(500);
tone(speakerPin,500,20);
delay(100);
tone(speakerPin,300,30);
delay(150);
tone(speakerPin,100,45);
delay(5000);
}
else
{
digitalWrite(LedPin,LOW); //Make the RED led off
digitalWrite(LedPin2,HIGH);//Make the GREEN led on
}
}
//www.jayconsystems.com
Completed project side view angle
Notes about the project
If you want to change the value of the noise level, I would recommend printing in the serial monitor the variable and to do a live test. For the serial communication, my microcontroller was set up for a 9600 baud rate in the code[Serial.begin(9600)], but in the serial monitor window, I had it set up to 19200 to be able to get proper, legible output readings. This might vary from computer and microcontroller.