Find highest value of 4 inputs without microcontroller? - Page 1
Forum section: electronics / projects

author: petersanch
author_rank: Regular Contributor
timestamp: November 15, 2018, 05:31:51 am
content: Hi All,I have 4 analog inputs and wish to find which one out of the 4 has the highest voltage. A TTL output is preferred. Feels as if there should be an IC that does this. Are there clever ways of doing this without a micro or A2D?Cheers!

author: mark03
author_rank: Frequent Contributor
timestamp: November 15, 2018, 06:10:21 am
content: Six comparators followed by some digital logic?(One comparator for every signal pair: AB, AC, AD, BC, BD, CD. Then work out the boolean logic for your desired output, whether 1-of-4 or two-bit binary encoding.)

author: helius
author_rank: Super Contributor
timestamp: November 15, 2018, 06:13:27 am
content: So with 4 inputs, there are 6 relevant comparisons: A vs B, C vs D, A vs C, B vs D, A vs D, B vs C.out(A) = (A>B)∧(A>D)∧(A>C)out(B) = (A<B)∧(B>D)∧(B>C)out(C) = (C>D)∧(B<C)∧(A<C)out(D) = (C<D)∧(B<D)∧(A<D)You can factor the comparisons into sums of terms, too.How about two LM339 quad comparators, with the outputs ran into a 74HCT132 quad 4-input NAND?

author: unitedatoms
author_rank: Frequent Contributor
timestamp: November 15, 2018, 10:45:10 pm
content: For positive only voltages, connect each input to anodes of leds of 4 optocouplers. Tie all led cathodes to single grounded resistor load. The ttl outputs of optos will provide 4 bit code. Where possible most expected values are 0001, 0010, 0100 and 1000. Other values must be decided upon too, use code into any combinatorial manner, for example 2 x 16 inputs demuxes with hardwired inputs if 2 bit output is required.

author: bson
author_rank: Unknown
timestamp: November 16, 2018, 02:10:13 am
content: I second the optocoupler idea. The LEDs may differ ever so slightly in forward voltage, so that will be your main source of inaccuracy.Ra//Rb biases the LEDs, and if you want to bias at -Vf = -2.5V then Vss < GND - Vf < -2.5V. If you don't care about biasing just remove Ra and replace Vss with ground.

author: NiHaoMike
author_rank: Super Contributor
timestamp: November 16, 2018, 02:43:44 am
content: Instead of LEDs, use NPN transistors to make a 4 input version of a long tailed pair.

author: JVR
author_rank: Regular Contributor
timestamp: November 16, 2018, 10:27:49 am
content: Quote from: bson on November 16, 2018, 02:10:13 amI might be missing this, due to my mind being filled with an extraordinary lust for beer, but how is this better than using comparators? This induces large offsets, and even more uncertainty due to the low gain of an optocoupler.

author: bson
author_rank: Unknown
timestamp: November 17, 2018, 01:45:34 am
content: Quote from: JVR on November 16, 2018, 10:27:49 amPost an actual circuit and we'll see.

author: german77
author_rank: Regular Contributor
timestamp: November 17, 2018, 02:45:53 am
content: I will go with comparators any time. But I like doing things the hard way. This idea is similar to the optocoupler idea but with transistors. From the emulation I can expect a minimum of 0.15V difference to say for sure which one is the maximum.

author: fsr
author_rank: Regular Contributor
timestamp: November 17, 2018, 03:32:10 am
content: The optocoupler idea is quite clever!But without a uC, how will you read the result, and how will you actually select the analog line, etc?From a cost perspective there could be some cheap, low pin-count uCs with several ADCs out there.But it all depends on how will you use the result and the signals.

author: german77
author_rank: Regular Contributor
timestamp: November 17, 2018, 04:02:33 am
content: Quote from: fsr on November 17, 2018, 03:32:10 amThe optocoupler may sound clever but it's actually horrible considering that you need 1.2V or something like that, of difference to know for sure which one is the highest. I'm sure he does use a uC to read the result but he doesn't want to waste ADC pins or resources to do this task.

