I am driving a 9V relay from the output of a comparator (LDR sensor) and the relay gets damaged due to rapidly changing voltage from the comparator when light on the LDR is small. The relay starts buzzing then damages. I put a freewheeling diode in parallel with the relay coil, but that didn't stop the buzzing. The relay is to switch 240V AC. How can I achieve smooth switching and prevent the buzzing?


simulate this circuit – Schematic created using CircuitLab

  • 1
    \$\begingroup\$ Um, using a relay coil with a 100Ω series resistor? Are we quite sure about that? \$\endgroup\$ Jun 10 '21 at 10:18
  • \$\begingroup\$ I used the 100ohm resistor so that i don't limit the current too much which may lower the voltage across the relay \$\endgroup\$
    – p_karis
    Jun 10 '21 at 10:48
  • \$\begingroup\$ well, um. You want that relay to turn on reliably, right, and you've got a large capacitor limiting current through the relay (and a series resistor is practically never a good idea here). Piece together this puzzle! \$\endgroup\$ Jun 10 '21 at 10:50
  • \$\begingroup\$ (this is probably not the reason for your problems, as these are the inherent ambiguities of your LDR amplification, see the answers you've already gotten, it's just that this whole thing seems ill-advised) \$\endgroup\$ Jun 10 '21 at 10:51
  • 1
    \$\begingroup\$ do i use a comparator opamp or tweak the lm358 to provide hystresis? \$\endgroup\$
    – p_karis
    Jun 10 '21 at 11:05

Adding hysteresis is easy. Add a resistor between +input and ouput of LM358. Resistor will be high (some 10k or higher) for a little hysteresis. Must simulate to confirm value. I use microcap12 (free) from Spectrum Software. Ok. Made simulation. 10k -> hysteresis of 3 V approximately. 100k -> 700 mV hysteresis. If you want, use a variable resistor to help fine.

I forget ... IMPORTANT POINT : Always decouple your OPamp with a 100 nF or 1 uF capacitor as near as possible from supply pins +Vsupply to Ground (or -Vsupply) !!! Very important ! The simulators does not need such a capacitor ... Also include a 100 Ohm resistor between +12V and +Vsupply of OPamp.

The edit (left) is part of the original edit for explanation. (Right figure) The curve in blue corresponds to a simple output of the assembly (in your case, it will be the voltage produced by the LDR on the terminal - of the operational amplifier (OPamp LM358).

The reasoning is generally based on the voltage of 6V (12V / 2, as if the amplifier were supplied with + 6V / -6V). The error voltage is epsilon = (U-) - (U+), inputs of LM358.

We see that when the sinusoidal voltage increases ( epsilon> 0), nothing happens, the output of LM358 is at the low level (here ~0V), epsilon voltage (> 0) * negative gain -> negative output -> 0V, low level output). At this stage, the voltage on the + terminal is about 4V theorytical (12V / 3 imposed by the 3 resistors, divider bridge) and lower than the sinusoidal voltage. Epsilon is positive. Op amp has a negative G gain therefore (epsilon+ * - = -) -> the output remains at 0V ("negative" side).

It will remain so as long as the sine voltage does not drop below the switch point at around + 4V (~ 4.087 V due to limitations of outputs voltages). As soon as it goes below, the output switches to ~ +12V because epsilon becomes negative. The operational amplifier has a negative gain, so (epsilon- * - = +) the output therefore goes to 12V (positive side). The voltage on the + input then becomes 8V theoritical (~ 7.185V). It will remain so until the input voltage rises above ... And the cycle will start again ...

I hope I did not make a mistake in transcribing my reasoning. (Google translate). Here is a schematic and results, R3 is the resistor I have added ... enter image description here

Do you see the 2 points marking hysteresis ? Hysteresis is here 3.098 V. See low in the graph, delta value of Vg.

  • \$\begingroup\$ pls explain futher. not understanding you. btw i also use microcap 12 \$\endgroup\$
    – p_karis
    Jun 10 '21 at 10:41
  • \$\begingroup\$ hello. i tried placing a positive feedback resistor (10k) to the non inverting input but that di d not stop the buzzing. pls assist \$\endgroup\$
    – p_karis
    Jun 10 '21 at 18:48
  • \$\begingroup\$ This is theoritical. I don't simulated with a LDR ... Can you give me a picture with points views by scope ? \$\endgroup\$
    – user288518
    Jun 14 '21 at 14:30
  • \$\begingroup\$ The resistors around the LDR must be carefully choosed. R4 and R7 may be omitted, I think. \$\endgroup\$
    – user288518
    Jun 14 '21 at 14:51
  • \$\begingroup\$ Try with a pot ( 10k) between +12V and gnd, cursor with serial resistor 1k .. 10k to input- to test. \$\endgroup\$
    – user288518
    Jun 14 '21 at 14:56

