# Distinguish between four voltages - without using an ADC

#### __Jana__

Joined Aug 7, 2019
9
Hello!
I am trying to distinguish between four different voltages on an input:
0 V
4 V
8 V
12 V

There is no need for precise measurement.

Eventually this information needs to be fed to a Microcontroller.
The easy solution would be to use an ADC, but I had issues with ESD where the ADC got damaged.
Obviously, I will need to fix that and add more protection circuitry, but I was also wondering if there is a way not using an ADC at all and just rely on passive components?
I can think of how to achieve this using two Schmitt triggers, but maybe there is another solution someone can point me towards?

Thanks!

#### Papabravo

Joined Feb 24, 2006
14,831
Use comparators like LM339(quad) or LM393(dual)
They can be used to detect individual levels or ranges (aka window comparator).
Comparator inputs can be protected with diodes and current limiting resistors. They are made with bipolar transistor and are more immune to static discharge than CMOS parts.

OR

Use a bipolar input opamp to isolate the ADC from the external circuitry.

#### MrChips

Joined Oct 2, 2009
22,478
How quickly do you need to recognize these voltages?
In other words what is the maximum response time required?

#### __Jana__

Joined Aug 7, 2019
9
Hello! Thank you for the inputs!

The response time is no big concern, anything in the lower ms range should be fine.

#### dl324

Joined Mar 30, 2015
12,212
Obviously, I will need to fix that and add more protection circuitry, but I was also wondering if there is a way not using an ADC at all and just rely on passive components?
Why don't you just use an analog input with a voltage divider?

#### __Jana__

Joined Aug 7, 2019
9
Why don't you just use an analog input with a voltage divider?
The signal goes to an IO-Expander, because I have too little GPIOs (and same counts for analog inputs) going back to the SOC.

#### MrChips

Joined Oct 2, 2009
22,478
You can do it with one GPIO pin and a resistor and capacitor.

You can also do it with a 2-bit resistor ladder DAC and one input pin for a total of 3 MCU pins. If the MCU has an internal comparator so much the better but not necessary.

#### WBahn

Joined Mar 31, 2012
26,398
One thought that comes to mind it to use one comparator to get a HI/LO based on if the signal is above/below 6V. That would be your upper bit.

Then use that signal to select a reference of either 2 V or 10 V for the second comparator. That would produce your lower bit.

You can use an op-amp circuit to add/subtract the needed voltage instead of using a mux and you can scale the inputs to keep everything well behaved.

#### MrChips

Joined Oct 2, 2009
22,478
We can do this with four resistors.

What is the maximum input signal voltage?
What is the operating voltage of your microcontroller?

#### Beau Schwabe

Joined Nov 7, 2019
76

Code:
INPUT    LSB       MSB
0V        0        0
4V        1        0
8V        0        1
12V       1        1

#### __Jana__

Joined Aug 7, 2019
9
We can do this with four resistors.

What is the maximum input signal voltage?
What is the operating voltage of your microcontroller?
Hey MrChips,
the voltage range is from 0 to 12 V. There will only be four discrete states: 0V, 4V, 8V, 12V
The microcontroller can handle 3.3V, but I'll probably need a GPIO expander. The GPIO expander can handle 5V

#### MrChips

Joined Oct 2, 2009
22,478
If I were to do this with the simplest circuit I would just use two resistors and a capacitor into one GPIO pin.

Here is another idea. If this does not work then the next step is to add voltage comparators as already shown.

#### WBahn

Joined Mar 31, 2012
26,398
If I were to do this with the simplest circuit I would just use two resistors and a capacitor into one GPIO pin.
How is that going to work? Is your idea to set the pin to an output for a while and then set it to an input and measure how long it takes to become recognized as a HI? If so, do you know what voltage that recognition threshold will be with sufficient certainly from part to part and over temperature? What will happen as the signal slowly changes as it passes through the undefined region?

Here is another idea. If this does not work then the next step is to add voltage comparators as already shown.

View attachment 225044
This violates the voltage levels of at least one pin for every non-zero input. In also puts one of the pins at 1.66 V, which is right at the midpoint of 3.3 V operation. It seems like you are inviting both unpredictable operation but also shoot through.

#### __Jana__

Joined Aug 7, 2019
9
If I were to do this with the simplest circuit I would just use two resistors and a capacitor into one GPIO pin.

Here is another idea. If this does not work then the next step is to add voltage comparators as already shown.

View attachment 225044

That looks like an option. Thank you I will think it through and test it out.

For your suggestion with two resistors and one capacitor, I assume this is what you mean?

Two resistors to scale down from 12 V (max) and the capacitor to filter the signal from high frequencies?

#### MrChips

Joined Oct 2, 2009
22,478
Two resistors and a capacitor

Set the GPIO to output and set the output to 0.
Set the GPIO to input and measure how long it takes to read 1.
Reset, rinse and repeat.

#### RIKRIK

Joined Oct 11, 2019
127
Hello!
I am trying to distinguish between four different voltages on an input:
0 V
4 V
8 V
12 V

There is no need for precise measurement.

Eventually this information needs to be fed to a Microcontroller.
The easy solution would be to use an ADC, but I had issues with ESD where the ADC got damaged.

Thanks!
What about a voltage to frequency converter, little 8pin dip package, cheap as chips . LM231

Last edited:

#### WBahn

Joined Mar 31, 2012
26,398
Two resistors and a capacitor

Set the GPIO to output and set the output to 0.
Set the GPIO to input and measure how long it takes to read 1.
Reset, rinse and repeat.
And at what voltage does it start reading a 1?

#### MrChips

Joined Oct 2, 2009
22,478
And at what voltage does it start reading a 1?
I am sure you can figure this out.

#### WBahn

Joined Mar 31, 2012
26,398
I am sure you can figure this out.
No, I can't -- and I don't think you can, either.

You are missing the point -- the point I explicitly made previously when I said, "If so, do you know what voltage that recognition threshold will be with sufficient certainly from part to part and over temperature?"

With 3.3 V logic, you are only guaranteed that your LO output will get down to 0.4 V. So you need to allow for a starting point anywhere between 0 V and 0.4 V. The HI detection threshold is guaranteed to be no higher than 2 V, but it is often in the 1.5 V range and could be as low as 0.8 V without violating the spec.

How much do the actual thresholds vary from part to part? How much does it vary even in the same part as it warms up?

Your circuit never will detect a HI if the input voltage is 0 V, but that's not a huge issue since you just have to have a max time limit. But in order for you 4 V input to be sure to ever be seen as a HI, the final voltage needs to be above 2 V and to have reasonable times it needs to be quite a bit higher. With 5 V you are looking at over two time constants, which means that your final voltage for a 12 V input is going to be more like 15 V on a 3.3 V input. Since you don't mention using a clamping diode, are you going to rely on the internal pad protection diodes?

#### MrChips

Joined Oct 2, 2009
22,478
Despite what the previous poster wrote you can build your own ADC with a single resistor and capacitor.
The diode to the positive supply is added to clamp the voltage to just over 3.3V. A Schottky diode such as 1N5817 should be used. The voltage on the capacitor will not exceed ½Vsupply while the program is running because the GPIO pin will discharge the capacitor to ground when this voltage is reached.

The input voltage must be higher than ½Vsupply, 1.7V in this example.
The program measures the time it takes to charge the capacitor to ½Vsupply. For high input voltage the time is short and increases as the input voltage decreases.