Arduino Projects: Clap ON Clap OFF Light

This is a project come true for me after about a dozen years. Let me tell you the story. A long time ago, as a toddler, I watched a movie where a man used a clap to switch off a lamp and it made more than excited. I so wanted to be able to do the same. Fast forward to today, I am building an Arduino project around that old dream of mine and I will show you how you can easily replicate it yourself.

It is a very simple project, at least when you are shown how to do it.


Before we begin, note that this project works with high voltage. Do it with adults and always double check before applying power. If you are not sure what you are doing, please skip this project.

In one of our previous tutorials, we built a relay controlled 230V lamp with Arduino. You will be better off visiting that Arduino project before coming back to this one.

Items needed

Here is a list of what we need for this project:

Here is the connection diagram. At this point, you don’t have to connect the cable with the plug and the socket to the relay.

First, we have to connect the microphone and the relay. Make sure to use the analog output of your microphone. I connected the microphone to the analog pin 0 (A0). I did connect the relay to the digital pin 2.

When you are done connecting, let’s upload the code below that turns the lamp ON with a clap. If everything works fine, you can now connect the plug to the relay. BE CAREFUL!!

The code we have just uploaded to the Arduino works, but it has a big downside.

When you turn ON loud music, your lamp will blink. We have to make the system more robust and able to ignore such noise.

How do we do it?

The idea I’ve got is to store the last few reads of the microphone output, then if a clap is detected (read from the microphone is high) check if there were a previous High and if it didn’t change the lamp state if it was quiet change the lamp state.

Yes, this is not perfect, and there are some more advanced ways to do this, but I think this is good enough for us at the moment.

Here is how we will do this:

  1. We will have a long long int
  2. Every few milliseconds we will shift this value by one (like this: value = value << 1) it will shift all bits by one to the left and add 0 at the beginning
  3. Then if the microphone will be bigger than some value we will increment our value by one
  4. At the same time if the microphone output is bigger than some values we will switch the lamp state but only when our value with long long int value is == 0, why 0? because it means that for the last reads, there was no loud sound detected.

Hope it makes sense. The code is way simpler than my explanation.

If you don’t get it from the text above, just check the code below and everything should be clear.

Thanks to this, our lamp will not react to, say a loud music

Simple and effective, just the way I love. Enjoy!

You might also like:

1 thought on “Arduino Projects: Clap ON Clap OFF Light”

  1. Pingback: 35 coole DIY Gadgets, die du machen kannst, um deine Freunde zu beeindrucken - Dekorationen

Leave a Comment