Clap Switch

17
STAGE#1: The Microphone and the Amplifier 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

description

Clap switch schematic diagram

Transcript of Clap Switch

STAGE#1: The Microphone and the AmplifierAs 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, andcreates 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.

Example#1RF = 100kRA = 1kVin= 0.010vAV = RF/RAAV = 100Vout = Vin * AVVout = 1v (Gain of 100)

Example2:RF = 1kRA = 1kVin= 0.010vAV = RF/RAAV = 1Vout = Vin * AVVout = 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 amplifiedwaveform intoa 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 comparesvoltages. 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 (+) inputis 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 willkeep a constant 1.5vat the negative input. When a loud sound is generated and amplifiedto a voltage higher than 1.5v, then the output willgo from 0-5v until the voltage at the(+) falls below 1.5v. We only needfor the output of the comparator to be high for amicro second or so,so don't worry too much about thetimeduration of the output of the comparator beinghigh.

You can change the resistor network around to make the reference voltage at thenegative input (-) higher or lowerusing this formula:

R1 is the resistor tied to the 5v line, while R2is the resistortied to ground.V(-) = [VCC / (R1+R2)]*R2V(-) = [5v / (7000+3000)] x 3000V(-) = 1.5v

HE VOLTAGE REGULATOR: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:1) 5V-Out2) Ground3) 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.

THE BRAIN (PIC10F222):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 portdedicaated to controlling the driving ofthe relay. So we are only using4 out of the8 pins. Read the software section to see how the chip is interpretting the comparator information, and how the algorithm is set up.

PIN-OUT: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.

THE PROGRAM: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 0x000MOVWF OSCCAL ; update register with factory cal value

INITIALIZEMOVLW B'0010' ; GPIO1=COMPARATOR IN - GPIO0-RELAY ACTIVATETRIS GPIO ; INITIALIZECLRF ADCON0 ; ADC DISABLECLRF GPIO ; CLEAR OUTPUTSMOVLW B'00001000'OPTION ; ENABLE GPIO2 AS A DIGITAL PORT

SCAN1:BTFSS GPIO,1 ; CHECK TO SEE IF LOUD NOISE (CLAP IS DETECTED)GOTO SCAN1 ; IF NOT, SCAN AGAIN (LOOP)ENSURE:BTFSC GPIO,1 ; HAS THE WAVEFORM GONE FROM HIGH TO LOW?GOTO ENSURE ; IF NOT, CHECK AGAIN (LOOP). IF SO, GOTO NEXT INSTRUCTIONCALL LOOP1 ; DELAYCOUNTDOWN: ; COUNTDOWN ROUTINEMOVLW 0XFF ; LOAD REFERENCE VALUEMOVWF TEMP5 ; LOAD TIMING REGISTER#1 WITH REFERENCE VALUE ABOVEMOVLW 0XFF ; LOAD REFERENCE VALUEMOVWF TEMP6 ; LOAD TIMING REGISTER#2 WITH REFERENCE VALUECOUNTDOWN2: ; ACTUAL DOUNTDOWN ROUTINEBTFSC 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 STEPGOTO COUNTDOWN2 ; GO BACK AND LOOK AGAIN FOR SECOND LOUD NOISEMOVLW 0XFF; LOAD TIMING REGISTER#1 WITH FULL VALUE AGAINMOVWF TEMP5 ; LOADDECFSZ TEMP6 ; DECREMENT T-REGISTER#2GOTO COUNTDOWN2 ; IF TIMING REGISTER#2 IS NOT Z, DO SECOND SCAN AGAINGOTO SCAN1 ; IF TIMING REGISTER#2 WAS 0, GO BACK TO START

RELAYACTIVATE:BSF GPIO,0 ; ACTIVATE RELAYCALL LOOP1 ; SERIES OF 3 DELAYSCALL LOOP1CALL LOOP1; 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 ***SCAN2:CALL LOOP1 ; *** SERIES OF 3 DELAYSCALL LOOP1CALL LOOP1BTFSS GPIO,1GOTO SCAN2ENSURE2:BTFSC GPIO,1GOTO ENSURE2CALL LOOP1COUNTDOWN3:MOVLW 0XFFMOVWF TEMP5MOVLW 0XFFMOVWF TEMP6COUNTDOWN4:BTFSC GPIO,1GOTO RELAYDEACTIVATEDECFSZ TEMP5GOTO COUNTDOWN4MOVLW 0XFFMOVWF TEMP5DECFSZ TEMP6GOTO COUNTDOWN4GOTO SCAN2

RELAYDEACTIVATE:BCF GPIO,0 ; DEACTIVATE RELAYRESET ; RESET PROGRAM

LOOP1:;THE LOOP ROUTINE WORKS LIKE THIS. WE LOAD TIMING REGISTER TEMP1/2 WITH 8-BITVALUES, AND IT WORKS TO ESSENTIALLY COUNT DOWN THE FIRST REGISTER, ANDWHEN 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.MOVLW 0X00MOVWF TEMP1MOVLW 0XF0MOVWF TEMP2

LOOP2:DECFSZ TEMP1GOTO LOOP2DECFSZ TEMP2GOTO LOOP2RETURN

END

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 aboutPOR 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 timingRC 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 somewires.

The single pulse from the monostable will triggerthe flip-flop, whichwill act totoggle it's output.The output can be connected to a relay driver exactly like the one seen inSTEP#4. That is a single clap-on, single clap-off circuit. If you have any questions, I am happy to help..

COMPONENTS:HARDWARE: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.

PART LIST:1x Prototyping board (Roughly 1" by 1") or bread board.1x 9v Connector1x LM324 Quad Op-Amp IC1x LM78L05 Mini 5v Regulator1x PIC10F222 Micro controller1x Electret microphone1x 5v Relay SPDT or SPST1x 2N2222 NPN Transistor2x 0.1uF Ceramic capacitors1x 1N4001 Diode1x 100k Ohm Multi-Turn Potentiometer1x 100k Ohm 1/4W resistor2x 10k Ohm 1/4W resistor1x 3k Ohm 1/4W resistor1x 7k Ohm 1/4W resistor1x 1k Ohm 1/4W resistor1x Project Box

ANOTHER CLAP SWITCH USING COMPUTER SPEAKERhis is a simple clap switch circuit with high sensitivity. The only unusual component is the single coil latching relay. While there are numerous means of generating the latching function, the latching relay greatly simplifies the circuit and provides a relay contact interface for driving a 115 or 230VAC load such as a light bulb.

Battery life is good since the load is only 400uA there is no relay coil current in either the On or Off states. The clap signal is picked up via a computer speaker that is utilized as a microphone, amplified by a high voltage gain audio amplifier and detected via a simple, but sensitive transistorized floating level detector (comparator). The high voltage gain amplifier will be discussed in a future article. The floating level detector portion of the circuit may be new to the world as I have never seen anything like this published. All circuitry consists of discrete components so it may be easily simulated.Schematic of the Clap Switch Circuit

Single coil latching relayEvery experimenter needs one of these latching relays in his bag of tricks inherent is an electromechanical memory property that remembers its state indefinitely in the absence of power. I cheated here because I did not have one on hand and tested the circuit with a standard non-latching relay type. It is permissible to use a 5V relay on 9V because the coil impulse lasts for only 80mS. These relays are relatively inexpensive following are two screen shots; one available on eBay and the others from DigiKey. Note that these are low power devices that may be used to interface to low power 115 and 230VAC circuits (e.g. 60W light bulb etc.). DigiKey also has a selection of single coil latching power relays that handle >10A I will let you do the search for these.

ANOTHER CIRCUIT