There's two causes that are kind of related.

The LM358 is not a comparator, but an op-amp. Op-amps can be quite poor comparators, and when inputs are very close to being equal, the output can be in a voltage that is half-on, instead of being fully high or fully low.

There is also no hysteresis built into the circuit to have definite thresholds when to switch to fully on and off levels to prevent the issues encountered when op-amp is being used as comparator.

  • \$\begingroup\$ how do you differentiate between a comparator opamp and an ordinary opamp \$\endgroup\$
    – p_karis
    Jun 10 '21 at 10:46
  • \$\begingroup\$ A LM339 is a comparator, LM358 is an opamp. It says it on the first page of the datasheet. Have a look at the examples: ti.com/lit/ds/snosbj3e/snosbj3e.pdf?ts=1623323156515 \$\endgroup\$
    – Kartman
    Jun 10 '21 at 11:07
  • \$\begingroup\$ okay. let me look at it \$\endgroup\$
    – p_karis
    Jun 10 '21 at 11:09
  • 2
    \$\begingroup\$ The datasheet says if a device is an op-amp or a comparator. \$\endgroup\$
    – Justme
    Jun 10 '21 at 11:42
  • \$\begingroup\$ @p_karis - Basically a comparator is an op amp. However, comparators usually have two specific features. First, their inputs are designed so that they recover very quickly from large voltage differences between inputs. This is not true for most op amps, which can require milliseconds to recover low-level accuracy. Second, the output stage is very often a single NPN or NMOS MOSFET with the emitter/source able to be tied to ground. This allows various useful connections, such as wired-OR, which are not as easy with regular op amps. \$\endgroup\$ Jun 14 '21 at 18:37

At a guess, part of your problem is your construction technique. Start by changing your schematic a bit


simulate this circuit – Schematic created using CircuitLab

Now, about the changes.

First, make the ground connection between the supply and source of the FET your primary connection. Every other ground should be outside this connection and not to the supply side.

Second, the ground connection of C1 should also connect as close as possible to the FET source.

Third, C2 should be made without jumpers. Connect it as close as humanly possible to the IC pins. As in, plug the capacitor leads directly into the socket next to the IC leads.

I suspect that you're using a breadboard with longish jumpers to make connection. In the process, when the relay turns on, ground current raises the voltage at the op amp, which turns it off. Then the current drops, and the op amp turns on again. This is a very old issue, and in early audio transistor circuits it was called "motorboating" due to the characteristic noise it would produce at the speakers.

D2/C1 also help isolate the circuit from voltage changes caused by current changes. If the supply voltage drops when the relay activates, this will not cause a big change on the LDR bridge which provides the sensor voltages to the op amp.

You no longer need R4 to keep the FET gate at a proper voltage - R2 does that for free.

  • \$\begingroup\$ Good view. Always design power wires first as short as possible. Use a 2 layer pcb is important. One layer for ground as wide as possible. The connected wires of OPamp supply should be as nearest of possible to the pins of external supply. Same for the part of circuit with the transistor, minimize inductance. Not always simple to make such circuitry. Called the "star circuitry connection". Very usefull in RF assemblies. \$\endgroup\$
    – user288518
    Jun 15 '21 at 12:17
  • \$\begingroup\$ @Antonio51 - Star connection is most useful when connecting multiple chassis together. For a printed circuit board you get best performance using a ground plane. You can still get in trouble with large currents and small signals, so placement and visualization of current flow, along with differential techniques where appropriate, may be necessary. And depending on the details of current levels, you can still get in trouble with a star configuration if power ground and signal ground are tied together. \$\endgroup\$ Jun 17 '21 at 12:07
  • \$\begingroup\$ Ok for multiple chassis. But RF problems was somewhat different in old times. Do you remember when some was testing electronics with "Valves" ? Do you know that how some some special "intrumentation amplifiers" are wired ? Why sometimes gnd or screen is not wired at one end of a coax cable ? Good decoupling is only the way to get stable "functionability". Sorry for my poor english. \$\endgroup\$
    – user288518
    Jun 17 '21 at 13:58
  • \$\begingroup\$ Good decoupling is always a good idea, but not necessarily enough. Sometimes coax cables are not grounded at both ends because, if the two grounds are at different potentials, due to things like current flow and IR drop, you get a potentially large magnetic loop formed by the shield, which in turns radiates noise into the signal line. Grounding, particularly at high frequencies, can be something of a black art. \$\endgroup\$ Jun 19 '21 at 0:39
  • \$\begingroup\$ Ok. "Grounding, particularly at high frequencies, can be something of a black art." No, it is not black art. Ground is integral part of the system, when EM waves propagates. So, one must well know HOW. Not very easy, however. \$\endgroup\$
    – user288518
    Jun 19 '21 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.