Arduino Clap Switch

Made a clap switch using Arduino. First experiment with this microcontroller. Works pretty well.

The switch that I used for the lamp can be found in Wal Mart. I opened it up, took the switch out and soldered an electric wire to the same location where the switch was.

Here it is in Wal Mart:
http://www.walmart.com/ip/GE-15-Amp-Polarized-Tap-with-Switch-White/16677117

Here is what mine looks like (used to look like that, I cleaned it up a bit):
photo (5)

The code for the Arduino clap switch is below. Some values will probably need to be adjusted depending on the environment. For example, the sound sensor was getting higher values in my office than in my bedroom. Have fun!


/*
	Arduino Clap Switch
	Artem Kalinchuk
	
	The THRESHOLD, SOUND_SAMPLE_LENGTH, 
	CLAP_DURATION_WINDOW, and CLAPS_FOR_TRIGGER are
	all adjustable values and will probably need to be 
	adjusted depending on your environment.
*/

const int SOUND_SENSOR = A0; //pin for the Grove Sound Sensor
const int LAMP_RELAY = 2; //pin for the Grove Relay
const int THRESHOLD = 400; //the sound level that will be treated as a 'clap'
const int SOUND_SAMPLE_LENGTH = 200; //the amount of ms to wait before determining to turn off/on
const int CLAP_DURATION_WINDOW = 1500; //the amount of ms max to make the number of claps specified (ms)
const int CLAPS_FOR_TRIGGER = 2; //the number of claps for the relay to trigger

//kind of used the same way as 'delay' but does not pause code.
//I use this because I have multiple 'delays' running in my original code.
//this 'delay' will make sure the relay does not switch on and off to fast.
//The current time is set to 1000 ms min (in code below)
unsigned long lastLampRelayLoop = 0;
int soundSensorReading = 0;
int soundLength = 0;
int previousSoundLength = 0;
int soundSampleLength = SOUND_SAMPLE_LENGTH;
int clapDurationWindow = CLAP_DURATION_WINDOW;
int currentClaps = 0;
int relayState = LOW;

void setup() {
  pinMode(LAMP_RELAY, OUTPUT);
}

void loop() {
  soundSensorReading = analogRead(SOUND_SENSOR);

  if (soundSensorReading >= THRESHOLD) { 
    soundLength++;
  } else {
    if (soundLength > 1) {
      previousSoundLength = soundLength;
    }

    soundLength = 0;
  }

  if (soundSampleLength == SOUND_SAMPLE_LENGTH) {
    soundSampleLength = 0;

    if (previousSoundLength > 1) { 
      clapDurationWindow = 0;
      currentClaps++;

      if (currentClaps == CLAPS_FOR_TRIGGER) {
        relayState = !relayState;

        if (millis()-lastLampRelayLoop >= 1000) {
          digitalWrite(LAMP_RELAY, relayState);
          lastLampRelayLoop = millis();
        }
      }

      previousSoundLength = 0;
    }
  }

  if (clapDurationWindow >= CLAP_DURATION_WINDOW) {
    currentClaps = 0; 
  }

  if (clapDurationWindow <= CLAP_DURATION_WINDOW) {
    clapDurationWindow++;
  }

  if (soundSampleLength < SOUND_SAMPLE_LENGTH) {
    soundSampleLength++; 
  }

  delay(1);
}

