Making The Clap Switch Using Arduino Mega 2560

Introduction
  • In this article I will explain about making the clap switch by using ArduinoMega2560.
  • It can be used to operate by making sound
  • When the sound is produced it can be automatically ON. 
Parts Of Lists
  • ArduinoMega2560
  • Sound Sensor
  • Relay
  • Bulb or Led
  • HookUp wires 
Sound sensor
  • It can find the sound easily by the sound nature.
  • It can detect the sound of the environment.
  • It can have the three pin standard.
                                   
                          Figure 1 - Sound Sensor. 
Relay
  • A relay is an electrically operated switch
  • Many relay are operated by electromagnetic into mechanical switch.
  • It can be used to operate as the switch.
                            
                                       Figure 2 - Relay
 
Connection

Step 1 - Connection From Arduino mega To Sound Sensor
  • Connecte the Vin pin of the sound sensor to the 13 of the Arduinoboard.
  • Connecte the vcc pin of the sound sensor to the 5v of the Arduinoboard.
  • Connect the gnd pin of the sound sensor to the gnd of the Arduinoboard. 
Step 2 - Connection From Relay To ArduinoMega2560
  • Connect the same procedure as the sound sensor of vcc and Gnd.
  • Connect the Vin of the relay to the 12 of the Arduinoboard.
  • Connect the bulb or led as it the postive side and the negative side. 
Programming
  1. int soundSensor = 13;  
  2. int relay = 12;  
  3. int claps = 0;  
  4. long detectionSpanInitial = 0;  
  5. long detectionSpan = 0;  
  6. boolean lightState = false;  
  7.   
  8. void setup() {  
  9.   pinMode(soundSensor, INPUT);  
  10.   pinMode(relay, OUTPUT);  
  11. }  
  12.   
  13. void loop() {  
  14.   
  15.   int sensorState = digitalRead(soundSensor);  
  16.   
  17.   if (sensorState == 0)  
  18.   {  
  19.     if (claps == 0)  
  20.     {  
  21.       detectionSpanInitial = detectionSpan = millis();  
  22.       claps++;  
  23.     }  
  24.     else if (claps > 0 && millis()-detectionSpan >= 50)  
  25.     {  
  26.       detectionSpan = millis();  
  27.       claps++;  
  28.     }  
  29.   }  
  30.   
  31.   if (millis()-detectionSpanInitial >= 400)  
  32.   {  
  33.     if (claps == 2)  
  34.     {  
  35.       if (!lightState)  
  36.         {  
  37.           lightState = true;  
  38.           digitalWrite(relay, HIGH);  
  39.         }  
  40.         else if (lightState)  
  41.         {  
  42.           lightState = false;  
  43.           digitalWrite(relay, LOW);  
  44.         }  
  45.     }  
  46.     claps = 0;  
  47.   }  
  48. }  
Explanation
  • It can be used to generate the bulb as the sound of the clap
  • It can work with the function of the clap as the fixed. 
Output               

 
                                                  Figure 3 - Output

Up Next
    Ebook Download
    View all
    Learn
    View all