Arduino: Using a Sound Sensor to Determine Ideal Audio Levels

In this tutorial, you will learn how to use the Arduino Uno and several parts of circuitry as in order to create a simplified sound level reader, which utilizes multiple LED's to indicate the sound levels and whether or not the sound levels are too loud in certain areas. The final circuit will be relatively simple, with 1 green and 1 red LED, each to indicate whether a certain area is too loud, or just right, these will be attached to a breadboard which will also be attached to the sound sensor. The overall difficulty level of this tutorial is not very challenging, however there are certain parts of the tutorial where some problem solving skills may be required.

In this tutorial you will learn the following skills-

  • How to control LED's
  • How to use the sound level sensor to set a threshold for an audio level
  • How to correctly create a circuit with 2 LED's and an analog input sound sensor
  • How to use the serial monitor

The following code functions -

  • digitalWrite
  • pinMode
  • int
  • delay
  • if
  • else
  • analogRead
  • Serial.begin
  • Serial.println

Required Materials-

  • KY-038 Sound Sensor or similar
  • Arduino Uno
  • 10 Dupont/Arduino Wires/Cables with male connectors on each end
  • 4 Dupont/Arduino Wries/Cables with 1 male and 1 female connector on each end
  • 1 Arduino 5mm Red LED
  • 1 Arduino 5mm Green LED
  • 2 220 ohm Arduino Resistors
  • 1 Mini Breadboard
  • 1 A-male to B-male USB Cable
  • Laptop or Computer with Arduino coding software installed

Step 1: Assembling the Base Circuitry

Required Time: 10 minutes

In order to begin the assembly of the circuit, we first need to connect all of the wires into their corresponding ports. For the red LED, insert 1 male/male dupont wire into port 9 on the arduino uno. For the Green LED, insert another wire identical to that of the one used for the red LED into port 5 on the arduino uno. Next, place 2 more male/male dupont wires into a Ground/GND port each. Using 3 male/female dupont wires, insert the male connector on the first wire into the A0 (analog 0) port, then insert male connector the second wire into the 5v port. Finally, insert the male end of the last male/female dupont wire into one of the Ground/GND ports on the arduino. Next, insert the corresponding port of the USB cable into the arduino female B port, and connect the other end to the computer. If the computer is on, you should see 2 small lights activate on the arduino.

Once all of the wires have been connected, the rest of the circuitry should be ready to be assembled.

Step 2: Connecting the Wires to the Breadboard

Required Time: 15 minutes

Place the mini breadboard next to the arduino, so that the wires can easily reach all ports. Plug both of the male/male cables connected to the ground ports into slots on the same horizontal row in the mini breadboard, around 8 slots/ports/holes apart on the far side of the breadboard. Next, plug both the port 9 and port 5 male/male dupont wires into separate holes/ports on the other, closer side of the breadboard on the same horizontal row, approximately 8 holes apart and making sure that none of them are vertically aligned with the ground cables.

Once all of the cables have been connected properly, you are ready to proceed to the next step.

Step 3: Finalizing the Circuitry - Connecting the LED's and Sound Sensor

Required Time: 20 minutes

In order to connect the LED's, you need to first ensure that you can distinguish between the long and short legs for each LED, so that you can determine which one goes where. Before we connect the LED's, we need to connect the resistors to make sure that the LED's don't burn out. Place the first leg of the first resistor in the hole in front of the wire connected to port 9, and then place the other leg on the other side of the breadboard, across the gap in the same horizontal row as the closest ground cable, around 2 or 3 holes apart to the left. Repeat the previous part of the step for the cable connected to port 5, with the other ground cable. Make sure that the resistors properly line up in the same vertical row as the cables connected to ports 5 and 9. Next, we will insert the LED's. in the furthest horizontal row of the breadboard, place the long leg of the red LED in the hole in the same vertical row as the resistor connected to the cable from port 9, then connect the short leg into the hole in the same vertical row as the closest ground cable. Repeat the previous part of the step with the green LED, connecting it to the cable and resistor from port 5, and the other ground cable.

