Comparator output needs a kick start and then quickly shortens to zero. - Page 1
Forum section: electronics / projects

author: PansLab
author_rank: Newbie
timestamp: July 12, 2020, 10:41:55 pm
content: Folks,I need some help please. I'm trying to use an arduino to measure a frequency. I'm using an op am module and sending that to an LM324 comparator. I have two behaviors I don't understand.1) with a varying signal, that does go above and below the reference voltage, on the non-inverting input the output stays low, until I short the inverting pin to ground. Then the comparator starts working as expected, sort of. Read on.2) The comparator works as expected for only a few seconds. The input signal does not change but the time interval of the comparator output shortens until it reaches zero and then stays that way.The schematic is attached. I've also taken screen shots of the mysterious shortening output length. Their chronology is indicated by the title.This is single supply, all on breadboards with Dupont wires.

author: StillTrying
author_rank: Super Contributor
timestamp: July 12, 2020, 11:52:36 pm
content: The -IN of the LM324 needs a bias voltagesimilarto what's on the +IN, probably with a few 10s of mV difference between the -IN and +IN.

author: WattsThat
author_rank: Frequent Contributor
timestamp: July 13, 2020, 12:24:15 am
content: StillTrying told you how to fix it but if you're trying to understand the behavior...Practically speaking, no current flows into or out of an op amps input pins. You can think of an input as a very, very high resistance. So, there is no load on the output side of C1 and the voltage there starts at zero and as the output of ‘386 goes up, the right side of that cap just keeps going up with nothing to pull the voltage down. Putting a bias voltage on the input pin provides the necessary load so the output of the cap will follow the input voltage.Dave has an excellent tutorial on op amps on YouTube:

author: Ian.M
author_rank: Super Contributor
timestamp: July 13, 2020, 01:55:46 am
content: WattsThat's explanation isn't entirely accurate - its nothing to do with the LM386's DC output level which is blocked by two capacitors. Theoretically no current flows into or out of an ideal OPAMP's input pins, but in practice BJT OPAMPs like the LM324 need a very small but significant input bias current. JFET or MOSFET input OPAMPs do have very much higher impedance inputs, closer to the ideal, but even so they still need a DC path from each input pin to signal ground, so their absolutely minuscule bias current doesn't cause an offset voltage to slowly build up eventually railing their output. OTOH, I also recommend Dave's OPAMP video.TheLM324OPAMP*has PNP Darlington inputs to a long tailed pair. Both inputs *MUST* have a DC path to ground and need about45nA45uAbias current. That means that across a 10K input resistor the input bias current will make the input pin end about0.45mV0.45Vmore positive. Therefore if you don't want to run intosignificanttrouble with input offset voltages you've got to keep the DC resistance as seen by the +in and -in pins well matchedif you are using high value resistors round the OPAMPMomentarily grounding the comparator OPAMP's -in made it work until its45nA45uAbias current had charged up the input capacitor enough for the troughs of the signal on the -in pin to no longer go below the reference voltage on the +in pin.I suggest duplicating the R3, R1 potential divider to bias its -in with matched input resistance. You may also need an 820K feedback resistor from Out to +in to give it just over 1% hysteresis to stop it 'chattering' on noise if there's no input.Also its crazy using a LM386 audio output amplifier as a preamp before your OPAMP comparator. You've got three spare OPAMPs in the LM324 - use two of them to give you x200 gain. Configure them each as inverting amplifiers, and split the gain fairly evenly between them. To make them easy to bias use the remaining OPAMP as a rail splitter. As the LM324 wont drive closer than 1.5V to its Vcc rail you don't want to split the 5V supply symmetrically, as you want an equal swing above and below your bias rail. A potential divider with 18K lower resistor and 33K upper resistor for about 1.75V, decoupled with 100nF to ground, with the OPAMP configured as a unit gain buffer will be ideal. Use the bias rail as signal ground for the amplifiers.* Its output will transition a lot slower than a real comparator - it can only slew at about 0.5 V/us, and its push-pull output high level will only be about 3.5V with 5V Vcc so it will crap out on you if you try to use it as a comparator for signals much over 70KHz, as the output just doesn't have enough time to go high. Compare that with a real comparator like aLM393, which can slew between valid TTL levels in only 300ns, so should be good for over 380KHz on small signals and well over 1MHz with enough input drive.Edit: Thanks for the correction to my three orders of magnitude screwup (see strikeout and italics in my second and third paragraphs above) - I need to clean my glasses and possibly drink less Pimms!

