In this lesson, you’ll learn how to connect the sound sensor (KY-037) and how to process sounds both in analog and digital form with your Arduino UNO.
Structure of the Sound Sensor
The most important components of the sound sensor you’ll learn about here are: the microphone, a comparator (in this case the LM393, which compares two voltages), and a potentiometer (to adjust the threshold value). You connect the sensor using at least 3 of the 4 pins – anode, cathode, analog and/or digital output.
Connection to the digital output
You can connect the sound sensor to your Arduino in two ways – analog or digital. The digital connection is useful when you want to trigger something as soon as a loud noise is detected. This could be, for example, a knock on the door, or a bang. Here’s how to connect your sensor:
Power your sensor using the pins + and G (for Ground, or minus) and connect the digital output (DO) to digital pin 3 on the Arduino. Next, connect the analog output (AO) to pin A1 on the Arduino. And that’s it.
___STEADY_PAYWALL___
Now copy the following sketch and upload it to your Arduino UNO.
const int sensor = 3;
const int led = LED_BUILTIN;
int noise = 0;
void setup() {
pinMode(sensor, INPUT);
pinMode(led, OUTPUT);
Serial.begin(9600);
}
void loop() {
noise = digitalRead(sensor);
Serial.println(noise);
if (noise == 1){
digitalWrite(led, HIGH);
}else{
digitalWrite(led, LOW);
}
}
If the internal LED of the Arduino is now constantly lit, it means that the sensor is continuously detecting a sound. Now take a small screwdriver and turn the screw on the potentiometer to the left – until the LED goes out.
Make a loud noise directly next to the microphone – for example, snap your fingers. The LED should light up briefly. If not – turn the screw slightly to the right again. With a little sensitivity, you will find the right fine adjustment. You can also track in the Serial Monitor whether the sensor detects a sound: in “silence” you will see a zero there, and a 1 when there is a sound.
And now the analog output
When you connect the sound sensor in analog mode, you get real-time feedback on the volume. For example, you can make an LED light up as soon as a volume you determine is exceeded.
You have already connected the analog output (AO) of the sensor to your Arduino. Now upload the following sketch.
const int sensor = A1;
const int led = LED_BUILTIN;
int noise = 0;
void setup() {
pinMode(sensor, INPUT);
pinMode(led, OUTPUT);
Serial.begin(9600);
}
void loop() {
noise = analogRead(sensor);
Serial.println(noise);
if (noise > 200){
digitalWrite(led, HIGH);
}else{
digitalWrite(led, LOW);
}
}
To better track the volume, start the Serial Monitor and observe the sensor values there. Again, some fine adjustment is necessary. Set the potentiometer so that you see values in the serial monitor that are slightly below 200. Now, if you make a noise that is loud enough for the value to shoot above 200, the internal LED of the Arduino will light up.
So you see, it’s not enough here that a sound is detected. This sound must also be loud enough to turn on the LED.
In the next lesson, you’ll build an alarm system with the sound sensor. You will represent the volume with an RGB LED, and as soon as a certain volume is exceeded, an alarm signal will sound from the piezo buzzer.