(The reason that the LED's and the resistors can be connected to the same vertical rows yet not touch is because all vertical rows are connected, while the horizontal rows are not).

For the second part of this step, we will connect the sound sensor. The sound sensor should have 4 pins sticking out from it, each with different markings (AO, G, +, DO). First, connect plug the female end of the dupont cable plugged in the A0 port into the AO pin on the sound sensor, this is the cable that sends analog inputs to the arduino. Next, plug the female end of the dupont cable inserted into a ground port into the G pin of the sound sensor. Finally, we need to plug the female end of the dupont cable connected to the 5v port in the pin marked with a + sign. This provides power for the sound sensor. We do not need to connect the DO (Digital Output) pin the anything, because for this tutorial we will be using analog inputs. If the sensor is connected properly, you should see 2 small red lights illuminate on the sound sensor.

Step 4: Establishing the Foundations for the Code

Required Time: 30 minutes

In this part of the tutorial, we will begin the second major part of creating the sound sensor; writing the code.

First, open a new project in the arduino software and give it a name relevant to the project. Next, we are going to establish the ports for each of the LED's, and for the sound sensor. In order to do this, we need to begin writing code above the void setup() section of the automatic pre-written code. To establish ports, we use the command "int". Writing the code for the arduino to recognize a port is simple, and all it requires is the int function, then the name that you want to give your port or whatever is connected to the port then using the equal sign to show what port the name is equal to by writing the number or name of the port as shown on the arduino board, then by finishing your code with a semicolon. An example for determining the port for the red LED is as follows:

int RedLed = 9;

You should be able to rewrite this line of code multiple times for green LED with port 5, and for the sound sensor with the port A0. Once you have done that, you need establish another variable for the sensor that is not a port, but a variable to store data coming from the sensors. The code should look like this: int sensorValue =0;

After this, the final variable that we have to declare is the threshold for the ideal sound level, which when surpassed, should illuminate the red LED. In order to do this, we must first find out what the ambient and constant sound level of a room is without any loud noises, and for this we need to use the serial monitor function of the arduino software.

Create a new program in the arduino software, and use the int function the establish the sensorpin value and the sensorValue. Next, in the void setup section of the pre-written code, write: Serial.begin(9600); This initializes the serial monitor and establishes its frequency. Next, in a new line, we write pinMode(sensorPin,INPUT); this tells the arduino that values coming from the sensorPin, or the A0 port are to be detected as inputs. After this, we will write our first code in the void loop section. The first thing that we need to do is establish what the sensor value is, and since its an analog signal we use the code: sensorValue = analogRead(sensorPin); This code tells the arduino to read the signals as an analog signal using analogRead, and it reads the signals from sensorPin, or the A0 port. In a new line, we write the final piece of code with is: Serial.println (sensorValue, DEC); This code tells the arduino software to print out the sensorValue inputs in the serial monitor using the Serial.println function. To upload to code to the arduino, ensure that the arduino uno is connected to your computer and press the arrow key facing right near the top left corner of the program. Once it is uploaded, select the tools drop-down menu and then select the serial monitor. After a few seconds, a continuous column of values should appear and new values will constantly be printed, and the values reflect the current audio level. Most of the time, if the room is quiet the values should remain more or less constant, and only deviate within 1 or 2 consecutive integers. Now, try clapping or making a loud noise for as long as you can, and you should see the values increasing by large amounts. These are the audio levels that you need to declare as too loud, as well as being the values that you use for your threshold in the main code. Going back to your main code, establish a new variable using int and name in threshold, and the value that it is equal to should be a number that is 2 or 3 integers greater than your constant audio level in a quiet room.

If you have successfully completed this step, then it means that you have correctly established all variables for your code and this should allow us to move onto the next part of the programming.

Step 5: Determining Input and Output Ports

Required Time: 10 minutes

This is a relatively simple step, with a purpose of creating code to help the arduino determine if it should output values to certain ports, or if it should detect input from certain ports. To do this, we need to use the pinMode function in the void setup code location. The code should look like this for the LED's: pinMode(ledPin,OUTPUT); and like this for the sound sensor: pinMode(sensorPin,INPUT); When writing this code, make sure to establish all variables for both LED's and the sound sensor.

This completes this step, and it allows us to proceed to the next part of the tutorial, where we use loops and the if and else statement to turn LED's on and off.

Step 6: Turning on the Red LED

Required Time: 15 minutes

In this part of the tutorial, we begin to write the code to turn the Red LED on when the sound level surpasses the threshold, and becomes to loud. To do this, we first need to create the code that allows the arduino software to read the input values from the sound sensor in order to determine whether or not it has exceeded the threshold. To do this, we can use the same function that we used when working with the serial monitor, which is the analogRead function: sensorValue = analogRead(sensorPin); Once we have written this piece of code, we can begin to use the if function in a new line. In order to turn on the red LED, the sensor value has to be greater than the threshold, so we can write:

if (sensorValue >= threshold) {

This code basically dictates that if the sensor value detected is greater then the threshold, then do the following, with the following being whatever code takes place after the curly bracket. The code that follows should turn on the red LED, which indicates that the environment that it is in is too loud, since the audio threshold was exceeded. In order to turn on the LED, we can use the function digitalWrite(redPin, HIGH); This code is relatively simple and instructs the arduino to send power to the red LED in order to turn it on, but we also need to make sure that the green LED remains of to avoid confusion. To do this we can reuse the digitalWrite function, however instead of using the 'HIGH' variable to turn the LED on, we can use the opposite variable, which is 'LOW', and this should turn the green LED off. The code may look complete from here, but the red LED will not stay on for long enough for us to see it, because we missed a vital function- the delay function, which tells us how long to keep the previous function active in milliseconds. So in order to let the red LED to stay on for long enough for us to see it, we need to use the delay function and in brackets next to it, establish the amount of time that we want it to remain on so that we can see it. If I wanted to red LED to stay on for 5 seconds whenever the threshold is surpassed then I would write: delay(5000); This would complete our code for turning to red LED on, so we would add another curly bracket ( } )after the delay to tell the arduino that this is the end of the code that it has to follow if the if statement is registered.

This completes the code for turning the red LED on, leading us to the next step, which is the else function, that should turn the green LED on.

Step 7: Turning on the Green LED

Required Time: 10 minutes

In order to allow the if statement that we used in the previous to work properly, we need to give the arduino an option to do something else in case the if statement is not registered, which is where we use the 'else' statement. In the else statement, we will reuse the digitalWrite function in order to turn the green LED on, because since the threshold was not exceeded and the if statement was therefor not followed, the red LED was not turned on, so the arduino will need to perform something else, which will be turning the green LED on in order to indicate that the sound levels are in the ideal level. To do this, we first need write the else statement, followed by a curly bracket in a new line: else {

This function tells the arduino what to do if it needs to do something else, so we need to tell it to turn the green LED on. To do that, we only need to write: digitalWrite(greenPin, HIGH); As well writing: digitalWrite(redPin, LOW); to make sure that the red LED stays of when the threshold is not exceeded. For this part of the code, we do not need to use a delay function to keep the green LED on, since the else function and its contents/included functions are regarded as a constant so they stay active unless instructed otherwise or interrupted by the if statement. So to finish the else statement, we just have to end it with a curly bracket: }

This completes all of the code required to operate the circuitry, and if everything has been performed correctly, the sound sensor and the LED's should all work, creating a functioning audio level indicator.

Step 8: Adjusting the Sensitivity of the Sound Sensor

While the circuit and the code may be complete, you may need to adjust the sensitivity of the sound sensor in order to adjust it to different environments.

If you are using the specified ky-038 sound sensor, then on the sensor itself there should be a small blue box on it, and on top of the box there should be a small metallic dial, with a notch cut into it. When the dial is turned, it can either decrease or increase the sensitivity of the sound sensor, which allows it to adjust to different environments by being more sensitive for a quiet environment, or by being less sensitive for a noisier environment and so on.

Step 9: Troubleshooting

In the unfortunate event that your circuit or code does not work, there are a serious of procedures that you can take in order to try and fix the issue.

  1. Make sure that the LED is working and is not burnt out
  2. Make sure that you are using a 220 ohm resistor
  3. Check that all necessary lines of code end with a semicolon ( ; )
  4. Check that you have not accidentally typed a stray symbol somewhere in your code
  5. Ensure that you have used 'int' to declare all ports
  6. Ensure that you have no typos and unnecessary capital letters (the programming language is case sensitive)
  7. Make sure that the 2 red LED's on the sound sensor are on, indicating that its functioning when plugged in
  8. Try adjusting the threshold value or the sensitivity of the sound sensor to make sure that it does not have a sensitivity that is either too low or too high.
  9. Try using a different breadboard
  10. Try different ports (making sure to declare the new ports for each wire if swapped)
  11. Try replacing the wires
  12. Try different resistors
  13. See if your arduino program needs any software updates
  14. Make sure you have declared all ports as input or output
  15. Make sure that you used a greater than (>) symbol instead of a less than (<) symbol for the if function
  16. Check that you don't have any missing or extra curly brackets
  17. Make sure that your threshold isn't too high or low.

Hopefully the aforementioned steps helped you fix your circuitry/code if you had any problems!

Thank you for following the tutorial

Share

    Recommendations

    • Big and Small Contest

      Big and Small Contest
    • Toys Contest

      Toys Contest
    • PCB Contest

      PCB Contest

    Discussions