author: Wolfgang
author_rank: Super Contributor
timestamp: November 17, 2018, 09:40:12 am
content: Quote from: german77 on November 17, 2018, 02:45:53 amYou could make this idea more accurate by using transistor array ICs, one for NPN and one for PNP. Within an array, Vbe does not differ much.

author: Kleinstein
author_rank: Super Contributor
timestamp: November 17, 2018, 10:06:00 am
content: For the transistor circuit it would help a lot if the emitter side resistor to the negative supply would be replaced with a constant current source of some type. This would make the level where the circuit starts to see a difference more independent of the absolute voltage level.Adjusting the current to the right level (around 1 mA for the shown circuit) can also reduce the turn over range, from a case where no output might be marked to the case where 2 about equal outputs would be marked.

author: DC1MC
author_rank: Super Contributor
timestamp: November 17, 2018, 10:43:52 am
content: You can draw some inspiration from the patent here:https://patents.google.com/patent/US6191623B1/enDC1MC

author: spec
author_rank: Frequent Contributor
timestamp: November 17, 2018, 11:11:26 am
content: The requirement sounds quite simple at first, but there is more to it than meets the eyeThe four LED circuit is so clever.It does have a lack of precision though and there is a danger of exceeding the LED reverse voltage. Of course, that depends on the voltage of the input signals.Attached is a precision version of the four LED circuit. In addition to the the highest voltage LED illuminating while the others are off, the three off outputs will be at logic 0. But the highest output will be at whatever the highest voltage input is.I haven't worked out a simple way to produce a solid logic 1

