Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Voltaat Arduino Ultimate Kit
90 min
Share

Make a Clap Switch with Arduino and Sound Sensor

Sound is a very important aspect to consider in a number of projects and in this tutorial I will show you how we can make a simple sound dependent clap switch which can be used to control a number of devices for example lights in our home.

Project Video

Overview

In this tutorial, you will learn how to create a sound-activated switch using an Arduino, a Microphone Module, and an LED. This project will enable you to control a light or other device with a simple clap.

Getting the Items

Arduino Uno R3 (Voltaat Version)
Get Item
Microphone Module
Get Item
Clear 5mm LED (5 pack)
Get Item
1/4 Watt Resistor (20 Pack)
Get Item
Half-size Breadboard
Get Item
Jumper Wires – Male to Female (40 Pack)
Get Item

Steps

Wiring it Up

Connect the wires between the Microphone Module and the Arduino and the LED as shown in the image below.


Connections from the Arduino to the breadboard:

  • Arduino GND pin → Breadboard ground line

  • Arduino 5V pin → Breadboard 5V line



Connections from the Microphone Module :

  • Microphone Module VCC pin → Breadboard 5V line

  • Microphone Module GND pin → Breadboard ground line

  • Microphone Module A0 pin → Arduino pin A0



Connections from the LED :

  • LED anode pin → 330 ohm resistor first pin

  • LED cathode pin → Breadboard ground line



Connections from the 330 ohm resistor:

  • 330 ohm resistor first pin → LED anode pin

  • 330 ohm resistor second pin → Arduino pin 3

Coding

/*
Voltaat learn (http://learn.voltaat.com)
Link for full tutorial:https://learn.voltaat.com/2023/05/03/measure-and-detect-the-co2-concentration-using-the-arduino-%f0%9f%8f%ad/

Tutorial: Make a Clap Switch with Arduino and Sound Sensor!

The purpose of this sketch is to turn the LED on and off when a clap is detected by the microphone module.

Connections from the Arduino to the breadboard:

• Arduino GND pin → Breadboard ground line

• Arduino 5V pin → Breadboard 5V line



Connections from the Microphone Module :

• Microphone Module VCC pin → Breadboard 5V line

• Microphone Module GND pin → Breadboard ground line

• Microphone Module A0 pin → Arduino pin A0



Connections from the LED :

• LED anode pin → 330 ohm resistor first pin

• LED cathode pin → Breadboard ground line



Connections from the 330 ohm resistor:

• 330 ohm resistor first pin → LED anode pin

• 330 ohm resistor second pin → Arduino pin 3

*/

// Declare variable Sensor and assign it to A0 analog input pin
int Sensor = A0;

// Declare variables
int clap = 0;
long detection_range_start = 0;
long detection_range = 0;
boolean status_lights = false;

// Setup function that runs once at the beginning of the program
void setup() {
// Set Sensor pin as input and pin 3 as output
pinMode(Sensor, INPUT);
pinMode(3,OUTPUT);
}

// Loop function that runs repeatedly throughout the program
void loop() {
// Read the status of the Sensor pin and store it in status_sensor variable
int status_sensor = digitalRead(Sensor);

// If the Sensor pin is LOW (i.e. a clap is detected)
if (status_sensor == 0)
{
// If the clap count is 0 (i.e. first clap is detected)
if (clap == 0)
{
// Record the start time of the clap detection and increment clap count by 1
detection_range_start = detection_range = millis();
clap++;
}
// If the clap count is greater than 0 and the time elapsed since the last clap is more than 50ms
else if (clap > 0 && millis()-detection_range >= 50)
{
// Record the time of the clap detection and increment clap count by 1
detection_range = millis();
clap++;
}
}

// If the time elapsed since the first clap is more than 400ms
if (millis()-detection_range_start >= 400)
{
// If two claps were detected
if (clap == 2)
{
// If the lights are currently off
if (!status_lights)
{
// Turn the lights on and set the status_lights variable to true
status_lights = true;
digitalWrite(3, HIGH);
}
// If the lights are currently on
else if (status_lights)
{
// Turn the lights off and set the status_lights variable to false
status_lights = false;
digitalWrite(3, LOW);
}
}
// Reset the clap count to 0
clap = 0;
}
}

Testing it Out

Once you’ve uploaded the code to the Arduino board, you will find that when you clap, the LED will toggle between on and off states. As shown in the video below.

Resources

No items found.