I'm trying to trigger an interrupt on a digital pin of a Raspberry Pi (although the question is valid for any microcontroller), based on an adjustable threshold value of an input analog signal (say 0 to 5V).
In other words, I have a sensor providing a voltage from 0 to 5V, and I'd like that when this value gets higher than an adjustable threshold within the range (e.g. above 3V), I get a
proper logical value of 1 (say 5V), and when the value goes down that threshold (e.g. under 3V), I can get a proper logical value of 0.
I'd like also to be as simple as possible (even if the threshold is fixed), as I've seen solutions involving op-amps that seem quite complicated for me. I've also discarded an external ADC because I want to drive the microcontroller on interrupts instead of polling values.
What would you consider to be the simplest way to do this? Thanks in advance.
1 Answer
What you're describing is a textbook case for a comparator. A comparator is superficially like an opamp, but it's specifically designed to output logic level values: high when the positive terminal voltage is higher than the negative terminal, and low otherwise. Most also have some hysteresis, to prevent rapid switching when the voltage is near the threshold.
When choosing one, note that most comparators have open collector outputs - they pull low, but not high. This makes interfacing between the logic level of your digital device, and the possibly different voltage levels of the signal you are testing, easier - just include a pullup on the output to logic high.
simulate this circuit – Schematic created using CircuitLab
In the example above, the voltage divider R1 R2 sets a reference voltage to compare to. Input on the + terminal is compared to the reference; if it's greater than the reference, the comparator output switches off, and the pullup R3 pulls the output voltage high. If it's less than the threshold, the comparator switches on, pulling the output voltage low.
-
\$\begingroup\$ so, I nailed the question, you nailed the answer. Great! Thanks \$\endgroup\$– RobertoCommented Feb 24, 2015 at 13:28