author: spec
author_rank: Frequent Contributor
timestamp: November 18, 2018, 12:04:02 am
content: Below is the best I can do at the moment.All channel outputs are at 0V, except for the highest voltage channel output which will be at 5V, so suitable for driving all 5V logic families.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: November 18, 2018, 12:42:17 am
content: Should be able to do it in slightly fewer, due to this being a sorting problem (of sorts) and sorting problems being guaranteed N log N worst case complexity.Not that this helps much at small N, and indeed, if N = 4, then 4 lg(4) = 4*2 = 8, so the 6-comparator solution given earlier can't be too far from there.I suspect it's possible to save some comparators, in exchange for more logic (e.g., doing pairwise comparisons on ordered sets; using analog muxes to compare pairs in sequence, latch the state and resolve the sampled result; etc.), but this probably won't save many transistors or much space.The analog method is to use a max(a, b, c, d) circuit and monitor the current through each diode; only one will be active at a time (outside of a transition range, which we can make arbitrarily narrow with op-amps).To improve on spec's proposal: instead of a pull-down resistor, first just use a current sink instead of a pull-down resistor. This keeps the offset, transition band, and speed more consistent over the operating range, and lets us work with it more easily, at least for now.Either:- In place of the diodes, use transistors to detect when a channel is active. That is, the pull-down current is communicated through the active transistor's collector, to a load resistor, which probably needs a level shift to get back to a standard logic level (say if the A through D inputs are 0-5V, and the output is 5V CMOS logic level). So we'd need four NPNs and four PNPs, plus resistors (the PNPs can be the "prebiased" or "digital" kind, saving on components).- When a channel is "losing", its op-amp is saturated to -V. In an active rectifier circuit, we often use a diode from -in to out, to reduce integrator windup (i.e., holding the output at (-in) - Vf rather than allowing it to saturate to the full -V, saving slewing time when that signal turns back on). We can capture this with crude comparators (probably just transistors or diodes), and resolve state with logic if necessary. (This is probably not as nice of a method, so I haven't fleshed it out as well.)Similar logic applies to this circuit as well, though being just a two-channel min(a, b) implementation for a particular situation (i.e., wrapping around an error amplifier).https://www.seventransistorlabs.com/Images/Limiter2.pngBy adding a logic output to the diff pair, a comparison output is available; this could be scaled to any number of limit inputs, if such were needed. This works very nicely for a power supply where you want V/I limiting and indication. The 4-variable version works just the same, but with more process variables.Tim

author: SiliconWizard
author_rank: Super Contributor
timestamp: November 18, 2018, 12:48:11 am
content: Quote from: spec on November 18, 2018, 12:04:02 amThat's clever.You can probably set your reference voltage a bit higher than 50mV to get more margin, as the highest voltage detection at the opamp's outputs should be a diode drop higher than the input voltage, thus significantly higher than 50mV even if input is very close to 0V, at least for 1N4148 diodes. Using rail-to-rail opamps will also improve the operating range, as well as using schottky diodes.

author: unitedatoms
author_rank: Frequent Contributor
timestamp: November 18, 2018, 01:37:30 am
content: @Spec: great!I'd rather rewire input opamps to be followers and leave it for diodes to decide on outcome of voltage drop across individual diodes. In theory only one diode will win and will have highest voltage drop across itself.After that works, the output 4 opamps/comparators should only compare cathode vs anode. So the divider can be removed and negative inputs of comparators should be connected only to single resistor.

author: spec
author_rank: Frequent Contributor
timestamp: November 18, 2018, 05:45:54 am
content: Quote from: SiliconWizard on November 18, 2018, 12:48:11 amThank you SWI like your suggestions to enhance the design- well though out.Of course, a bit of decoupling would help too.

author: spec
author_rank: Frequent Contributor
timestamp: November 18, 2018, 05:52:57 am
content: Quote from: unitedatoms on November 18, 2018, 01:37:30 amThank you too uAHmm, some interesting thoughts about the architecture. Could you perhaps post an outline schematic of your approach to illustrate.

author: spec
author_rank: Frequent Contributor
timestamp: November 18, 2018, 05:59:03 am
content: Quote from: T3sl4co1l on November 18, 2018, 12:42:17 amA schematic can often replace a thousand words.

author: LaserSteve
author_rank: Super Contributor
timestamp: November 18, 2018, 07:16:15 am
content: My friend Jim modeled this attached circuit for me right before he passed on. My application was RGB to luminance for ILDA standard signals [0 to 5V] in laser show projectors with only green lasers. Enjoy..

author: T3sl4co1l
author_rank: Super Contributor
timestamp: November 18, 2018, 04:21:05 pm
content: Quote from: spec on November 18, 2018, 05:59:03 amTakes as long to write. And doesn't contain any design info!Tim

author: spec
author_rank: Frequent Contributor
timestamp: November 18, 2018, 04:53:22 pm
content: Quote from: T3sl4co1l on November 18, 2018, 04:21:05 pmAh I see your clever scheme- use transistors as diodes and difference amplifiers.The only thing is that the A#15 circuit uses: 2 x LM234 , 4 x diodes, 3 x resistorWhile the A#23 circuit uses: 1 xLM324, 9 transistors, 11 resistorsDon't you use a circuit capture package?If not, you do know that there are some pretty good free versions available on the web.I'm an EAGLE fanboy

author: T3sl4co1l
author_rank: Super Contributor
timestamp: November 18, 2018, 05:11:49 pm
content: Quote from: spec on November 18, 2018, 04:53:22 pmYeah, like I said, there's the other way, which is detecting which amps are saturated. (Did you even read my reply or just glaze over at it?)And like I said, the PNPs can be "prebiased" types, for 16 components total.QuoteI use Altium. No point in cranking it open just for a doodle.Tim

author: unitedatoms
author_rank: Frequent Contributor
timestamp: November 18, 2018, 05:39:01 pm
content: @Teslacoil:That schematic looks like the best. Will work for negative voltages too. It is possibly fine to have opamps feedbacks with total n2pathes = 16 feedbacks. I was not sure about AC stability. But may be it is fine and stable

author: spec
author_rank: Frequent Contributor
timestamp: November 18, 2018, 07:00:27 pm
content: Quote from: T3sl4co1l on November 18, 2018, 05:11:49 pmThe latter- far too longQuote from: T3sl4co1l on November 18, 2018, 05:11:49 pmYou should have.

author: spec
author_rank: Frequent Contributor
timestamp: November 18, 2018, 07:05:19 pm
content: Quote from: T3sl4co1l on November 18, 2018, 12:42:17 amBut you didn'tThat's why you should post a circuit rather than just say what you should be able to do.Your circuit does nothing more than my circuit and uses more components. It is also much more complex.It also does not operate down to the 0V rail, to what extent is not clear because you haven't put in values and it does not have an adjustable initial offset (50mV in my circuit).

author: SiliconWizard
author_rank: Super Contributor
timestamp: November 18, 2018, 07:59:39 pm
content: I think spec's approach is the simplest so far if using discrete components, and if using RR opamps and schottky diodes, the operating voltage range is pretty decent. The way the feedback loop is designed allows to get usable input voltages down to almost 0V and also very small detectable voltage differences, which wouldn't be the case with just diodes?As is, it won't work properly for negative input voltages though, even if the opamps are powered by a dual supply. At least one of the input voltages would have to be positive in order for it to work I think, otherwise all outputs would be down to the negative rail.Of course if it were to be implemented in silicon, using opamps in this way would be wasteful and full-fledged opamps would probably not be required. Using a few transistors instead would yield a much smaller area, although I'm still wondering about the effective usable voltage range for the 4 inputs.I've also thought about a clocked version of this, using switches, only one comparator and a peak detection block. Would be effective area-wise if dealing with a lot more inputs too, as it would scale up well. Drawback would be the latency to get the end comparison, but that may be negligible depending on the application. Implementing this with discrete parts would take significantly more parts too.

author: spec
author_rank: Frequent Contributor
timestamp: November 18, 2018, 08:40:13 pm
content: It's a tantalizing problem- you just feel that there ought to be a simpler way, and of course there is: a tiny MCU Sorry I mentioned that word

author: SiliconWizard
author_rank: Super Contributor
timestamp: November 18, 2018, 08:44:32 pm
content: Quote from: spec on November 18, 2018, 08:40:13 pmIn terms of part count, obviouslyAlthough, depending on your input voltage sources, you may need additional buffering anyway - so wouldn't be that clear then!Also, you'd be limited to the MCU's Vdd, whereas the opamp based design gives you a lot more freedom.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: November 19, 2018, 01:19:16 am
content: Quote from: spec on November 18, 2018, 07:05:19 pmWell, yeah, because I was discussing analysis there, not synthesis...An example circuit cannot prove an O(N log N) complexity, and even a very large number of variously sized examples can only hint at it. Nay, complexity is proven differently. (A proof which I don't need to provide, as search algorithms are well known theorems from CS, which the reader should look up if they are curious).QuoteI didn't say it did..? Indeed you gave an implementation of one of the possibilities I listed.QuoteThe resistors are left as an exercise to the reader.Nothing complicated, a fractional mA for the CCS is fine, and that sets the B-E resistor minimum value, and the pull-down value is in turn set by hFE (minimum) and speed or leakage (maximum).Input range is easily controlled for any type of op-amp you might use, also, so I'm not too worried about diode drops or whatever. There's also using a current mirror, and probably a variety of other things.An op-amp with output saturation detection would be cool -- easy to make from first principles, but devilishly hard once it's in IC form.Tim

author: technix
author_rank: Super Contributor
timestamp: November 19, 2018, 06:27:52 am
content: Here is an idea that does not use a crazy number of comparators and might work even if the input voltage has a wide range: 74HCT4052 + LM311 comparator + a small state machine in either a MCU or small CPLD. I am assuming VCC = 5V, VDDIO = 3.3V, GND = 0, VEE = -5V (can be generated using a 7660 from the 5V rail)Two 74HCT4052 is used to implement a 4x2 analog matrix switch, choosing two arbitrary signals from the four inputs. 74HCT4052 with 5V VCC and -5V VEE allows switching signals anywhere in that symmetric bipolar range, and is compatible with 3.3V LVCMOS logic levels. LM311 comparator can operate bipolar across VEE and VCC and has a output transistor that conveniently allows the implementation of a built-in VEE-VCC to GND-VDDIO level shifter. Now you have an AFE allowing choosing of any of the four inputs within -5V to 5V range to be compared, and operate the whole thing using 3.3V LVCMOS logic.In the logic side, you can use a low cost CPLD like XC2C32A to implement a state machine it use a low cost MCU like that Panduk job to implement a sort algorithm. If your CPLD needs clock, just implement an RC oscillator with a 74LVC1G14, or if your CPLD has Schmitt trigger inputs, the CPLD itself.

author: spec
author_rank: Frequent Contributor
timestamp: November 19, 2018, 10:58:16 am
content: I would love to see a schematic showing an actual implementation of your scheme with all components identified rather than just talking about it.By the way, the crazy amount of comparators amounts to 2 jelly bean chips.

author: technix
author_rank: Super Contributor
timestamp: November 19, 2018, 12:24:23 pm
content: Quote from: spec on November 19, 2018, 10:58:16 amHere is the circuitry. U1 and R1 is the comparator with level shifted output. This is a fairly basic way of using it, you can replace it with arbitrary comparator depending on your needs. U2 and U3 are the 74HCT4052 analog multiplexers implementing the nx2 matrix switch, 4x2 here. U4 is the CPLD implementing the state machine, with R2/C1 providing clock as a Schmitt trigger relaxation oscillator.Benefits of this circuitry:* This is scalable almost ad infinitum, as long as you have enough multiplexers. Replace the two 74HCT4052 with 74HCT4051 and you get the 8-way version, etc.* You can even get away with using only one analog multiplexer, if one or both of the inputs to the comparator has a sample and hold circuitry.* This circuitry has bipolar analog inputs, permitting the input to go anywhere from -5V to +5V.

author: GeorgeOfTheJungle
author_rank: Super Contributor
timestamp: November 19, 2018, 12:49:46 pm
content: I've heard there's a 3 cents^W 7 cents µC with twelve twelve bits ADCs: pms132.

author: technix
author_rank: Super Contributor
timestamp: November 19, 2018, 12:56:59 pm
content: Quote from: GeorgeOfTheJungle on November 19, 2018, 12:49:46 pmA full dev kit and programmer costs well into kilobucks territory...

author: spec
author_rank: Frequent Contributor
timestamp: November 19, 2018, 06:31:14 pm
content: Quote from: technix on November 19, 2018, 12:24:23 pmThank you for taking the time to draw this up- I like looking at circuits and I am sure others do too.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: November 19, 2018, 06:35:01 pm
content: Yabbut... you need imagination to do design. I interpreted this thread as a design thread. Can words not generate an image in your mind?Tim

author: spec
author_rank: Frequent Contributor
timestamp: November 19, 2018, 06:38:48 pm
content: Quote from: GeorgeOfTheJungle on November 19, 2018, 12:49:46 pmWow 12 bits: that is useful. Thanks for posting

author: spec
author_rank: Frequent Contributor
timestamp: November 19, 2018, 06:44:20 pm
content: Quote from: T3sl4co1l on November 19, 2018, 06:35:01 pmI think we have seen the difference between imagination and an actual circuit. Would you kindly desist. I am only interested in technical matters.

author: spec
author_rank: Frequent Contributor
timestamp: November 19, 2018, 06:51:40 pm
content: Quote from: SiliconWizard on November 18, 2018, 08:44:32 pmAh yes! I hadn't thought about voltage rangeIt's always the simple things that get you.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: November 19, 2018, 06:57:19 pm
content: Voltage range is easy, put a divider in front of everything.(Matched resistors, of course.)Visual aid:https://www.seventransistorlabs.com/Calc/ResDiv.html#threea calculator I wrote just for this kind of purpose, actually.Tim

author: spec
author_rank: Frequent Contributor
timestamp: November 19, 2018, 07:06:36 pm
content: Nice one Tim. So the parts count is MCU + 8 resistors: two resistors per channel. I don't know about you all, but that circuit would get my vote.It has the advantages of simplicity, low cost, and flexibility. To add another input just requires 2 more resistors.Can you please post a schematic of this attenuator approach that you speak of- sorry, couldn't resist it

author: SiliconWizard
author_rank: Super Contributor
timestamp: November 19, 2018, 07:10:54 pm
content: Yes, dividing resistors + probably buffering to get reasonable accuracy. That's 4 more opamps then. Not easy!There's a few MCUs with 4 integrated opamps though (very few), you can have a look at Microchip for instance. Could then be reasonable.

author: spec
author_rank: Frequent Contributor
timestamp: November 19, 2018, 07:14:32 pm
content: Quote from: SiliconWizard on November 19, 2018, 07:10:54 pmCan't you get MCUs with Hi Z analog inputs?

author: T3sl4co1l
author_rank: Super Contributor
timestamp: November 19, 2018, 07:14:47 pm
content: I wouldn't worry about buffering to MCU ADC inputs. Well, as long as they're conventional; who knows what you get at that price point.The firmware dev time will likely dominate well over the parts cost and layout time, until large production quantities.Got a schematic for that firmware handy?Tim

author: spec
author_rank: Frequent Contributor
timestamp: November 19, 2018, 07:30:51 pm
content: Quote from: T3sl4co1l on November 19, 2018, 07:14:47 pmI agree- just had a look at a couple of MCU datasheets.

author: SiliconWizard
author_rank: Super Contributor
timestamp: November 19, 2018, 07:43:44 pm
content: Quote from: spec on November 19, 2018, 07:14:32 pmMost are not internally buffered, so the inputs directly get to the internal sample-and-hold block through switches.The most common max output impedance for input sources recommended for most MCUs I've seen is around 10k. Of course the end accuracy will depend on the sampling time, so without any buffering you'll be severely limited in sampling rate, unless you can use low-value dividing resistors if your input signals can drive that. So it will all depend on the characteristics of your input signals and how fast you want your 4-input comparator to be. Another factor will be that most MCUs (especially the cheaper ones) only have 1 sample-and-hold block, meaning that you can't sample all 4 inputs at the same time, again this will hinder the speed and accuracy of the comparator. For slow-changing input signals, that would be adequate. But all in all, the cost, accuracy, simplicity, operating range and speed of the opamp/diode based solution is not that easy to beat.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: November 19, 2018, 09:21:59 pm
content: Yeah, in the typical case, the pin connects to a sampling capacitor. The RC time constant plus leakage sets the maximum value for meetingallspecifications. The actual leakage current is small, so the resistance is almost entirely just to ensure analog bandwidth comparable to the maximum sample rate.Tim

author: not1xor1
author_rank: Frequent Contributor
timestamp: November 20, 2018, 01:18:52 pm
content: just for fun (would need low tolerance or selected parts)

author: T3sl4co1l
author_rank: Super Contributor
timestamp: November 20, 2018, 05:13:03 pm
content: ^A perfectly fine application of the TL431, knowing that it is, in fact, an op-amp with a shortage of pins, an open-collector output, and a disturbingly large yet conspicuously stable (2.50V) input offset voltage.Would also need the high side level translation, and would benefit from a low side CCS, but technically uses fewer pins than the LM324.Tim

author: NiHaoMike
author_rank: Super Contributor
timestamp: November 21, 2018, 01:57:31 am
content: Quote from: T3sl4co1l on November 20, 2018, 05:13:03 pmThere have been contests around the 555, maybe it's time to have one for the TL431?

author: T3sl4co1l
author_rank: Super Contributor
timestamp: November 21, 2018, 02:05:02 am
content: Quote from: NiHaoMike on November 21, 2018, 01:57:31 amSure.I want to see one around the UC384x. I propose that it is as universal, or even more versatile than the 555.Tim

author: not1xor1
author_rank: Frequent Contributor
timestamp: November 21, 2018, 05:27:14 am
content: Quote from: T3sl4co1l on November 20, 2018, 05:13:03 pmProbably the LM385-adj would work as well, with ground referenced output and finding the lowest output.