Generating precise outputs from a comparator - Page 1
Forum section: electronics / beginners

author: guymo
author_rank: Regular Contributor
timestamp: July 04, 2018, 07:35:46 pm
content: Hello,I am trying to design an analog circuit to perform frequency addition for sawtooth waves. Given two sawtooths running 0 - 5V, if we sum the voltages, detect when the sum crosses 5V, and subtract 5V in that case, we get a new sawtooth wave whose frequency is the sum of the input wave frequencies.The obvious way to do this is to use a comparator and somehow clip or set the output voltages to be 0V and 5V. Obviously a certain amount of precision is desirable and ultimately I'd like to be able to chain a bunch of these stages which calls for more precision and a low parts count if possible.So: what's the best way to get precise voltage outputs from a comparator?Ideas I have had:- use an LM311 with a 5V reference for the pull-up; I'm not sure if the 0V output will be precise and there will be loading issues for the pull-up.- use a rail-to-rail op amp powered from 0V and 5V. This seems quite a good approach although there is some danger that the input waves go beyond the power supply rails of this component and perhaps give it trouble- run the comparator output through a CMOS IC (inverter for instance) powered from 0 and 5V- clip the outputs of a regular comparator with some sort of shunt reference -- but what? I've played with a TL431 and didn't get very good results- use a precision clipping circuit. Parts count is going up at this point...Any advice or ideas welcomed! I'm posting in Beginners because I basically don't know what I am doing...

author: D-Jack
author_rank: Contributor
timestamp: July 04, 2018, 11:16:24 pm
content: So the output should be: (wave1 + wave2) or (wave1+wave2)-5v?And can you use dual supply op-amp or must it be single supply?

author: guymo
author_rank: Regular Contributor
timestamp: July 05, 2018, 05:42:12 am
content: Quote from: D-Jack on July 04, 2018, 11:16:24 pmYes, overall output will be wave1+wave2 if that does not exceed 5V, and wave1+wave2-5V otherwise.The system as a whole will be running on good old fashioned +/-15V dual supplies.

author: Giaime
author_rank: Regular Contributor
timestamp: July 05, 2018, 06:45:07 am
content: As far as output accuracy goes, I have obtained best results with CMOS ICs, especially when run in parallel. Their low RDS(on) for the output stages help to reduce loading effects: I use them for precision PWM DACs.Of course they cannot be more accurate than the power supply rail they're being powered with.

author: Zero999
author_rank: Super Contributor
timestamp: July 05, 2018, 08:16:23 am
content: What issues did you have with the TL431?

author: D-Jack
author_rank: Contributor
timestamp: July 05, 2018, 10:15:12 am
content: Quote from: guymo on July 05, 2018, 05:42:12 amOk. You can achieve this with 4 op-amp (one as summing, one as inverting with unity gain, one as comparator and one as differential), but you will need a second 5V supply or some extra circuitry to generate 5V to the comparator.EDIT: sorry I missed the part where you said the two wave frequencies are added at the end. That will be much more complicated I'm afraid

author: guymo
author_rank: Regular Contributor
timestamp: July 05, 2018, 03:14:34 pm
content: Quote from: D-JackThe frequency addition is just the result of the transfer function I am after. If the two inputs are A and B, and the output isA + B (when 0 <= A+B < 5)A + B - 5 (when A + B >=5)then assuming A and B are 0-5V sawtooth waves, the output is a sawtooth whose frequency is the sum of those of A and B. So all I need to do is achieve that transfer function.I'd be very interested to see the four op amp circuit you have in mind. My sketch uses only two, but I am not convinced that I can get it to be precise. My two op amp circuit is sketched in the attachment. The first is a comparator which magically produces 0 and 5V outputs -- that's the purpose of this question -- and the second constructs the result. I guess you are taking more care in places to tidy up the voltages or buffer carefully or... As you can see from my sketch I don't mind having to come up with voltage references if necessary.Giaime's idea could work in a similar circuit by interposing a CMOS stage after the first comparator, to tidy it up to 0 and 5v. Then I could just use "any old comparator". I might give this a try. A 40106 might have the advantage of reducing glitches at the transitions too.Both of these approaches will need a pretty accurate 5V supply for the CMOS or comparator stage. What's the best way to create one of those? A 7805 regulator might not be close enough. How about using a precision reference and buffering with an op amp -- could that power a few 40106s?

author: guymo
author_rank: Regular Contributor
timestamp: July 05, 2018, 03:17:01 pm
content: Quote from: Hero999 on July 05, 2018, 08:16:23 amI was using two of them, one for a 2.5V reference and one for the 5V clamp. The 5V clamp seemed to work ok though it was more like 5.1V; the 2.5V reference I couldn't get close to 2.5V, I think because the cathode voltage was 12V or so, so the reference strays from its nominal 2.5V. I don't really understand the data sheet! And I seemed to need to feed both of them quite a lot of current.

