• Usha Saha

Interfacing Sound Sensor with Arduino

Updated: Dec 1, 2020

Sound senor, the name itself signifies that a sound sensor is used to detect sound waves. It is very affordable and easy to interface with devices like #Arduino, nodemcu, esp8266, etc. A sound #sensor can detect voice, claps, doorknobs, loud voices, humming sounds, etc. This article covers a sketch on how to interface a sound sensor with Arduino, along with a circuit diagram, codes, and video solution.


List of components used:-


The components used for this #interfacing are a sound sensor, an Arduino board, a LED, a 1k ohm resistor, a breadboard, and jumper wires for connection purposes.

List of components

Bread Board:- A breadboard is a rectangular board with small holes in it which are used for connecting elements. The connections are not fixed and can be varied. In a breadboard, all the holes in one horizontal line are in series connection with each other. Breadboards are made from plastic and come in all shapes, sizes, and even different colors, the most common sizes are "full-size," "half-size," and "mini" breadboards.

Breadboard

Resistor:- A 1k resistor used in the circuit is shown below.

LED

LED:- A Light Emitting Diode is a semiconductor device that emits light when current flows through it.

LED

Jumper wires:- Jumper wires are used to establish a connection between different elements in the circuit and Arduino board.

Jumper Wires

Arduino Board:-Arduino Board can read inputs such as light on a sensor, a finger on a button, or a Twitter message and convert it to an output such as activating a motor, turning on an LED, publishing something online. All of this can be done by sending a set of instructions to the microcontroller on the board by the use of the Arduino programming language.


Arduino Board

Sound sensor:- A sound sensor typically has 4 pins namely analog, digital, ground, and positive. For this interfacing, particularly the digital pin is taken into account. It has a microphone(the one in the silver color in the picture) installed in it, which is an electric microphone.

The microphone constitutes a thin diaphragm which is similar to an eardrum. The diaphragm is one plate of a capacitor and the second plate is a backplate. When a sound hits the microphone it leads to the vibration in the diaphragm. These vibrations change the voltage between the capacitor plate. In this way, one can measure the amplitude of the sound. This is how an electric microphone works.


The black chip in the image is an lm393 comparator to digitalize the input. Also, there is one blue box which is a built-in potentiometer used for sensitivity adjustment. The module also has two #LEDs one for power and the other for status.


Sound Sensor

Circuit Diagram


The annotation of the circuit diagram is shown below. The connections are as follows VCC of the sound sensor is connected to 5 volts of Arduino board, the ground pin is connected to the ground of the breadboard, and the digital pin is connected to the 7th pin of the Arduino board. The negative terminal of the LED is connected to the ground of the breadboard and the positive terminal of the battery is connected to one end of the 1k resistor, the other end of the resistor is connected to the 12th pin of the Arduino board.


Annotation Circuit Diagram

The physical connections between the Arduino board and the breadboard are shown below.


Connection of Arduino to Breadboard

The physical connection between the Arduino board, breadboard, and the sensor looks like this.


Arduino, breadboard, and sound sensor connection


Code:


The step by step codes implanted is as follows.


const int ledPin = 12. 
  • A constant integer led pin is assigned pin value 12.

const int sensorPin = 7.
  • A constant integer sensor pin is assigned pin value 7.

unsigned long le = 0.
  • Another variable of unsigned integer data type is initialized to zero(0). Here the variable le store the time of the last event that has occurred. That is when the last clap or humming sound or any sound has happened.

boolean ledState = false
  • With this code, a boolean is initialized and stored with a false value.

pinMode (ledPin, OUTPUT).
  • In the first void, setup underpin mode the led pin is made output.

pinMode (sensorPin, INPUT).
  • The sensor pin which is the sound sensor is made the input.

int sensorData = digitalRead(sensorPin).
  • With this code, the sensor value is read via digital read function and stored in the sensor data.

if (sensorData == LOW) {
  • In the first, 'if' statement the sensor data equals low that is the pin goes low signal is detected.

if (millis() - le > 25) {
  • The second if checks if 25ms has passed for last sound, then the led gets off.

ledState = !ledState.
  • If the above condition is true the led states are switched via code.

digitalWrite(ledPin, ledState ? HIGH : LOW)
  • The code reads the high/ low status.

le = millis() 
  • This code stores the value of the last event that has occurred.



const int ledPin = 12;
const int sensorPin = 7;
unsigned long le = 0;
boolean ledState = false;
void setup ()
{
pinMode (ledPin, OUTPUT);
pinMode (sensorPin, INPUT);
Serial.begin(9600);
}
void loop ()
{
int sensorData = digitalRead(sensorPin);
 if (sensorData == LOW) {
  if (millis() - le > 25) {
   ledState = !ledState;
   digitalWrite(ledPin, ledState ? HIGH : LOW);
    }
    le = millis();
    }
    }

Download the codes from here.

sound sensor
.txt
Download TXT • 449B

Working


When the codes are applied to the Arduino board and a humming sound is made on the microphone, the internal structure of the sound sensor converts this analog signal to a digital one, and the LED glows. In a similar manner when again a humming sound is made the LED turns off. This project is designed in such a way that when the sound is detected and the led is off it goes on and when the led is one and sound is detected the led goes off.


Circuit view with Codes injected

A video explanation for a better understanding of the project.

Learn Electronics. (Sept 14, 2020). "Interfacing Sound Sensor with Arduino". https://www.youtube.com/watch?v=NJjKnQ_tESw


This specific #DIY interfacing deals with one of the many ways a sound senor can be used. On can also code in a different approach for making the switch ON and OFF. The sound sensor finds several applications in switches, security purposes, as well as for monitoring purposes.

See Also:

  1. Interfacing Water/Rain Sensor with Arduino

  2. Object detection using Ultrasonic Sensor & Arduino

  3. Interfacing Ultrasonic Sensor with Arduino

52 views
  • Facebook
  • YouTube
  • Instagram
  • LinkedIn
LearnElectronics
Contact Us
© Copyright - LearnElectronics 2021