The other day when I was at the reuse center I came across a few ikea lamps. I picked up two low voltage spot lights for $2 total. I think they came from this lamp. I also bought a moon lamp for $0.86 and I am still not sure why they charged such an odd amount for it. I wasn't too sure about the moon lamp, but I knew right away what I would do with the spot lights. It wasn't even 12 hours prior that I was trying to sketch up an idea on my slate chalkboard in low light conditions and thought I needed some task lighting. That distraction was what spawned this project.
Surely these lights would do great on their own with just an on/off toggle switch, but where is the fun in that? So, I decided to make a clapper circuit that turned out to be something a little more too.
A "Clapper" is a device that will turn on or off an AC appliance that is plugged into it, such as a lamp or fan when it "hears" you clap twice in approximate succession. They were originally sold in the mid eighties and it appears you can still buy them today.
In my version of the clapper if you clap twice within one second, the circuit toggles the lamp output. On becomes off and vice versa. If you clap three times within one second, the lamps begin dimming up and down via PWM until a fourth clap is detected or a one minute timeout occurs, whichever comes first. The brightness value is then stored and restored for subsequent toggling of the lights on/off with the two clap event. I also added a relay output to turn on and off the moon lamp. To trigger this relay, you shine a laser beam at the circuit to toggle it. Laser beams begets moon beams. It's science.
Here is a video of the Clever Clapper in action.
In case you are confused by the title of this post being the Clever Clapper and the text on the chalk board that says The Dandy Dapper Clapper you should know that the Clever Clapper is the hardware that a Dandy Dapper Clapper uses to clap his or her way to optimal lighting conditions.
These spot lamps didn't come with anything other than the lamp and holder. I needed a way to hold the holder so I banged a couple of nails into a some poplar wood to attach the lamp holder to. Ok, so I didn't "bang" these framing nails into the 1" x 2" bit of poplar wood so much as I drilled a hole slightly smaller than the O.D. of the nail and pressed them in, but you get the idea. This is not a finished installation; just the prototyping stage.
Here is a pic of the lamp, lamp holder, lamp holder holder and the lamp holder holder holder ( aka wall ).
Since we are talking about mechanical stuff, below is a picture of how the circuit and moon lamp are mounted to a piece of red oak. The wood board was drilled and tapped to accept aluminum standoffs to screw the circuit board and TIP120 heatsinks to.
Here are a couple of pictures showing the inside of the moon lamp. I added this black box with relay inside to control the hot AC line of this lamp with a microcontroller.
You can download a .rar of the ATTiny2313 code and Clever Clapper circuit schematic here.
There isn't much to say about the software except that there may be some "legacy" code in it still. That is, variables and the like that I didn't delete when changing things around or removing functionality so the program would fit into the available program space.
Below is a picture of the schematic. Starting at the top left you can see an electret microphone and it's filtered power supply. This is fed into one half of an LM358 Op-amp setup for 100x amplification. The 100k0 potentiometer is used for clap detection sensitivity adjustment. There is a connection to PD3 here that is no longer used, but more on that later. Then, the signal, or more accurately, part of the signal passes thru a low pass filter and on to the other half of the LM358 setup as a comparator. The output of this comparator stage is fed to the ATTiny2313. Otherwise the circuit is pretty straight forward, standard 7805 power supply and uC accoutrements, TIP120 darlington transistor with pulldown resistors. Oh, and don't forget that 1n4004 flywheel diode across the relay coil or you will be replacing your uC in very short order.
Here is a close up of the circuit. You can see the electret mic in the lower middle. There are two phototransistors on the board but only one is connected. They are the clear things with one at the center top of the ATTiny2313. There is also a red tactile switch who's input to the microcontroller is interpreted as two hand claps. This is used in the event you want to be quiet and still toggle the lights on and off.
When I first started this project I knew that I wanted to detect hand claps. As a human, I am quite good at this but, I had no idea how to describe a hand clap to a microcontroller. So, to "see" as a digital device would, I opened Audacity and started clapping my hands. Below is a picture of the spectrum analysis. To help prevent false triggers, it appears I can easily block non-clapping frequencies above ~5khz. This significantly improved the reliability of the circuit.
As I alluded to earlier I was not able to implement all of the features I wanted in my clapper. This is because I ran out of program memory. The last build used 100.0% of the available space. Of course I could have re-written the code trying to optimize for space or more likely I will just use a different uC in subsequent hardware iterations, should I decide the current implementation is prosaic. I feel code and hardware optimizations are a worthy subject of focus, but too, sometimes you just want to get the job done.
Here is a video of one feature that didn't make the cut due to the lack of program memory space. This mode would have been activated by a succession of four claps. In this mode, the raw speaker input ( pre-lowpass filter ) is fed to the microcontroller and modulates the PWM output to the lamps. The light looks like it is "talking" to you. It is a little hard to see in the video as I think the automatic white balance function of the video camera takes some of the resolution away. I was sad to see this feature go.
Over all I am quite pleased with the Clever Clapper. It is a lot of fun walking into a room and command-clapping lights to turn on - to your desired brightness value no less. I have yet to decide if I want to remake the hardware to allow for more features. I may, as one feature I wanted was clap programmable clap codes for instantly setting different lighting moods.
// clap off
Comments
I am currently working on a same project as yours, I want to ask you a couple of question:
1. what is the comparator for? is it designed just to help the first potentiometer to "adjust" the mic sensitivity? or is there any reason?
2. how far can the mic detect your clap?
3. if according to your audacity experiment, the frequency of a clapping hand is below the 5Khz, why dont you just build it with LPF instead of BPF? just wondering.. will it make a difference with your current circuit?
hope to hear from you soon,
Thank you so much,
Alvin
One half of the OpAmp is setup as a comparator. It turns the amplified audio signal into a digital pulse to be detected by the microcontroller. The amplifier stage amplifies the audio and anything above the comparator threshold the microcontroller will take a look at.
I have my clapper in a room that is about 20' x 30'. I can clap anywhere in that room and activate the clever clapper. I am not out of amplification range either so it stands to reason your room could be larger and still have success.
The high pass filter is just passives to power the microphone. It happens to be a filter too so I labeled it that way. I did try a band pass filter in a narrow range around 5kHz, but there was not enough energy to activate the comparator. In the end I just kept the low pass ( and the mic power too ).
Best of luck in your project!
-Pete
It indicates that my filter is still not working, so any suggestion?
Thanks a lot
I suspect your filter is working, just not producing the results you desire.
You could eliminate the LPF by the comparator and the circuit would still operate. It would then pass audio frequencies above ~5 kHz to the microcontroller too.
Clap detection based on filters like this works ok. Of course, it is really just " loud noise " detection. Your voice and the cars driving by are all capable of producing sub 5 kHz audio. If this is amplified to a point greater than the comparator threshold then it will send a digital pulse to the uC. However, the uC is looking for 2+ of those distinct noises withing a time window so a lot of bangs and crashes are eliminated.
I have found that in actual practice mine only gives false triggers occasionally. I was folding clothes one day and the light toggled when I was shaking out a shirt and loud sneezing often, but not always will trigger the circuit to toggle the light output.
With the filtering, sensitivity adjustment and software all together the results are pretty acceptable.
If you have access to an oscilloscope and signal generator you can evaluate your filter design, but as I said before heavy filtering was not successful for me and in the end a compromise gave the best real world results.
-Pete
You can send me an email if you like. My address is listed in my Blogger profile.
-Pete
Thanks a lot for the code and everything!
And my tip is: You should not use _delay_ms() with dynamic values. If you want delay for x milliseconds, you do
for(uint16_t i = 0; i < x; i++)_delay_ms(1);
Good job on optimizing the code. you should post it up for others too!
I am not sure what you mean by not using _delay_ms with dynamic values. For one, I don't see where the clever clapper code does this and secondly, I am not so sure that it would be a bad thing to use a variable in a delay routine.
What advantage do you propose exists in writing a for loop around _delay_ms(1) for 20 iterations vs a single line _delay_ms(20);?
I've builded the circuit and works but not perfectly. Maybe u could help. When i clap 3 times i set PWM with fourth clap. When i clap 2 times it should turn off but light become brighter. When i clap two times light turn off. Everything else works good.
Good job getting your clever clapper "almost" working. Send me an email and I will try to help you thru the troubles you are having. My email address is in my profile in the side bar.
-Pete
I wanted to build my own clever clapper but I can't find the ATTiny2313 anywhere near my place. I also tried to online but I can't seem to find one. Is there any substitute for the ATTiny2313?
I took a look at sparkfun.com and it looks like they have 250 in stock. If you can't get one from them, send me an email and I can send you one.
-Pete