author: D-Jack
author_rank: Contributor
timestamp: July 05, 2018, 04:14:38 pm
content: Ok now I see,I attached an example of a circuit. There are ways to optimize and improve to best fit your application. To obtain a precise 5V reference I would suggest using a voltage reference with series topology, and if you don't want to have different supplies on op-amp (then you can use a single 4-channel opamp IC instead of multiple opamps) you can use a p-channel mosfet to act as pull-up at the input of the defference amplifier(U5).The idea is to: 1) add the signals, 2) compare them, if above 5V, sature high, if not sature low and then 3) subtract the added waveform with the output of the comparator

author: guymo
author_rank: Regular Contributor
timestamp: July 05, 2018, 07:25:38 pm
content: Thanks. That circuit will work, but I am not sure in what ways it improves on my one. I'm using non inverting stages which saves two op amps but may be less good in some ways -- are there reasons to avoid that?It seems that we're sort of reaching consensus on the right way to do the "precision output comparator" stage: either a rail to rail op amp powered from 0-5V or a MOS stage to condition the output.D-Jack, your design has an op amp powered from 0V and 5V receiving input from 0 to -5V. Is that ok? What devices would you recommend for that sort of maltreatment?

author: D-Jack
author_rank: Contributor
timestamp: July 05, 2018, 07:57:12 pm
content: Quote from: guymo on July 05, 2018, 07:25:38 pmIt's not textbook material that's for sure. You can do it since the op-amp will saturate to its lowest voltage which is ground and there will be no damage, but a better way would be to power the op-amp like the rest and place a mosfet on the output connected to 5V. I attached a picture to illustrateQuote from: guymo on July 05, 2018, 07:25:38 pmUsing a mosfet allows you to be flexible when choosing op-amps since the "precision" is handled by the mosfet.Quote from: guymo on July 05, 2018, 07:25:38 pmA transfer function for a non-invert amplification will add a 1 so the gain would be always (1 + x) and for many application that would be undesirable

author: guymo
author_rank: Regular Contributor
timestamp: July 05, 2018, 08:14:10 pm
content: YES! That FET stage looks like just the sort of output conditioning I was looking for. In some sense it's the discrete version of the idea from above of inserting a CMOS IC to condition the voltage, right? I will play around with this idea.Quote from: D-Jack on July 05, 2018, 07:57:12 pmSome data sheets warn against damage to the device if the input goes below the lower supply voltage -- is that overly cautious? I sort of imagined there were diodes inside which would go nuts if you bring the voltage too low, but I have basically no idea about this.Quote from: D-Jack on July 05, 2018, 07:57:12 pmIf that's the only issue you are aware of then my design is ok! The gains are definitely what I need. So the combination of that plus the FET conditioning stage could be the answer. Two op amps and a transistor -- not bad. Thanks!

author: rstofer
author_rank: Super Contributor
timestamp: July 05, 2018, 09:50:45 pm
content: Quote from: guymo on July 05, 2018, 03:14:34 pmYou want to add thefrequenciesand get a sawtooth? Like 1V @ 100 Hz + 3V @ 300 Hz gives a 4V 400 Hz sawtooth? I'll concede I haven't actually spent any time on the time domain view of this but I don't think it really results in a sawtooth. Even if they are the same frequency, if they are out of phase the result won't be a sawtooth. If the signals line up in phase and frequency, their voltages can be added to come up with a sawtooth.Sure, you can add voltages all day long but the result won't be a sawtooth without phase and frequency alignment.

author: romons
author_rank: Contributor
timestamp: July 05, 2018, 10:51:14 pm
content: rstofer, yes, this works. I spent a few minutes simulating it, and it really does work. You need to use an increasing sawtooth, ie, 0 up to 5 then immediately back to 0 for both of the sawtooths.The way to think about it is the sawtooths are a line that is restarted when it hits 5, repeating again and again, similar to modulo arithmetic. Adding two lines adds the slope modulo 5, and since the slope defines the frequency, adds the frequency.Very clever.

author: D-Jack
author_rank: Contributor
timestamp: July 06, 2018, 12:22:57 am
content: Quote from: guymo on July 05, 2018, 08:14:10 pmYes it is a discrete way to pull the input to a voltage with minimum dropout. You can achieve this with either pmos or nmos.Quote from: guymo on July 05, 2018, 08:14:10 pmWell that's the first thing but I didn't dive too deep into your circuit because I assumed you'd want the output to be equal in amplitude, that's why I set all the gains to 1. There may be some other constraints though and I would suggest you do some simulations. Upon doing one using your drawings I couldn't replicate the expected result but then again the circuit is incomplete and it may work with just two op-amps in the end.

