Arduino projects: Clap ON clap OFF light

This is a project come true for me after about dozen of year. 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.

Warning!

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 tutorial, 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!!

void setup()
{
pinMode(2, OUTPUT);
pinMode(A0, INPUT);
}
void loop()
{
if(analogRead(A0) > 600)
{
digitalWrite(2, !digitalRead(2));
delay(1000);
}
}

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

When you turn ON a 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.

long long int last_states = 0;
void setup()
{
pinMode(2, OUTPUT);
pinMode(A0, INPUT);
}
void loop()
{
last_states = last_states << 1; if(analogRead(A0) > 600)
{
if(last_states == 0){
digitalWrite(2, !digitalRead(2));
}
last_states++;
}
delay(20);
}

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

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

 

tchouken2

Related Posts