Clap switch with latching relay

Parkera

Joined May 3, 2016
106
Hi Parkera I've ordered up 2N7000 to switch on the relay so will have to go with it now.
Any ideas how to adjust circuit so clapper is more consistent?
Tried changing R1 and VR1 but no use.
A few comments:
The LM358 will work at 5V, but the output does not swing that close to the rails. This may have an impact on turning on the 2N7000. 9 volts should be OK. 12 volts (if you have that for a lab supply) also would be fine for testing purposes up to, but NOT including the relay (LEDs can tell you what is going on in the collector/drain circuit).

R4 will bleed off any stored charge in the gate capacitance of the 2N7000. The opamp may or may not do a good job of that. I recommend keeping it, but the value could be a bit higher to save a few microamps. Be warned that if you go too high there will be a significant turn-off delay (T=RC of the gate capacitance).

R1 adjusts the bias required for the microphone. Best to set it per the recommendation of the mic manufacture. This usually will give the microphone the maximum sensitivity. With 22K and 9V the current will only be 400uA max.

The threshold voltage divider (R2, VR1, R3) could easily be scaled 50 times what your original circuit shows, especially if you do wind up using a CMOS opamp. That would put that divider current around 75 uA.

VR1 sets the threshold for the comparator, which is the mic output vs. the DC voltage level at the non-inverting input (+). Remember that the microphone output will have a DC component (in total silence) that the "clap" waveform will ride on. Of course the output of the microphone is going to depend on several factors. The most important of these are:
* Microphone sensitivity
* Directional characteristics of the microphone
* How close you are to the microphone
* How "loud" you clap (usually a huge variable)
* The harmonic content (frequencies present) in the "clap" (also usually a large variable).

And now you know why you have been having "consistency problems". Unfortunately most of those are hard to control, making development of this kind of a project difficult. (I had the same kind of problems with one I built 15 years ago.) Unless you go with elaborate signal conditioning, about the best you can do is to optimize the circuit while testing it with a consistent "clap"; you want repeatability for comparative testing, think something along the lines of a recording of a typical "clap", played back with a high quality sound system.

No need to go crazy with the high quality, but you want something better than a 2" speaker, say a system with a frequency response of around 100 Hz to perhaps 8kHz and reasonably flat. Keep the speaker/microphone distance and the volume the same each time. If you can, suspend both the speaker and microphone in the air; this will minimize reflections, which are hard to control. Again, you don't need to go "nuts" with this, just be aware it has a significant impact on sound. The main thing is to compare "apples to apples" while you optimize the circuit.

After you have your circuit pretty well optimized, try it out in reality. From there, you can experiment with locations within the room and the VR1 setting. You are working with a simple circuit to do what is in reality a complex task, so don't expect too much. But it is a fun project to do. Good luck.
 
Last edited:

Thread Starter

Copey84

Joined Jul 27, 2015
198
Thanks for the detailed post Parkera.

Was beginning to think it is impossible to have this type of clapper circuit respond consistently, your post explains it well.

Im still to get 9v battery, when I do I'll try and set it up best I can given the info from your post.
At least I know for sure that perfecting the on off may not be possible with this design of circuit.

Just a few other things I'm not sure about, is the LM358 a CMOS chip? Decided to use on recommendation but not sure it is a CMOS, even after search on line.
Also what value should I make R4 when using 9v?
The condenser mic I'm using came in a mix bag of components no info on it. It's a small PCB mount type think there fairly common but can't get any info on correct biasing. I've had to lower R1 to 10k to get a response from speaker, should I go with 10k as 22k doesn't work? Or can you think of a more suitable value for R1?
 

Parkera

Joined May 3, 2016
106
Copey84 - First, let me make a correction to a bit of mis-information I gave you. R4 does not bleed of the charge from the 2N7000, it bleeds off the charge from the gate inside the CD4017, pin 14. on to your specific questions.

The LM358 is a bipolar opamp. It has relatively low current drain (~1-2ma) for a bipolar opamp, especially for when it came out. It's performance is considered roughly equivalent to a 741. It is a nice general purpose opamp, but it is nowhere near as low as a CMOS opamp. By the way, I like the TL072 if I'm looking for a JFET input/low noise/wider bandwidth opamp. It still is basically a bipolar device, thus still draws a similar amount of current.

I haven't worked with CMOS opamps much, so I can't make any recommendations. Their big strength compared to a bipolar opamp is their ability to have the output swing from rail to rail. They also have the advantages of a FET input automatically (high impedance and low noise). I just checked a data sheet at random (LMC660) and the quiescent supply current is a bit higher than a LM358. If that is typical, I would stay with a LM358 for your design where low-current is an important parameter. This application is not particularly difficult for an opamp, so almost anything will work. As a learning exercise it would be good to pour over the data sheets on several potential opamps and compare input, output and supply parameters (and of course cost and packaging).

The value of R4 is not at all critical for this application. As a general rule, unless there are other compelling reasons, I like keeping resistances 1Meg or under. The reason for this is when you get into very high impedances, solid state circuits tend to become more susceptible to outside noise.

Again, I have not worked much with electret mics, so I can't tell you much from experience. I would have to research and teach myself first, which I don't have the time for now. I did find a TI appnote which is probably just what you need. It can be found at http://www.ti.com/lit/ug/tidu765/tidu765.pdf. It does go into a mathematical design approach (the correct way to design), but I suspect it may challenge you a bit. Still, try to get as much out of it as you can. Their approach (a transimpedance amplifier) is a better approach than trying to process essentially an AC signal with a standard voltage amplifier - and it negates any previous recommendations regarding the opamp. (It just goes to show that there is always more than one way to do something and don't be afraid to consider a change in direction.)

Good luck.
 

Thread Starter

Copey84

Joined Jul 27, 2015
198
Great info again Parkera, thanks.

I'm going to stick with the LM358, seems good enough for this project.

I'm thinking of building the circuit in app note you provided, to see if there's a better response to clapping.
If so could I simply connect the output of new amplifier circuit to input of CD4017?
Could C5 and R6 be removed from output of new amplifier leaving just connection from amp to CD4017 through R4?
 

Parkera

Joined May 3, 2016
106
Copey84 - C5? Please post a current revised schematic as of this time. Also, since a transimpedance amplifier is a current to voltage amplifier, the input amplifier should have a very low input current and input offset current characteristics, not strong points of the LM358. Usually a FET input device is called for.
 

Thread Starter

Copey84

Joined Jul 27, 2015
198
Hi Parkera, C5 is a capacitor from app note diagram that you sent previously.
I thought maybe I could use the amplifier circuit from the app note and connect it to CD4017.
Thought C5 and R6 could be omitted, R4 coincidentally is on both diagrams on outputs of amplifiers, was going to leave it in circuit.
Anyway since you've pointed out I will need a FET instead of LM358, I'll just concentrate on trying to make original circuit work.

Finally got 9v battery, and 2N7000 arrived today along with relay. Will put it together later and change R values to see if I can improve the circuit. The app note has a value of 14k calculated for R1 so will try that.
Although not on diagram I have included positive feedback as suggested in previous post, should the value of the feedback resistor be scaled depending on R1? At the moment it's at 220k, 10 times the value of R1 at 22k. I'm thinking feedback resistor needs to be 140k, or is it not that important?
 

Parkera

Joined May 3, 2016
106
Copey84 - Yes, you could use the TI circuit to directly drive the CD4017. The integrator components (R4, C5, R6) will probably help to prevent false triggering from random room noise/sounds. However, if you find they are not necessary then you can drive the CD4017 directly from the output of a bipolar output type opamp (i.e. LM358 or TL072) without a pulldown resistor. R4/C5 adjust the attack time (rise time) of the integrated waveform. C5/R6 determine the decay time of the integrated waveform. The combination of the attack time, decay time and the internal threshold levels of the CD4017 effectively form the pulse width applied to the CD4017. It is a lot of variables to be playing with all at once; I would start out with R4=0 ohms and play with the C5/R6 time constant for best results in a quiet room. Then increase the value of R4 for best results in a noisy room. (There is probably a 'science' to determine the values, but I don't know that science.)

If you use the TI circuit (a transimpedance amplifier), I don't believe a bipolar-input opamp will work well because the input current-related parameters are not good enough. The TL072 is not an exotic opamp by any means - that is one of the ic's that Radio Shack carries (carried). They are cheap and readily available, so don't be scared to pick up a few. They even have the same pinout as an LM358 so you don't have to rewire (values may change though). It probably isn't the best opamp available for the purpose, but it is a far cry better than an LM358 as a transimpedance amplifier).

Hysteresis is another name for the "positive feedback" that was suggested. That requires a resistor to be in series with the + input (pin 3) of the original circuit, with a feedback resistor from the output back to the + input pin. The amount of hysteresis applied is = VoRin/(Rin + Rf) where Vo is the output voltage, Rf is the feedback resistor and Rin is the equivalent input resistor at the + input of the opamp. The equivalent input resistance = ((VR1 + R3) in parallel with R2) + Rin (the resistor directly attached to the + input pin).

That sounds more complex than it is, but I don't have a good way to draw and insert a schematic, so words will have to suffice - sorry. As for the amount of hysteresis to apply - I would try perhaps 2% (of the output) as a starting point and see how that goes. It is one of those things that falls under "optimization". Have fun experimenting.
 

Thread Starter

Copey84

Joined Jul 27, 2015
198
Hello again everyone.
Not had time to work on project recently, however I did have the circuit built up on breadboard and sat in room where it will be used. Wanted to test it for awhile before soldering up. Unfortunately it didn't work for long before the knew battery died.
Anyway I finally had a look at it this evening and replaced the battery with another knew one, connected it up and circuit works as supposed to. Suspected something's not right as battery shouldn't have ran down so quickly. Put meter into circuit to measure current when on and off and found that when red led is on 30ma is being used, and when off 22ma is used.
Can anyone answer why 22ma is flowing when circuit is supposed to be off?
Connections are ok, nothing shorting out that shouldn't be, circuit seems to work fine apart from current flow.
Appreciate any replys.
 

Thread Starter

Copey84

Joined Jul 27, 2015
198
Thanks for reply Alec_t, I've left pin 12 open and it has reduced the current down to 0.70ma.
Is it possible to have no current flow when circuit is off? That 0.70 will eat into battery life.
 

Thread Starter

Copey84

Joined Jul 27, 2015
198
Hi, I've included wiring diagram with changes made.
Never intended to use LEDs so not included. Since I've only got a dual relay coil I decided to use another mosfet to pulse the second coil.
Also note R4 is now a feedback resistor rated at 5.6M.
Basically is the circuit ok like this, not sure now because of current flowing when circuit supposed to be off.
1496855766154-941338675.jpg
 

Parkera

Joined May 3, 2016
106
"Is it possible to have no current flow when circuit is off? That 0.70 will eat into battery life."

I suspect the LM358 is taking the majority of the 0.7 mA, followed by the bias current for the microphone. If you tried to reduce the current in the threshold divider the circuit would probably become too temperature sensitive. You might be able to save about 20 nanoamps if you went with a CMOS opamp (input bias current), but that only gains you 0.003% and is nothing significant. Really, there is not much more you can do. Besides, what is wrong with 714 hours on a 9V battery?
 

Thread Starter

Copey84

Joined Jul 27, 2015
198
Hi Parkera, thanks for reply.
I disconnected op amp and mic and current stopped flowing as you said.
Bit disappointed I'll only get a month out of battery but it will have to do.

I went with a feedback resistor 10 times greater than R2 as suggested in a previous post, 5.6M was the closest.
Although having just recently seen your post on calculating feedback resistor not sure it's suitable now.
My calculations seem off, and it's hard to tell if reducing feedback R is making any difference.
Is the 10 times ratio suitable?
 

Parkera

Joined May 3, 2016
106
Hi Copey84 - The feedback resistor (R4) is connected back to the + input of the opamp, which provides hysteresis to that stage. Because the circuit does not fit the 'classic hysteresis' circuit, the exact calculation of the amount of hysteresis is not straight-forward, but you don't need to be concerned with that in reality. Remember the purpose of the hysteresis - to prevent "flashing" of the light on & off when the room has a degree of background noise near the threshold of a "clap". You mentioned the circuit is basically OK as is, I assume the lamp operation is satisfactory and you are just trying to get the current consumption down as low as you can.

While there is current flowing through the feedback resistor (from the battery, through the opamp output stage, through the feedback resistor, through the input components, to ground back and back to the battery) the 'simple' maximum possible current will be limited by the 5.6M + the parallel combination of the 560K and the 470K resistors = 9V/5.855M = 1.5uA. That is about 0.2% of your total current consumption (700 uA). Even if you doubled all values (R4 = 10M, input resistors would become about 1 megohms each), you would only save 0.1%. Temperature stability will also be degraded more than 2X (it is not a linear effect). Clearly you are at the point of diminishing returns.

Have you given thought to eliminating the batteries altogether? Obviously you have AC power available in VERY close proximity (for the lamp). All you would have to add would be a "wall wart" and a LM317 regulator.
 

Thread Starter

Copey84

Joined Jul 27, 2015
198
Hi Parkera, I've decided to go with a power supply, makes sense.
Have a spare 5v phone charger that i think will work.
Do you always use a voltage regulator on a switch mode power supply? I thought the voltage out of them was suitable without reg.

I've been trying to improve the sensitivity of mic as I need it to operate 3 meters away. Tried using the variable resistor but no use.
As the op amp is being run as a comparator should the positive and negative inputs be nearly the same?
Then as the mic picks up a signal the difference between the two inputs changes causing an output signal.
Basically by keeping both inputs at the same voltage level will that increase the sensitivity of mic?
Replaced R1 with a 13K and that has kept the neg input the same as positive input, both at 1.85v, however the mics only good at a few feet.
Have a new mic in circuit as other got damaged from having to resolder a broken wire. They look identical but this new one is just not as sensitive at distance, that's why I've been playing around with R values trying to set it up right.
The variable resistor is working but makes no difference, just thought I'd mention.
Understand there maybe inconsistency problems with the clapper as previously discussed, but I really need to get it working at 3 meters other wise it ain't much use, frustrating as it did work at 3m before battery died and mic replaced.
Appreciate reply if you can make any sense of post thanks.
 

Parkera

Joined May 3, 2016
106
Hi Copey84 - The LM358 and CD4017 will work at 5V, but there isn't much head room or dynamic range with the LM358 at that voltage. You can expect that it's output will swing from approximately 0.5V to 3.5V. The 4017 will have an upper threshold level of somewhere around 3.3 volts. You can make it work, but you really have to know the exact specifications and signal levels to pull it off. Things get much easier with higher supply voltages; 12-15V is probably ideal.

I would recommend a voltage regulator regardless mainly because your threshold level is derived directly from your supply voltage - you don't want that drifting around or having a lot of noise on it. Depending on the circuit and layout, sometimes you can get by with just supply bypass capacitors.

When an opamp is being used as a comparator the inputs are different from each other. One is always the threshold level, and the other is always the signal level. When it is used in the negative feedback (linear) configuration a virtual ground created which forces the two inputs to be the same as long as it is operated in the linear region. If you saturate the output stage then the inputs will no longer track each other.

The variable resistor will only have a tiny effect on the threshold voltage (The change is the same as changing the 470K to a 471K). If the value was a larger percentage of the total divider resistance its effect would be greater. To be useful I would say it should be at least 100K and the 470K changed to a 430K. With where you are at now, this is just FYI.

It sounds like your new mic has less sensitivity than your old "junk box" mic. And yea, less than 3 meters (10') is kind of pointless. Since you don't know what it's sensitivity was you can't match a new one to it. You just have to work with what you have now. In your circuit before the mic gave out, the audio sensitivity was dictated by the sensitivity of the microphone. The threshold was dictated by the LM358 stage and the 4017 was essentially used as a flip-flop and FET driver.

If you accept the microphone for what it is, you can use the LM358 in the linear configuration with gain to set your audio sensitivity. CMOS ICs have natural switching thresholds which you can take advantage of and they scale to the supply voltage. Those thresholds are approximately 1/3 and 2/3 of Vcc. There is some temperature sensitivity to the thresholds, but not that bad.

As a suggestion, I would bias the microphone as the manufacture recommends. That will probably give you the greatest microphone sensitivity (output signal). Configure the LM358 as an AC-coupled inverting amplifier where the input resistor is approximately 10X the value of the microphone bias resistor. A 0.1uF coupling capacitor should give you a good low-frequency response where most of the acoustic power of the "clap" exists. Adjust the feedback resistor (gain) for the best operation in a quiet room. As a final refinement you can try bypassing the feedback resistor with a capacitor. Its purpose will be to limit the bandwidth, thus extraneous noise, which should give you a bit more consistency of overall operation. I'm going to guess the upper frequency response should be perhaps 1 kHz. Select a capacitor value so that 1/(2 * pi * RC) = 1kz, where R is the feedback resistor value. Experiment with the capacitor value to give you the best results.

Since you are really working with a new circuit now, I would re-consider my earlier suggestion of a transimpedance amplifier circuit using a CMOS opamp.
 

Thread Starter

Copey84

Joined Jul 27, 2015
198
Hi Parkera, I was able to improve the response of circuit yesterday by using original 22k resistor for mic and replacing the pot with a 200k value one. I also found that taking the 470k out of circuit seemed to help, is that ok, or should it remain?

The input voltage levels were below a volt with the negative slightly higher. Was thinking its best to keep them close so that a weaker signal could still trigger circuit, is that the case, or have I got it wrong?
Was able to adjust voltage level on positive input through the 200k pot, better range I think.

Having got circuit working better I tried it out again in room where I eventually plan to use it.
It kind of works at at a distance of 3m but still a bit inconsistent. Main problem was trying to set it up so that door opening and closing doesn't false trigger. Ended up having to run mic remote from breadboard away from door to prevent false trigger. Mic is now only 1.5 m from me and although door doesn't effect it, it is still inconsistent, which is surprising as it's closer to me.

If I amplify mic as suggested will it improve consistency of trigger? And will a softer clap noise trigger the circuit instead of having to clap hard to get a signal?
Will probably set it up anyway and see if it makes any difference.
 

Parkera

Joined May 3, 2016
106
Hi Copey84 - To quickly answer your questions - by taking the 470K out and replacing it with a 200K you have changed your threshold level from a nearly fixed 2.28 volts (the 1k pot is a negligible portion of a 1M divider) to having a 0 to 1.31 volt adjustment range (assuming a 5V supply). Generally, you can do anything you want to the divider circuit without hurting anything, but the performance will vary. The fact that the circuit seems to be working better suggests the threshold is not optimized. The door will be a problem that will never completely go away. We will deal with that later.

I'm beginning to suspect that you don't FULLY understand the purpose of the comparator or the purpose of the microphone bias resistor. It also sounds like you do have an oscilloscope and I assume a multimeter that you can use. Can you generate sine waves through a speaker, such as from a computer? That will make the development of this circuit MUCH easier. It is time to start at the beginning and take it one step at a time to optimize the circuit.

For right now we will solidify the power supply voltage and the microphone/bias resistor. NOTHING ELSE MATTERS AT THIS TIME. These will become "constants" in the circuit. In fact, disconnect everything else.

#1 - Chose a microphone that you have at least a recommended bias current spec for or a recommended bias resistor and supply voltage. List all available microphone specifications in your next posting.

#2 - Pick a supply voltage to work with. This will primarily be dictated by the requirements of your latching relay. If it is a 9V relay - use a 9V supply voltage. If it is a 5V relay - use that voltage. Secondarily, the remaining electronics will factor into selecting a supply voltage. CMOS logic chips and an LM358 can work well from 5V to 15V. Generally working around 12V in the analog world makes life a bit easier but is not a show stopper by any means. We will stay away from other "exoctic" components that work only on 5V or less.

#3 - Obtain sine wave generator software.
 

Thread Starter

Copey84

Joined Jul 27, 2015
198
Hello, I've made some changes to circuit by amplifying the signal from mic first, then sending it to comparator, then to 4017, as suggested.
It does appear to be working much better, not having to clap as loud to get response. Going to set it up in room and see how it goes for few days, hope there isn't too many false triggers.
I've included new wiring diagram, can anyone see any problems or changes that might be needed?
Put in a 1M positive feedback resistor, not sure about value though. Also are the protection diodes ok in parallel with the relay coils or should they be between drain and source of 2N7000s?
As always appreciate replys.1497356484073-47922640.jpg
 
Last edited:
Top