Introduction: Arduino Clap Sensitive Light Control (The Clapper)

Picture of Arduino Clap Sensitive Light Control (The Clapper)

In a spree of ordering little extras for my arduino, i picked up a sound sensor board similar to this one and i had the idea of controlling the lights in my room. During the long delivery period i started work on the code for such a project, using a button to simulate a clap. This i did fairly soon after scanning the arduino reference page to learn some new functions and i could succesfully switch an led. In the final project i linked the output to a relay capable of driving my lights and the microphone board in place of the button.

The requirements are on the next page.

Also Please vote for me in the contests ive entered if you like this project.

Step 1: Requirements

Picture of Requirements

Requirements:
Arduino, needs two digital I/O
Jumpers (M-M)
Relay(necessary for the mains voltage)
Breadboard
Resistor*
LED*
Computer (I just happened to use my Pi for this one)
Arduino IDE
Attiny (not essential but for compact-ness)
*For testing.

Step 2: Breadboard Setup. (No Relay)

Picture of Breadboard Setup. (No Relay)

To set up your breadboard, copy the fritzing file. The resistor value will depend on your LED, i just happened to know what mine required. Also check your microphone board as the pinouts may be different, mine had one output pin which could be changed from analogue to digital using a potentiometer on board. If your board has analog and digital outputs make sure you wire the digital output.

Step 3: The Code

Picture of The Code

Some very simple code just copy and paste into the Arduino IDE which can be found here.

const int buttonPin = 2;
const int ledPin = 0;


int buttonstate = 0;
int ledstate = 0;

void setup() {

pinMode(ledPin, OUTPUT);

pinMode(buttonPin, INPUT);
}

void loop(){

buttonstate = digitalRead(buttonPin);
ledstate = digitalRead(ledPin);

if (ledstate == HIGH && buttonstate == LOW) {
delay(250);
digitalWrite(ledPin, LOW);
}
if (ledstate == LOW && buttonstate == LOW) {
delay(250);
digitalWrite(ledPin, HIGH);
}
}

Step 4: Troubleshooting

As a point of reference when i clap a little LED on the microphone board lights up, if you have a constant flashing LED adjust the onboard potentiometer till it stops, from this point on the sensitivity can be adjusted to your preference.

If nothing happens at all when you clap check all your connections and check that your LED is wired Correctly.

Step 5: Relay Setup

Picture of Relay Setup

IF YOU ARE WIRING THIS TO MAINS VOLTAGE BE EXTREMELY CAREFUL, MAINS CAN KILL INSTANTLY, I AM IN NO WAY RESPONSIBLE OR LIABLE FOR ANY INJURY OR FATAL HARM THAT MAY BEFALL YOU AS A CONSEQUENCE OF FOLLOWING THIS TUTORIAL.

PURSUE AT YOUR OWN RISK

The relay version varies very little but to begin with, check your relay is for 5V.

Then check if your landlord or home owner is accepting of this project.

Switch the outlet/light switch off in the switchbox for your home, to reduce risk of electrocution.

Test your relay board before hooking it up to mains, a circuit like the one below should work.

Step 6: Condensed Version (Attiny85)

Picture of Condensed Version (Attiny85)

Steps on how to bootload and program your Attiny will be uploaded at a later date, in the mean time there are many tutorials out there on how to do it.

One fairly simple method that requires no extra hardware is using your arduino as an isp.

The Attiny pinout is below.

Step 7: Watch This Space

Videos and pictures of the project will be uploaded but in the mean time dont hesitate to ask questions, give opinions and feedback.
Inbox me if you have any questions.

Step 8: Gallery

Picture of Gallery

Comments

DevinB29 (author)2016-12-09

Is there a video of this whole project in action?

JoergO (author)2016-06-18

Meh - Since I'm just shopping on DX I came here only to find out what kind of microphone board I could best use. Unfortunately you don't say anything about that, its not in the requirements, it just suddenly pops up from nowhere. Is this like a 'sound detector' module?

batmanRulz (author)2016-06-11

with the help of @rclymer in the comments, can you provide the final code? im not very good with this. thanks for the project.

adanon (author)2016-02-16

Is that a 5a/220v relay?

AlistairM (author)adanon2016-02-16

I believe so.

adanon (author)AlistairM2016-02-17

Another question... How did you power the arduino?

SrivishnuT (author)adanon2016-05-25

