Arduino Nano - Sound Sensor

The sound sensor has the capability to detect the presence of sound in its surroundings. It can be employed to create projects that respond to sound, like lights that activate with a clap or a pet feeder that responds to sound cues.

This tutorial instructs you how to use the Arduino Nano and a sound sensor to detect sound. We will explore:

Subsequently, you have the flexibility to modify the code to trigger an LED or a light (through a relay) upon sound detection, or even enable the rotation of a servo motor.

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×Sound Sensor
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino Nano
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of Sound Sensor

The sound sensor can be used to detect sound in the surrounding environment. You can easily adjust the sensitivity of the sensor by using the built-in potentiometer.

Pinout

The sound sensor includes three pins:

  • VCC pin: needs to be connected to VCC (3.3V to 5V)
  • GND pin: needs to be connected to GND (0V)
  • OUT pin: is an output pin: HIGH if quiet and LOW if sound is detected. This pin needs to be connected to Arduino Nano's input pin.
Sound Sensor Pinout
image source: diyables.io

The sound sensor has a handy built-in potentiometer that lets you easily adjust its sensitivity. Additionally, it features two LED indicators:

  • One LED indicates the power status.
  • The other LED indicates the sound state: it turns on when there is sound and off when it's quiet.

How It Works

The sound sensor module includes a convenient built-in potentiometer that allows you to adjust the sound sensitivity. Here's how the output pin of the sensor behaves:

  • When sound is detected, the output pin is set to LOW.
  • When sound is not detected, the output pin is set to HIGH.

Wiring Diagram

The wiring diagram between Arduino Nano and Sound Sensor

This image is created using Fritzing. Click to enlarge image

How To Program For Sound Sensor

  • Initializes the Arduino Nano pin to the digital input mode by using pinMode() function. For example, pin D2
pinMode(2, INPUT);
  • Reads the state of the Arduino Nano pin by using digitalRead() function.
int soundState = digitalRead(2);

Arduino Nano Code - Detecting the sound

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-sound-sensor */ #define SENSOR_PIN 2 // The Arduino Nano pin D2 connected to the OUT pin of the sound sensor int prev_state = HIGH; // The previous state from the input pin int sound_state; // The current reading from the input pin void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(9600); // initialize the Arduino Nano's pin as an input pinMode(SENSOR_PIN, INPUT); } void loop() { // read the state of the the Arduino Nano's input pin sound_state = digitalRead(SENSOR_PIN); if (prev_state == HIGH && sound_state == LOW) Serial.println("The sound has been detected"); else if (prev_state == LOW && sound_state == HIGH) Serial.println("The sound has disappeared"); // save the the last state prev_state = sound_state; }

Detailed Instructions

  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino Nano
  • Clap your hand in front of the sound sensor
  • Check out the result on the Serial Monitor.
COM6
Send
The sound has been detected The sound has disappeared The sound has been detected The sound has disappeared
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Please take note that if you notice the LED constantly staying on or off even when there is sound present, you can make adjustments to the potentiometer in order to fine-tune the sound sensitivity of the sensor.

Now we have the flexibility to personalize the code and make it trigger an LED or a light when sound is detected. We can even make a servo motor rotate. For more detailed information and step-by-step instructions, please refer to the tutorials provided at the end of this guide.

Troubleshooting

If you encounter issues with the sound sensor not functioning properly, you can follow these steps for troubleshooting:

  • Reduce vibrations: The sound sensor is sensitive to mechanical vibrations and wind noise. To minimize these disturbances, try mounting the sound sensor on a stable surface.
  • Consider the sensing range: Keep in mind that this sound sensor has a limited sensing range of approximately 10 inches. For accurate readings, make sure the sound source is closer to the sensor.
  • Check the power supply: Ensure that the power supply is clean and free from noise. The sound sensor, being an analog circuit, is sensitive to power supply disturbances.

By following these steps, you can address common issues and improve the performance of the sound sensor.

Video Tutorial

Function References

Learn More