Clap Switch
30 Jul 2015For some time I was thinking of making a simple clap switch for my room. ( Kind of lazy to go and switch off the light and fan every time… :-) ) First get it work on a breadboard.
Circuit is simple and straight way. Feed the output from the mic to a comparator. The output of comparator will act as the trigger for micro-controller. Micro-controller detects the pattern and enable the corresponding relay switch. Micro-controller always waits for an interrupt (a clap). If a clap is detected it will wait and counts the following claps within a period of 1s. It will discard miss claps and only takes into account the pattern that is programmed. 2 claps means light on/off & 3 claps means fan on/off. There will be separate switch to enable/disable the clap circuit and precedence will be given for the physical switch. That means if the physical switch is on regardless of the relay state light/fan will be ON. If it’s not then it will depend on the clap circuit.
Code here.
/*
* RoomControl.c
*
* Created: 26-07-2015 04:01:21
* Author: ANANTHAKRISHNAN U S
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/delay.h>
#define F_CPU 1000000UL
#define RELAY_PORT PORTC
#define RELAY_CNTRL DDRC
int relay= -1;
void timer_init();
int main(void)
{
timer_init();
/* Enable output ports */
RELAY_CNTRL |= 0xFF; // First 4 bits are outputs
RELAY_PORT = 0xFF;
/* ENable INT0 (PIN 4) as interrupt*/
GICR |= (1<<INT0);
MCUCR |=(1<<ISC00); // rising edge will trigger INT0
sei();
while(1)
{
}
}
ISR(INT0_vect)
{
if (relay==-1)
{
relay = 1;
timer_init();
} else
{
relay=relay+1;
}
_delay_ms(100);
GIFR |= (1<<INTF1);
}
void timer_init()
{
TCNT1 = 0;
TCCR1B |= (1<<WGM12)|(1<<CS11)|(1<<CS10); // 64 pre scaling & CTC
OCR1A = 19625; // ~1 sec
TIMSK |= (1<<OCIE1A);
}
ISR(TIMER1_COMPA_vect)
{
TCCR1B &= ~(1<<WGM12);
TCCR1B &= ~(1<<CS11);
TCCR1B &= ~(1<<CS10); // 64 pre scaling & CTC
TIMSK &= ~(1<<OCIE1A);
if (relay== 2) // 2 clap
{
RELAY_PORT ^= 0x04;
}
else if(relay == 3) // 3 clap
{
RELAY_PORT ^= 0x08;
}
relay = -1;
TCNT1 = 0;
}
Next is to get it working on a general purpose board. ( Also need an AC to DC converter. I used an old phone adaptor for that purpose) Here is final thing. (With switchboard open to show relay switching)It will detect claps even when sound is playing in the background.But things won’t work if the volume is too high. Also it considers loud voices as claps. (Kind of obvious anyway) But it is smart enough to discard continuous loud noises. But if the loud noise matches the pattern then relay will be misfired. Both light and fan can be switched on and off separately. And there is a separate switch to disable the clap switch (When I need to hear music as loud as possible :-)).