well, if you look at the Arduino and see the ports, then maybe you could notice that you can power it using the usb port or just hook up a 9-volt battery adapter to that circle connecter in

DwainB1 (author)2016-05-17

What microphone did you use and did you have to use a special library for it?

AlistairM (author)DwainB12016-05-18

I used a cheap 99p module I got off eBay. It has a digital out pin, you tweak the threshold by way of a pot on the module and it'll go high if the volume of the sound exceeds the threshold. From that I just used a digital read, no library required. Hope that helps

rclymer (author)2014-01-19

Cool project, I built something very similar but with a motion sensor. If you're looking for some improvements, you could add a timer to automatically shut off the lights or listen for the next clap. For future reference, a digitalRead from an output pin, isn't necessary. The ledState variable should always contain the led status. Also, removing delays is always a step toward stronger code.

unsigned long currentMillis = millis();

buttonState = digitalRead(buttonPin);

if((currentMills - buttonMillis > 100) && buttonState == LOW) //assuming a pull-up resistor inverts the values.

{

ledState == !ledState;

digitalWrite(ledPin, ledState);

buttonMillis = currentMillis;

}

This loop will only run once the button is pushed(or sound is triggered) and it has been 100ms since the last time the button was pressed.

adanon (author)rclymer2016-02-16

Where should it be placed in the code?

AlistairM (author)rclymer2014-01-19

Thank you very much, i was trying to think of a simple way to incorporate two claps. that way it wont get set off by me coughing. My knowledge of code is quite basic, but i would greatly appreciate it if you could send a commented version of the code above to my inbox. that way i can further my knowledge. and build better code

robertzegwaard (author)2016-01-12

Like your setup, but how does it "know" the difference between a hand-clap and other sounds?

AlistairM (author)robertzegwaard2016-01-12

Thats a slight issue with it, I could cough and the lights would go off. There was an implementation of a double clap system. The code is somewhere in the comments.

EdwardU (author)2014-09-04

Fantastic! Finally got this to work after ages of trying. Great tutorial. I'd love to know how to get it turn on a series of different coloured LEDs in succession, and to make it respond to double claps. I know i need a delay in the code somewhere to make it listen for a second clap after it 'hears' the first, but I don't know how to do it!

AlistairM (author)EdwardU2014-09-04

I'm happy you enjoyed it, what was the issue with getting your setup to work? Somewhere in the comments I'm sure someone posted the code for a double clap, if not I'll see if I can upload a set/

MinP (author)AlistairM2015-10-31

Same problem here

GabrielS38 (author)2015-08-24

I just had a few questions. I am new to the Arduino, or any micro-controller, so these should be pretty easy. I went with an Arduino pro Mini (its 5v, dont worry) and i grabbed a 6pin FTDI cable, to connect it to my pc, i did this because it was smaller and the uno seemed like a bit much for what I needed. Also, is the breadboard necessary. I don't know what breadboards do, but I feel like there should be a way to cut it out of the circuit, because it is so large, and there are only like 3 connections to it. Lastly, I grabbed this resistor and this mic, https://www.sparkfun.com/products/8635, https://www.sparkfun.com/products/100, will these work and how would I connect them, preferably without the breadboard. Thanks a lot to whoever can help me

TillerReel23 (author)2015-05-18

Can anyone help me with converting to basic stamp?

DavidC7 (author)2014-09-01

Great project, thanks for sharing! I've been wanting to do something similar for a while. You could use an optocoupler and triac in order to isolate the logic voltage part from the AC part, thus protecting your microcontroller.

AlistairM (author)DavidC72014-09-02

I've only recently learnt about triacs and they would indeed make a nice and easy solid state alternative to the relay board I was using.

awusum_harsh (author)2014-04-05

can we use a 2 pin mic for this ???

AlistairM (author)awusum_harsh2014-04-06

look at the datasheet, some two pin mics need a resistor/capacitor setup. Once that is set up it should work fine

audreyobscura (author)2014-01-17

Awesome! Thanks for sharing your schematic and code with the community on Instructables.

AlistairM (author)audreyobscura2014-01-17

What improvements can be made, this is only my second instructable.

About This Instructable

115,491views

104favorites

License:

Bio: I am a young electronics hobbyist, interested in computers, rapid prototyping and electronics.
More by AlistairM:True Atmega Standalone, Running without crystal oscillator (Arduino)Arm Mbed Microcontroller BasicsArduino Clap Sensitive Light Control (The Clapper)
Add instructable to: