High Voltage Code

Electronics and programming tutorials. Learn by building things!

Arduino Clap Switch: Controlling home appliances with a double clap!

Posted on Nov 15th 2019 by Joseph

In this project, we're going to build a simple device to control lights or any other electrical appliances with a double clap.

Essentially what we're be building today is a power strip with an attached clap switch. So instead of using a boring ON/OFF button, we'll use sound to turn things on and off!

How does it work ?

The main idea here is to use an Arduino controlled relay that will open and close the circuit of a power strip. If you're wondering what a relay is, you can think of it as an electromechanical switch. Heres how it works.

Inside our relay, there's a small electromagnet. When this electromagnet is supplied with a power source (e.g 5V from our Arduino) it attracts the moving part called armature. The armature then connects the contacts inside the relay and closes the external circuit.

In this context, the external circuit is the circuit of a power strip.

Relay explained

Here I described how the Normally Open (NO) relay works, but most relays can be used as Normally Closed(NC) as well. NC means that the external circuit will be closed until the relay is activated. Now in our case using NC would be impractical, because a constant current should be supplied from the Arduino just to keep the lights off for example.

Relay terminals

Now to register our claps, we will use a sound sensor that converts sound waves to electrical signal. The sound sensor we're using for this project has a patentiometer that is used to adjust the sensitivity of the sensor. This allows us to set a threshold at which the electrical signal becomes a digital LOW signal.

Finally this signal is going to be used by the Arduino to determine whether the relay should be activated or deactivated.

Shopping time!

Right, so for this project we'll need:

Now if you're making a permanent project like me, you will also need :

Also some wires, shrinkable plastic tubes for insulation and a power strip with cord that you can get in any local hardware store.


*In this project we'll be working with voltages that can kill you. Be extra careful. Make sure you disconnect the power strip from the electric socket when wiring, soldering or programming your Arduino. *

The wiring.

This is how our device looks schematatically. Fritzing Arduino clap switch schematic

Now if you prefer building this project on a breadboard first, it should look something like this:

Fritzing Arduino clap switch on a breadboard

Assembling the device.

Alright, so we'll start by removing a small part of the plastic sheath from the power strip cord. If you're from Europe, you will probably see these 3 wires.

Power strip stripped!

The blue wire is Neutral (N).

The brown one is Line(L).

The green-yellow is Protective Earth(PE).

Check this link if you're from another part of the world. https://www.allaboutcircuits.com/textbook/reference/chpt-2/wiring-color-codes-infographic/

So It's the N and L that interests us here.

Remove some of the insulation from the N. Make sure you don't damage the wire itself.

Completely cut off the L and remove some of insulation from both sides of the wire.

Leave PE as is.

Now we have to solder an external wire to N which should go to the N terminal of the AC-DC converter and we'll use our relay's Common (C) terminal to connect L from mains with the AC-DC module's L terminal.

It should be clear by looking at the picture below.

Connecting relay and acdc

The other end of the L wire should go to Normally Open(NO) terminal of the relay .

Assembling the rest from here should be straight forward. I used small piece of stripboard to make all the required connections, but it can be done without it.

Leave the comment if you need any further explaining. Here's how it looks in my project box with everything connected according to to the schematic.

Arduino Clapper circuit

The Code

Now that we have everything in place, time to program our little device. Make sure you disconnected your power strip from the power socket. Connect your Arduino to the PC, open the Arduino IDE and upload the code below:

int sensor = 2;
int relay = 4;
int clap = 0;
long detection_range_start = 0;
long detection_range = 0;
boolean relay_activated = false;

void setup() {
  // put your setup code here, to run once:
  pinMode(sensor, INPUT);
  pinMode(relay, OUTPUT);


void loop() {
  int status_sensor = digitalRead(sensor);
  if (status_sensor == 0)
    if (clap == 0)
      detection_range_start = detection_range = millis();
    else if (clap > 0 && millis() - detection_range >= 50)
      detection_range = millis();
  if (millis() - detection_range_start >= 400)
    if (clap == 2)
      if (!relay_activated)
        relay_activated = true;
        digitalWrite(relay, HIGH);
      else if (relay_activated)
        relay_activated = false;    
        digitalWrite(relay, LOW);
    clap = 0;

Credit to Abid Cg for writing the original code. We're using sligtly modified version of it.

Final remarks

Now you can disconnect the USB cable from Arduino, connect the power strip to the socket and test the device. The only thing left is to actually calibrate our device. As I mentioned in the beginning it is done by adjusting the potentiometer on the sensor module. Play with it to find the perfect position.

Anyways, here's our device in action.

Thanks for reading, leave your comments below and good luck on your projects!

If you liked this post, consider sharing it with your friends!
Joseph (Juozas)

Software developer with sick passion for electronics!

Subscribe to get all the latest updates on new projects and other news from High Voltage Code!