I'd like to embark on a project that takes two inputs, compares them, and then performs an action based on the outcome of the comparison.
More specifically, I would like the inputs to come from some sort of distance sensor (digital or analog). The distance to be measured will be between 0.5 and 8 inches. Both inputs can vary from 0.5 to 8 inches. I'd like one of the inputs to be a "reference" and the other to be compared against it, such that if input1 is currently at 0.75'', input2 (which needs to be monitored realtime) will be compared against that. input1 will remain static over the course of time in which input2 needs to be monitored.
I'm familiar with microcontrollers, in particular the Teensy, and I know I could do this project going that route. However, this is something I'm doing at work in an industrial application on a large(ish) CNC machine, and I'd like to keep everything as simple and dummy proof as possible. Therefore, I'd like to explore building a logic circuit or using a IC comparator if it makes sense in this application. Unfortunately, I don't have a lot of experience in either of these two topics.
The flesh out the project for you, this circuit is essentially going to act as an e-Stop on my machine. When input2 is <= input1, it will trigger an e-Stop. I'll have to get more info from my maintenance team, but they've already wired up some e-Stops on this machine so that part of the integration should not be too difficult: essentially, where previously our e-Stop was triggered by a hard button, this "e-Stop" will be triggered by the output of this comparator. I think powering the logic circuit may be a chore, though, as the machine runs off of 480V power supply, though I may be able to hack in to a DC supply somewhere or other, as there are already some controllers on the machine that the manufacturer provided.
Any suggestions or advice you can give for approaches to this project would be greatly appreciated. Even if you have suggestions for sensors that can work in this type of application.
Any Op-Amp will do. For an Op-Amp, There is a + and - input. If the + input is greater than the - input, it will drive the output up to V+ (Give or take maybe 0.5V depending on the chip). If - input is greater than the + input, then it drives the output to V-. So all you'd need to do is set up a Pot and find some form of Analog DC output Distance sensor. Have the output drive a relay as your E-Stop (Or something similar). Or possibly, you can do some form of Latching relay topology (You connect one of the NO outputs to supply power, so when you start it, it will stay closed, and then have something to break power.)
However, you'll probably need filtering, which means some relatively large caps on the various outputs. Also, since you are using it in a CNC machine, most likely your speeds are on the millisecond scale, so you don't need an expensive op-amp. Find something that will supply appropriate voltages. I'm not sure what voltage range you are working in.
Thank you for your response mirith. I was also thinking an OpAmp would work, I just wanted to explore the IC route. The Pot you described is for....what exactly. To tune in the gain on the inputs? Also, how could I work in an tolerance window on the comparison? I.e. I don't need the two inputs to be exactly equal, I'd like to account for some slop in my mechanics, i.e. give or take 0.1'' or, say, 1 V (ish?). Also, can you expand a bit on "latching relay topology". I'm a bit familiar with the concept of a latching relay but more info would help.
Using an OpAmp as a comparator is what you would use in terms of an IC. Its one of its major functions. The Potentiometer (Or some other way of setting a voltage) would be a way to set your threshold. This would require no specific IC other than the OpAmp.
A different way would be to do everything in software, which you do by having something like an arduino measuring the analog output (Or digital output) of your distance sensor, and then you use software to compare the value, either from something saved in the software, something you send to it (Probably via serial), or something you set using some form of Pot or rotary encoder (Later would require a display). The benefit would be custom software filtering (Welcome to DSP), which might be necessary due to any imprecision/noise on your sensor, and any custom profiles, or complex logic.
The latching relay circuit I've used was using 2 DPDT standard relays (Which could be simplified), but you put A Normally Open (NO) Reset/On button (Momentary) in parallel with the NO Section of Relay 1 with one side connected to + Power (Or Live if AC). You connect the other side to Relay 1's coil. Finally, you connect the 'negative' side of Relay 1's coil to Relay 2's NC (Normally Closed) area (Or an NC Momentary Button, but if you want to use both, put them in series). Finally the other end of Relay 2's connection goes to your Negative (Neutral if AC). You drive the coil of relay 2 using your comparator circuit somehow (Depends on your relays). The other pole on Relay 1 is then used to power your circuit.
When you press the Reset button, it energizes the coil, which closes they relay, and so when you release the reset button, it stays on as its own contacts complete the circuit. When you need to stop the unit, you use Relay 2 (Or a button) to break the circuit, cutting power.
So, the normally closed portion of relay one is connected to (+) power, correct? Also, I don't get the part where you say "it stays on as it's own contacts complete the circuit". I see how relay 2 is used to cut power: I can have my logic circuit turn relay 2 on/off as appropriate, which would also cut/provide power as needed. I guess I'm lost as to the whole need for relay 1, and how this latching thing works: how do you unlatch it?
Okay, tried to diagram it below. So, when its off, Everything is off. To Turn it on you hit the NO Button. This closes Relay 1's contact, which is in parallel with the Button, and replaces it, basically acting like it is holding down the button. This latches Relay 1 on. Now to turn it off, you either engage Relay 2 (Which, while normally closed, completes the circuit) or activate the NC button (Same as Relay 2). This breaks the circuit, disengaging Relay 1's coil and resetting Relay 1's contact to off, now back into the initial off state.
Just take a moment and think about it step by step when you press either button what happens. Hopefully it will be clearer.
So when I engage relay 2, I take a (-) from the normally open contact on relay 2 and a (+) from the normally open contact on relay 1 to drive my e-Stop circuit. My question, though, is that when relay 2 gets engaged, and it disengages relay 1, how long will there be a (+) signal present? I need that (+) to stay on long enough to trigger the e-Stop. If I'm not mistaken, once the reaction time of the relay has passed, I'll be left with just the (-).
Also, why is a DPDT relay needed? Wouldn't this work equally will with a SPST for relay 1 and a SPDT for relay 2?
The idea behind this is actually to be able to completely cut power to something as your E-Stop (Proper E-Stop). Relay 1 is a DPDT (Or technically a DPST I think would work), Specifically, so that the OTHER contact is supplying power to the rest of your circuit. This is basically an On/Off switch that can easily kill power and doesn't require high voltage switches. V+ does not need to be the same as for your circuit in this case, but your contacts need to be able to handle whatever is going through them.
To Drive Relay 2's Coil, you'll need to get power from somewhere. It could be this circuit, but in all reality, it should probably be from the circuit you are driving, to make it self contained.
I don't have the time or software to actually draw this out for you, but this explains the concept:
I think I got it. See attached schematic. Now I just need to find a sensor to use and go from there. Are resistors needed in the comparator circuit? And if so, where and what value?
There is a common fallacy going around that you can use an opamp as a comparator,
but this is not always the case - some opamps can be permanently damaged if used as a
comparator from a high voltage supply (the input circuitry is not designed for large
voltage differences between the inputs for any length of time - opamp circuits keep
the two inputs at the same voltage with negative feedback.
Another issue is slew-rate and output voltage. If your opamp has a sluggish slew-rate
(it will) you'll need to clean its output up with a schmitt-trigger. If the opamp is powered
from different voltage to the logic circuitry you have to do level conversion or clipping of
some kind too.
And lastly many opamps don't have rail-to-rail outputs, and this may mean that when
powered from 5V they cannot generate a guaranteed logic HIGH or LOW on their outputs
at all, leading to unreliable operation.
On the other hand comparators are purpose designed to be comparators and usually
have open-collector outputs, tolerate input voltages across the entire supply and
switch the output much faster (with hysteresis). quad comparator like the LM339 is
dirt cheap too.
For low performance uses many opamps can be pressed into service as comparators,
but its not great practice and simply doesn't work at high speeds. (However this can
be an advantage as high speed noise is going to get filtered out, whereas fast comparators
respond to high speed noise)