I'd like to have a switch activated when something gets near it. Today it's just a pushbutton, that's active as long as it's depressed.

I'm going to use an analog ultrasonic ranger, using its analog output. What I'd like to do is have an analog circuit activate a mosfet when a certain range threshold is hit. My question is - how do I make this "binary?" How do I have Vgs applied to the mosfet when the analog voltage on the ranger crosses a specific value? I thought about using a zener diode with the breakdown voltage at the distance that I need (the threshold I'm looking for is about 10" away from the device, which translates to ~100mV on the analog pin). Given the low voltage the sensor will output at that distance, I assume a zener won't work.

I'd like to not involve an MCU if possible, because I'd like this to be a simple drop-in.

  • \$\begingroup\$ Are you looking for a Schmitt trigger? \$\endgroup\$ – Andrew Morton Mar 9 '15 at 22:04
  • \$\begingroup\$ I suppose I am, now that I know what that's called :). Mind expanding on that as an answer? \$\endgroup\$ – kolosy Mar 9 '15 at 22:09
  • \$\begingroup\$ I think it might be better to wait for replies from people with more knowledge on this than me ;) \$\endgroup\$ – Andrew Morton Mar 9 '15 at 22:11
  • \$\begingroup\$ @AndrewMorton is right. I posted my answer before I saw his comment. Basically, you'll use a comparator (see my answer) to build a Schmitt trigger :) \$\endgroup\$ – bitsmack Mar 9 '15 at 22:13
  • \$\begingroup\$ Also, please note that the data sheet indicates you will not be able to tell the difference between 1 inch and 6 inches. Any range of 6 inches or less will be reported as 6 inches. This may or may not be a problem, but you need to be aware of the limitation. \$\endgroup\$ – WhatRoughBeast Mar 9 '15 at 22:17

A simple comparator circuit will do:


simulate this circuit – Schematic created using CircuitLab

U1 can be a slow, rail-to-rail CMOS part that costs under $1 - either a general purpose op-amp, or a dedicated comparator. Ensure that there's no diode between (+) and (-) inputs. Microchip has plenty of them, for example.

Since we assume push-pull outputs like on every op-amp, there's no need for any pull-up/pull-down resistors on the output of U1. If you use an op-amp, that'll be the case. With comparators - it varies.

The ratio of R3 to R4 sets the amount of hysteresis - here it's at 1%. The turn-on voltage is set by the ratio of R1 to R2, and given by VCC * R1/(R1+R2) * (1 - R3/R4). R5 isolates the gate capacitance from the amplifier's output, preventing the amplifier from oscillating.

  • \$\begingroup\$ Awesome. What's the purpose of C1 here? \$\endgroup\$ – kolosy Mar 9 '15 at 22:12
  • \$\begingroup\$ C1 is a decoupling capacitor and isolates the dynamic currents needed by U1 from influencing the rest of the circuit. \$\endgroup\$ – Kuba Ober Mar 9 '15 at 22:14
  • \$\begingroup\$ @KubaOber And, as a decoupling capacitor, should it be placed as close as electrically possible to U1? \$\endgroup\$ – Andrew Morton Mar 9 '15 at 22:16
  • \$\begingroup\$ Actually, a general-purpose op amp won't work here. The sensor output at 10" will be 100 mV or less, so you need rail-to-rail input capability. \$\endgroup\$ – WhatRoughBeast Mar 10 '15 at 1:41
  • \$\begingroup\$ @WhatRoughBeast I'm presuming, perhaps wrongly, that the part will be used according to its datasheet :) \$\endgroup\$ – Kuba Ober Mar 10 '15 at 14:33

You're looking for a "comparator":

enter image description here

You apply a voltage to the Vin- pin which sets the threshold. This voltage is usually derived from a resistor divider. Then, your analog signal is connected to the Vin+ pin.

Choose a comparator with push-pull outputs, instead of open-drain. This way you won't need pullup resistor on the output.

There are also methods of setting hysteresis, if you need to. Here's a good App Note from Maxim.

This is the general layout:

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.