author: guymo
author_rank: Regular Contributor
timestamp: July 06, 2018, 10:52:02 am
content: Quote from: romons on July 05, 2018, 10:51:14 pmExactly -- arithmetic mod 5 is the best way to convince yourself that it works, and it also doesn't matter about phase alignment, though of course the phase of the resulting wave does depend on the phases of the waves you start with.I think rstofer misunderstood what I meant by a 0-5V sawtooth. I mean a wave that starts at 0V and rises to 5V before resetting, while I think rstofer thought I meant a sawtooth of any amplitude between 0 and 5V, which of course won't work.

author: rstofer
author_rank: Super Contributor
timestamp: July 06, 2018, 01:25:55 pm
content: Quote from: guymo on July 06, 2018, 10:52:02 amI did indeed misunderstand. Old age...I ran an example on Excel and it didn't work but the example had the same misunderstanding.

author: Zero999
author_rank: Super Contributor
timestamp: July 06, 2018, 03:02:59 pm
content: Quote from: guymo on July 05, 2018, 03:17:01 pm5.1V isn't that bad. It would be on the upper band of the tolerance, for the standard grade TL431, so it's not unexpected, especially if you have 5% tolerance resistors and your meter is slightly out. Use the B grade TL431 and 0.5% resistors, if you want more precision.Another reason for the voltage being wrong is the current through the TL431 being too low. It should be at least 1mA, for it to work properly.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: July 06, 2018, 09:58:10 pm
content: This won't add the frequencies in the way you're probably expecting.A comparator is a nonlinear element. This is a special case of a more general truth: when waveforms are added together in a linear circuit, they obey superposition: it's just that, the result is the simple sum of the inputs. When the sum is passed through a nonlinear circuit, however, superposition is broken, and the result is not the simple sum, but contains product terms (which contain sum and difference frequencies, harmonics, and harmonics of the sums and differences, and so on).The simplest (textbook) case is a product of two sines (y = (sin wt)(sin vt)), or the square of the sum of two sines (y = (sin wt + sin vt)^2). In either case, we can use trig identities to simplify the result, and observe that the frequencies w and v add and subtract.A triangle wave is a bunch of sine waves stacked up in a particular manner. That is, we can take the Fourier series of the triangle wave, and look at the superposition of sines (harmonics). When this wave is sent through a nonlinear circuit (the comparator), the harmonics are mixed in this way, and a different result is obtained (namely, a square wave of some duty cycle; the harmonics of which, generally go as 1/n when n = odd and zero otherwise; but a variable duty cycle promotes even harmonics and puts a sinc(f) envelope on them corresponding to pulse width; okay so this is technically correct, but for simplicity's sake, suffice it to say, it's just some kind of square wave).The sum of two (orthogonal*) triangle waves, will indeed cross zero (or for two biased (0-5V) triangle waves, cross 5V -- it's just an arbitrary threshold, might as well center things around zero for argument's sake here), (sum of frequencies) times per second. So the comparator output will be a square wave with those zero crossings.But notice what the one triangle is doing to the other. Think about one triangle varying very slowly. It's varying the threshold the other one is being compared at: it's making PWM. You will indeed get one extra pair of edges, every cycle of the slow wave; but the edges aren't evenly distributed. The one frequency remains, as does the slow frequency (we've made a PWM modulator -- it could just as well be used for, say, audio reproduction!), and the sum and difference, and their harmonics. It's a very messy process, if all you wanted was the sum and difference!*Meaning, the frequencies don't match up in some relevant way. They aren't harmonically related, say. Or, at least, the ratio is an odd enough number that we can't measure it very easily.Tim

author: guymo
author_rank: Regular Contributor
timestamp: July 07, 2018, 11:27:26 pm
content: Quote from: T3sl4co1l on July 06, 2018, 09:58:10 pmI don't see why not. I do appreciate your detailed post but I still think that this circuit performs frequency addition for fixed amplitude sawtooth waves.To (perhaps) simplify, consider a sawtooth wave that rises from 0V to 1V with frequency f, and suppose that the voltage of this wave at time t=0 is k.Then the voltage at time t is the fractional part of ft+k. (I'm doing "arithmetic mod 1" i.e. taking the fractional part).My "frequency adder" circuit would take two such waves, sum their voltages, and subtract 1V if the result goes over this threshold. That is to say, it just returns the fractional part of the sum.If the input waves are given by the fractional parts of f_1 t + k_1 and f_2 t + k_2 then the result is the fractional part off_1 t + k_1 + f_2 t + k_2 = (f_1 + f_2) t + (k_1 + k_2)So it is a sawtooth of frequency f_1 + f_2 as claimed.I do not make any claims for what this circuit would produce if fed with triangles, sines or other arbitrary waveforms. It's a sawtooth frequency adder. I am surprised that it seems surprising to people!