This instructable not only gives the reader the information needed to create a clap-clap on/clap clap off switching circuit, but the logic used to make a hardware-only clap-on, clap off circuit. More information on a hardware-only version can be found in the final section of this instructable.
This circuit employs some very simple and cheap circuitry, and a simple program. It is relatively easy to make a hardware only clap on/off circuit, but I wanted to create a circuit that required two claps to switch on, and two claps to switch off. The claps must be in quick succession, as seen in the video below, or else it does not work, which is the entire point of the circuit =) I will go into detail about the circuitry, and the program. I will also do my very best to answer any questions you guys may have. This device can be thrown together in a single evening.
The software works like this:
1) The software waits for an initial loud noise, then starts a countdown sequence.
2) If the device detects another loud noise within about 250 milliseconds, then the relay will toggle on. If the timer runs out of time without detecting another loud noise, the program resets.
3) Once two claps have been detected, and the relay toggles on. The software then starts the same sequence over again, only waiting to toggle the relay off.
4) Once the relay toggles off again after detecting two claps in succession, the program resets back to the original state.
Since the PIC10F222 has a limited instruction set, I had to add in some extra lines of code. I would have used the PIX18F1220, but that would have been over-kill. Ah, but how I miss the BTF (Bit Toggle) instruction =)
Step 1: PART LIST:
I'll start of by giving you guys a part list. The box and the screws are not necessary for this project, but I'll add them to the list.
1x Prototyping board (Roughly 1" by 1") or bread board.
1x 9v Connector
1x LM324 Quad Op-Amp IC
1x LM78L05 Mini 5v Regulator
1x PIC10F222 Micro controller
1x Electret microphone
1x 5v Relay SPDT or SPST
1x 2N2222 NPN Transistor
2x 0.1uF Ceramic capacitors
1x 1N4001 Diode
1x 100k Ohm Multi-Turn Potentiometer
1x 100k Ohm 1/4W resistor
2x 10k Ohm 1/4W resistor
1x 3k Ohm 1/4W resistor
1x 7k Ohm 1/4W resistor
1x 1k Ohm 1/4W resistor
1x Project Box
Step 2: THE POWER SUPPLY
This step is a short one. We're going to talk about the power supply circuit.
We only need a 9v battery to properly drive this circuit. We can use 7VDC and up. The 78L05 5V regulator is used here because this is a low-power device, and we need not use a big LM7805 when we have so little space to begin with.
The 78L05 has three pins:
3) Voltage-In (7VDC+)
We need only a 0.1uf ceramic decoupling capacitor between the 5v-Out line, and ground to rid the circuit of any unwanted high frequency interference that may come along. However, since we are using a battery, this capacitor is really optional. Good practice, more like. If you are using an AC-DC wall wart, it is suggested you place a 100uf electrolytic capacitor between the Voltage-In pin, and the ground line to protect against any surges on the line, and to smooth the DC going in to the regulator.
Step 3: THE SIGNAL AMPLIFIER AND COMPARATOR
As you can see from the picture below, we have an electret microphone connected to the ground line, and to one end of a 10k resistor. The other end of the resistor is tied to the 5v line. when an audio sound is sensed by the micrpphone, it changes that audio signal into a voltage that emulates the tone picked up. We can use that noise, but first we have to condition it. To rid ourselves of the DC component, we AC-couple the signal using a coupling capacitor. This signal will be extremely small, so we must first amplify it. We are going to do that using a handy-dandy LM324 quad op-amp IC. This chip has 4x on-board op-amps. We are only going to use two of them. Go here for the data sheet: http://www.national.com/ds/LM/LM124.pdf
The pull-down resistor to the right of the coupling capacitor is necessary for the operation of the non-inverting amplifier stage to work. In fact, all components in the below diagram are crutial. The way a non-inverting op-amp works, is it takes two values RA, which is the 1k resistor connected to the (-) input and ground, and RF (100k potentiometer), which is the feedback resistor, which is connected between the (-) input, and the output, and creates a voltage gain factor. The voltage gain factor (AV) is a multiplier. Once we determine the AV, we multiply the voltage at the input by the AV, and we have our output voltage. The 100k pot is used to vary the voltage gain. This will either increase the sensitivity or decrease the sensitivity of the circuit. The equation for AV = RF/RA in a non-inverting amplifier circuit.
RF = 100k
RA = 1k
AV = RF/RA
AV = 100
Vout = Vin * AV
Vout = 1v (Gain of 100)
RF = 1k
RA = 1k
AV = RF/RA
AV = 1
Vout = Vin * AV
Vout = 0.010v (No gain)
STAGE#2 The Comparator:
The standarad MCU does not like to work with sine waves, or any odd shaped wave. That is, unless you are using the ADC, which we are not. We want to turn our now amplified waveform into a 0-5VDC square wave that can be used by the PIC10F222. What are are going to emply here is the comparator circuit. A comparator does exactly as you'd think. It compares voltages. If the voltage at the (-) input is higher than the voltage at the (+) input, then the output will be 0v. If the voltage at the (+) input is higher than the voltage at the (-) input, then the output will be 5v.
We have a resistor voltage divider network at the negative (-) input. This will keep a constant 1.5v at the negative input. When a loud sound is generated and amplified to a voltage higher than 1.5v, then the output will go from 0-5v until the voltage at the (+) falls below 1.5v. We only need for the output of the comparator to be high for a micro second or so, so don't worry too much about the time duration of the output of the comparator being high.
You can change the resistor network around to make the reference voltage at the negative input (-) higher or lower using this formula:
R1 is the resistor tied to the 5v line, while R2 is the resistor tied to ground.
V(-) = [VCC / (R1+R2)] *R2
V(-) = [5v / (7000+3000)] x 3000
V(-) = 1.5v
Step 4: THE BRAIN AND THE THE RELAY SWITCH
Ah the good ol' archiac PIC10F222. This chip has an on-board internal oscillator, which exterminates any necessary external driving hardware, such as a crystal oscllator. We really only need 5v, Ground, the output of the comparator, and an I/O port dedicaated to controlling the driving of the relay. So we are only using 4 out of the 8 pins. Read the software section to see how the chip is interpretting the comparator information, and how the algorithm is set up.
Pin#2 = VCC (5v)
Pin#4 = GPIO,1 (Programmed as an input)
Pin#5 = GPIO,0 (Programmed as an output)
Pin#7 = Ground
The Relay Switch:
In order to drive our 5v relay using the output of a PIC, we have to set up a driver circuit, which in this case is a simple NPN transistor, and a diode. The diode does nothing to drive the relay, but it acts to protect the relay coil from surges. Any coil such as this should always be protected by a diode. The 1N4004 is as good as any. The base of the transistor is protected by a 10k ohm resistor. You want to make sure that you are using this, or else you will over-drive your transistor. When GPIO,0 outputs 5v (high or set), then the base of the NPN transistor is activated, allowing 5v to pass through the internal coil of the relay, and thereby creating a magnetic field that toggles the internal switch. When GPIO,0 is cleared (0v), then the transistor is deactivated, and the path from 5v to ground along the coil stops dead. The magnetic flield then collapses, and the relay swiitches back to default state. In other words, if you used the configuation below, you have 5v at the common wiper of the SPDT (Single pull double throw) relay. In default setting, the wiper is connected to the NC (Normally connected) pin of the relay. when the relay is activated, the wiper connects to the NO (Normally open) pin of the relay, and power is applied to the LED. When the relay is deactivated, the wiper re-connects to the NC pin, and the path from 5v to the LED is broken.
Step 5: SOFTWARE
I have done my best to comment out this program as best as I could. You should be able to follow along. It is no masterpiece, but again, I was working with an inferior instruction set. You can download the .ASM code and use it with your PIC ICD2 programmer puck, as seen in the image. I am also happy to answer any questions you may have relating to the software!
The ASM code can be downloaded here: http://www.electroniclessons.com/10F222TMPO.ASM
MAIN CODE 0x000
MOVWF OSCCAL ; update register with factory cal value
MOVLW B'0010' ; GPIO1=COMPARATOR IN - GPIO0-RELAY ACTIVATE
TRIS GPIO ; INITIALIZE
CLRF ADCON0 ; ADC DISABLE
CLRF GPIO ; CLEAR OUTPUTS
OPTION ; ENABLE GPIO2 AS A DIGITAL PORT
BTFSS GPIO,1 ; CHECK TO SEE IF LOUD NOISE (CLAP IS DETECTED)
GOTO SCAN1 ; IF NOT, SCAN AGAIN (LOOP)
BTFSC GPIO,1 ; HAS THE WAVEFORM GONE FROM HIGH TO LOW?
GOTO ENSURE ; IF NOT, CHECK AGAIN (LOOP). IF SO, GOTO NEXT INSTRUCTION
CALL LOOP1 ; DELAY
COUNTDOWN: ; COUNTDOWN ROUTINE
MOVLW 0XFF ; LOAD REFERENCE VALUE
MOVWF TEMP5 ; LOAD TIMING REGISTER#1 WITH REFERENCE VALUE ABOVE
MOVLW 0XFF ; LOAD REFERENCE VALUE
MOVWF TEMP6 ; LOAD TIMING REGISTER#2 WITH REFERENCE VALUE
COUNTDOWN2: ; ACTUAL DOUNTDOWN ROUTINE
BTFSC GPIO,1 ; CHECK TO SEE IF LOUD NOISE IS APPARENT.
GOTO RELAYACTIVATE ; IF YES, ACTIVATE RELAY (TOGGLE)
DECFSZ TEMP5 ; IF NOT, DECREMENT COUNTER#1 IF REG=0, SKIP NEXT STEP
GOTO COUNTDOWN2 ; GO BACK AND LOOK AGAIN FOR SECOND LOUD NOISE
MOVLW 0XFF ; LOAD TIMING REGISTER#1 WITH FULL VALUE AGAIN
MOVWF TEMP5 ; LOAD
DECFSZ TEMP6 ; DECREMENT T-REGISTER#2
GOTO COUNTDOWN2 ; IF TIMING REGISTER#2 IS NOT Z, DO SECOND SCAN AGAIN
GOTO SCAN1 ; IF TIMING REGISTER#2 WAS 0, GO BACK TO START
BSF GPIO,0 ; ACTIVATE RELAY
CALL LOOP1 ; SERIES OF 3 DELAYS
; THIS NEXT SEQUENCE IS JUST A MIMICK OF THE FIRST SEQUENCE, ONLY WE ARE
; WAITING TO TOGGLE THE RELAY BACK TO ITS ORIGINAL POTISION.
; I HAVE ADDED IN A SHORT DELAY AT THE BEGINNING, DENOTED BY ***
CALL LOOP1 ; *** SERIES OF 3 DELAYS
BCF GPIO,0 ; DEACTIVATE RELAY
RESET ; RESET PROGRAM
;THE LOOP ROUTINE WORKS LIKE THIS. WE LOAD TIMING REGISTER TEMP1/2 WITH 8-BIT
VALUES, AND IT WORKS TO ESSENTIALLY COUNT DOWN THE FIRST REGISTER, AND
WHEN THE FIRST REGISTER IS 0, THE PROGRAM DECREMENTS THE SECOND TIMING REGISTER (TEMP2). AT THIS POINT, THIS KEEPS ON DOING THE SAME THING UNTIL TEMP2 REGISTER VALUE = 0, AT WHICH POINT, THE DELAY ENDS, AND WE GO BACK TO THE PROGRAM.
Step 6: THE CONCLUSIONS!
I didn't think that this project would be so simple, but I ended up designing it as I built it. The hardware took about two to three hours, and the software took less than an hour. Some slight modificaitons had to be made along the way, but over all, the entire project was put together in one night. If I had a more recent PIC to work with, the program would have been MUCH more efficient. However, the program works perfectly.
I've build clap-on, clap-off circuits before, but never one like this. I hope that if you liked it, you'll vote for it = )
DON'T WANT TO USE SOFTWARE?
Try this on for size. Create the exact same power supply circuit (You can use a 7805 if you want), the same microphone circuit, and the same amplifier/comparator circuit. Buy a 555 timer, and a TTL JK flip flop such as the 74LS76 or the 74LS109A (My favorite).
1) Configure the 555 timer as a monostable multivibrator with a delay of about 100-500ms.
2) Configure the JK flip-flop so that is works in toggle mode.
3) Place a POR (POWER-ON-RESET) on the flip-flop IC so that it will start in a known state.
If you don't know about POR circuits, google one. It is a circuit that essentially clears a digital device for a short period on power on, so that the output will start in a known state. Sometimes, decoupling caps don't do the trick, and the start-up spike on power-on will mess with the chip function.
4) The signal coming from the comparator will be a square wave, but it will be a frequency, rather than straight DC. We need to turn this square frequency 10101010101 into a single pulse 01111111110. This is done using a monstable multivibrator. A 555 timer will do, and the data sheet will show you how. The monostable circuit, when triggered will offer a single pulse. This single pulse will be ideal for acting as a trigger to toggle our JK flip-flop. It will require a timing RC network, comprised of a single resistor and single capacitor.
5) If you don't know about JK flip-flops, they can be configured to act many different ways, You have to set it up into toggle mode. This can be done by looking at the data sheet. It will show you a configuration set-up for toggle mode. it doesn't require any external hardware, except some wires.
The single pulse from the monostable will trigger the flip-flop, which will act to toggle it's output. The output can be connected to a relay driver exactly like the one seen in STEP#4. That is a single clap-on, single clap-off circuit. If you have any questions, I am happy to help..
THANKS FOR LOOKING EVERYONE!