author: PansLab
author_rank: Newbie
timestamp: July 13, 2020, 02:58:55 am
content: StillTrying, thank you. Your clue about biasing both inputs was the key. I took and replicated the resistors on both inputs and it started working reliably.Ian, thank you for clarifying. Plus, you're correct that it is crazy to use the 386 as a pre-amp. but here is my cop out. It's been *decades* since I've messed with discreet circuits like this. I'm so out of practice that I'm struggling to get an op amp to work as a comparator and interface it to a pre-amp output. I knew I'd be in way over my head trying to get a pre-amp working, and that's if I have all the right parts. I have a 5 pack of those audio amp circuits and I know they work. so I did the lazy thing and slapped one of those in.Thank you everyone.

author: Ian.M
author_rank: Super Contributor
timestamp: July 13, 2020, 03:36:53 am
content: The Atmega328P on your Nano has a built-in analog comparator. Bias its inputs correctly and you could probably*ditch the OPAMP!* Assuming you can route its output to whatever peripheral you are using to measure the frequency.

author: PansLab
author_rank: Newbie
timestamp: July 13, 2020, 12:07:27 pm
content: Ian, I saw that technique and considered it. But the output of the device is really low ~ -300mv to 300mv. Everything I've seen so far says an amp is needed and routing that to a comparator make for concise frequency measurements using interrupts.

author: Ian.M
author_rank: Super Contributor
timestamp: July 13, 2020, 05:07:07 pm
content: Sorry, this is long, but hopefully worth it to you.600mV pk-pk is really generous, lots of signal. I've been playing around with a sim of the x200 gain, all in the quad OPAMP circuit I discussed a couple of posts back on the assumption the input signal would only be +/-5mV, and it was looking pretty good.You don't want more than about 3V pk-pk after any amplification,(in general, assuming 5V supply), to leave enough headroom to avoid clipping all the way up to the comparator. If one is going to clip the signal, better to do so deliberately in a carefully considered fashion rather than by overdriving an amplifier that wasn't expected to clip.For signal level comparison at the comparator:* The LM393 comparator I mentioned and linked above is specified to work with as little as 5mV of overdrive (i.e. amount past the threshold) on a 100mV step signal, and still deliver a typ. 1.3us response time, so working with a +/-50mV signal is well within its capabilities.* The ATmega328P on chip comparator has the following specs, (at Vcc=5V, Vin=Vcc/2 unless otherwise stated):VACIOAnalog Comparator Input Offset Voltage typ. <10mV max. 40mVIACLKAnalog Comparator Input Leakage Currentmin. -50nA max. 50nAtACIDAnalog Comparator Propagation Delayat Vcc=2.7V typ. 750ns, at Vcc = 4.0V typ. 500 nsso is able to comfortably handle a +/-300mV input signal. Assuming its a sinewave, and neglecting the propagation delay, the max. 40mV input offset would only give a phase error of 7.7 deg, and the expected 10mV or less, under 2 deg.It does sound like your preamp lineup is working more by luck than good judgement. A gain of five would give you 3V pk-pk from your +/-300mV input signal, which would be trivial to achieve in a single OPAMP with AC coupled input, then straight into the MCU's on-chip comparator (or an external one). That should work reasonably well even if the input signal drops by an order of magnitude.However I think we are still putting the cart ahead of the horse:What does your input signal actually come from and what does it represent? (in case there's an obviously better way to skin the cat!)How well characterised is your input signal? i.e. What's its:Min.and Max. frequency?Min. and Max. amplitude?Waveform?Does it have high or low frequency noise that needs to be filtered out? (if so, what's the expected noise level?)How are you measuring the frequency? e.g. counting rising edges within a time interval,ortiming one period (e.g. rising edge to rising edge), then computing the reciprocal toget the frequency.Are you doing it purely in software (apart from maybe an edge triggered interrupt), or do you want to use a hardware counter (and possibly input capture events) to offload most of the work from the CPU?Lastly, what potentially useful parts do you actually have on hand? e.g. single, dual or quad OPAMPs and/or real comparators suitable for 5V single supply operation. Resistor selection: E24 series, E12 series or worse. Capacitors are generally less critical except in timing or resonant circuits, and anything within an order of magnitude will often do.