14 responses to “Arduino Clap Switch”

  1. Kyle says:

    Hey! I think this project is awesome but i was wondering if i could get some info on the circuit. i do have a you tube account and ill give credit to you if i make a video on it.. heres one of my videos http://www.youtube.com/watch?v=Fx9IkBd4rrs

  2. Artem says:

    Kyle,

    This doesn’t have much circuitry other than adding a Grove Shield to the Arduino and connecting a Sound Sensor and a Relay to the Shield. All these components can be found online, on websites such as seeedstudio.com.

    You can, of course, create your own circuit based on the components that I used.

    http://www.seeedstudio.com/depot/arduino-uno-rev3-p-694.html
    http://www.seeedstudio.com/depot/grove-base-shield-p-754.html
    http://www.seeedstudio.com/depot/grove-sound-sensor-p-752.html
    http://www.seeedstudio.com/depot/grove-relay-p-769.html

    That’s the uno. I used a mega but the only difference is the shield size.

  3. Ronaldo says:

    hi .. I really like your work and was wondering if you could show me the schematic that you used on this one

  4. Artem says:

    Ronaldo,

    Like I mentioned in the previous comment, I did not create a schematic. I used Grove Shield components to make this work. You can look at the links posted in the previous comment to see the schematic for each component and put it together yourself.

  5. syah says:

    does this programming code can be applied with arduino nano?

  6. Artem says:

    Syah,

    Yes, it can be used with any Arduino model but some models do not support the grove shield so you will need to improvise.

  7. Pasha says:

    How can I control relay by button and claps together? i.g. switch on relay by claps and turn off by button.
    Thanks.

  8. faiz says:

    How did you connecting the lamp wire to the arduino? Which type of arduino did you used?

  9. Artem says:

    Faiz,

    I used a relay. The relay was switching the outlet for the lamp (see the picture). That’s the way I did it but you can do it a different way.

  10. Tuomo says:

    Hi! I am trying to build motorised rollcurtains. I would like it to work like this: three claps = curtains down (motor will run 4 seconds for example) then two claps = curtains up (same motor will run to opposite direction for 4 seconds).

    I have worked with your code two days, but I am still out of my league so I can`t get this to work properly.

    Can you help me, please?

    here is my code I came up with:

    const int SOUND_SENSOR = A0; //pin for the Grove Sound Sensor
    const int LAMP_RELAY = 13; //pin for the Grove Relay
    const int THRESHOLD = 520; //the sound level that will be treated as a ‘clap’
    const int SOUND_SAMPLE_LENGTH = 200; //the amount of ms to wait before determining to turn off/on
    const int CLAP_DURATION_WINDOW = 1500; //the amount of ms max to make the number of claps specified (ms)
    const int CLAPS_FOR_TRIGGER = 3; //the number of claps for the relay to trigger

    //kind of used the same way as ‘delay’ but does not pause code.
    //I use this because I have multiple ‘delays’ running in my original code.
    //this ‘delay’ will make sure the relay does not switch on and off to fast.
    //The current time is set to 1000 ms min (in code below)
    unsigned long lastLampRelayLoop = 0;
    int soundSensorReading = 0;
    int soundLength = 0;
    int previousSoundLength = 0;
    int soundSampleLength = SOUND_SAMPLE_LENGTH;
    int clapDurationWindow = CLAP_DURATION_WINDOW;
    int currentClaps = 0;
    int relayState = LOW;
    int ledPin = 2;
    int ledPin2 = 7;

    void setup() {
    pinMode(LAMP_RELAY, OUTPUT);
    Serial.begin(9600);

    pinMode(ledPin, OUTPUT);
    pinMode (ledPin2, OUTPUT);

    }

    void loop() {
    soundSensorReading = analogRead(SOUND_SENSOR);

    if (soundSensorReading >= THRESHOLD) {
    soundLength++;

    }
    else {
    if (soundLength > 1) {
    previousSoundLength = soundLength;
    }

    soundLength = 0;
    }

    if (soundSampleLength == SOUND_SAMPLE_LENGTH) {
    soundSampleLength = 0;

    if (previousSoundLength > 1) {
    clapDurationWindow = 0;
    currentClaps++;
    Serial.println (currentClaps); // That I could see number of claps from serial monitor.

    if (currentClaps == 3) {
    relayState = !relayState;
    digitalWrite (ledPin, HIGH);
    delay(4000);
    digitalWrite (ledPin, LOW);

    }
    else if (currentClaps == 2)

    digitalWrite (ledPin2, HIGH);
    delay(4000);
    digitalWrite (ledPin2, LOW);

    if (millis()-lastLampRelayLoop >= 1000) {
    digitalWrite(LAMP_RELAY, relayState);
    lastLampRelayLoop = millis();

    }
    }

    previousSoundLength = 0;

    }
    if (clapDurationWindow >= CLAP_DURATION_WINDOW) {
    currentClaps = 0;
    }

    if (clapDurationWindow <= CLAP_DURATION_WINDOW) {
    clapDurationWindow++;
    }

    if (soundSampleLength < SOUND_SAMPLE_LENGTH) {
    soundSampleLength++;
    }

    delay(1);

    }

  11. Edward Tay says:

    Have many voltage that you supply for your arduino? using a 9V battery?

  12. Edward Tay says:

    How* many voltage that you supply for your arduino? using a 9V battery?

  13. Artem says:

    Edward,

    I believe the Arduino ranges between 5-12v but I may be wrong. I was using 9v for this project.

  14. Artem says:

    Tuomo,

    You will probably need to control more than one relay (unless it’s a 2-in-1 relay). You will have to turn on the UP relay (and turn off DOWN relay) when there are 2 claps. Turn on DOWN relay (and turn off UP relay) when there are 3 claps.

    To time it, use the millis() function. Use a variable to store the last activity millis() (the last time you triggered one of the relays) and compare that to the current millis(). If it is more than 4 seconds, turn off all relays.

Leave a Reply