What's the best way to read varying voltage values?

This is an automotive project. This is my first time working with the Arduino (and it's been years since I worked with any uC) and I purchased this Mega 2560 The Most Complete Starter Kit – ELEGOO Official so I'm using the Mega 2560 to prototype with.

I need to interface with a single wire that has varying voltage values when a switch is rotated to differing positions. There is a pull up resistor that has voltage (as a default) to 12vDC, position 1 drops voltage to ~7.58vDc, position 2 to 4.18vDC and position 3 to 0.86vDC. There is also another source wire I want to monitor that is 12vDC default and goes to 0 (binary) when a switch is depressed.

I do not know how dirty the voltage is on either, am I best to use an ADC or a resistor ladder? Is the Arduino safe with a resistor ladder if the resistance values are in the 100k ohm range (stunting current) or are Zeners still needed?

In an automotive environment, plan on extreme "dirtyness", especially from the 12V wiring. Expect occasional spikes over 100V, and even possible polarity reversals. See this overview, and plan on protecting your circuit with TVS diodes, capacitors, etc.

You might take a closer look at your signals, they may be ratiometric as the system should work from about 6V to 24V exact values depending on the OEM. Also remember the polarity can be reversed if the vehicle is improperly jump started, this is part of the specifications.

There is a good app note AN2689 by ST on automotive electronics. reading it will help you a lot. https://www.st.com/resource/en/application_note/cd00181783-protection-of-automotive-electronics-from-electrical-hazards-guidelines-for-design-and-component-selection-stmicroelectronics.pdf Also take a look at this: Distilled Automotive Electronics Design | Analog Devices I hope this is a one up design.

For your specific question, in the past I've used one analog input like my first diagram and done the level detection/ladder in software. It can easily be modified then.

For your on/off switch input (my diagram 4) you can substitute a 4049 for an inverted signal - it may depend if the state matters when everything is first powered up.

Below is what I've distilled from articles such as the ones linked, and some experience with dead electronics in cars.
Parts I've used are because they are local or I already had them, so you might find better parts.
Some might be overkill and you could substitute for sanity.

Any input should have schottky diodes and a resistor at minimum. I've avoided ordinary zener diodes as they can affect the input, and the schottkies probably do a better job anyway.
I use low-ish value voltage divider resistors to ensure a reliable analog read.

Analog input, suitable for automotive voltages
image

Another analog resistive sensor input, and a Dallas Onewire input:

Switch input, debounced, switch to ground, not linked to vehicle voltages

Another digital input from vehicle switches that are on or off with full vehicle voltage swing, like headlight switches for example

The +5v supply, Vcc in my diagrams, for pullups, schottky diodes and anything except the Arduino itself should have a separate supply. This ensures any pulses, noise or otherwise returned via the protection diodes is handled by a separate regulator and NOT the Arduino internals.

The LM2940 is specifically made for automotive use, but you could substitute the ordinary old 7805. The tantalum caps pictured near the regulator should be physically very close to the regulator.
I have the Arduino itself powered by a semi-filtered supply - if the onboard regulator gets hot stick a couple of 1N5401's or 4.7R 5W resistor in series just to drop the supply voltage a bit. In the past I've used one of those cheap adjustable Arduino switching supplies set to 10v. They work great.
image
Powering the Arduino via it's barrel jack or the Vin pin gives the most protection. I wouldnt try to power an Arduino Mega2560 via 5 volts in an automotive environment unless it is extremely close to the regulator - best to let some filtering and the onboard regulator do their job.

You can run 3 or more Arduinos from this baby:
image

Overall, I've gone fairly complicated, so use what suits you and your reliability requirements.

1 Like

And more good information:

Woiuld you care to tell us what function the switch is supposed to perform? EG does it control the air con fan?

That kit isnt cheap - maybe you could consider getting a "cheap as chips" nano for testing?
You can use the ADC with some careful filtering to measure the voltage. A reasonably large value series resistor (eg 100k) and a capacitor to ground would protect the input.

They are ratiometric and thanks for the links. I was hoping there were components (break out boards or an Arduino pcb with automotive protection built it) that were ready made to use, as I'm not an engineer, however it appears that is not the case.

You might take a closer look at your signals, they may be ratiometric as the system should work from about 6V to 24V exact values depending on the OEM. Also remember the polarity can be reversed if the vehicle is improperly jump started, this is part of the specifications.

There is a good app note AN2689 by ST on automotive electronics. reading it will help you a lot. https://www.st.com/resource/en/application_note/cd00181783-protection-of-automotive-electronics-from-electrical-hazards-guidelines-for-design-and-component-selection-stmicroelectronics.pdf Also take a look at this: Distilled Automotive Electronics Design | Analog Devices I hope this is a one up design.

1 Like

That would be nice but for which system do you design it for. A trick you can use is measure the 12V voltage and your signal in rapid succession then do the rationing with the processor. For example if the battery is 110% then multiply your reading and setpoint by 110%, that should get you close.

1 Like

Thank you for all of this, this is going to take me some time to digest, however I can see the value in all of it. Eventually I want to add something that filters out analog values that are too low or too high (out of range for a pressure sensor) and I believe some sort of 4050 clipping circuit could be appropriate.

I've ordered the Schottky's, and Buffer's and should be able to bench build something this weekend.

What do you think of this power supply?

One controls a transmission adjustment, one controls mode of driving and one controls traction nannies.

I'm open to any predesigned PCB suggestions that have solder terminals on them so I can add some of the circuitry Xfacta has posted here if you have any suggestions.

My solderable PCB space requirements would be (non smd)

Room for 4 of these

Room for a piezo buzzer

USB connection and all of the other standard Arduino parts such as the clock, power supply, reset button, etc

Room for one other circuit that will clip voltage when it goes too high or low, the sensor is a 5v analog pressure sensor

A blue tooth connection to interface with a programming app

Woiuld you care to tell us what function the switch is supposed to perform? EG does it control the air con fan?

That kit isnt cheap - maybe you could consider getting a "cheap as chips" nano for testing?

As a standard signals on a schematic go Input - process - output.
I'm guessing this diagram is t'other way around.
You could give better protection AND provide smoothing
with R38=330k, R40 150k, C33=1uF (time constant of 0.1sec.)
schottky diodes or a 5817 would be a better choice - the will be no big currents.

NB You will need a suitable ground connection.

Hi John,
Thanks for your input. Yes I have a couple of my diagrams the wrong way around, it suited my overall layout onscreen (all external connectors around the perimeter) but I'll fix that.
I've taken on board your resistor suggestions, and changing from 1N5819 to 1N5817 for the lower forward voltage drop.
Everything was working OK on veroboard, but I'll end up getting a real circuit board made including those changes.

EDIT
In fact the BAT46 (or BAT48) schottky diodes would be just as good in this case, since their forward current could never be high, and their forward voltage drops is the same as a 1N5817

1 Like

Hello @xfacta ,
I have set this up and am getting unexpected results on the bench with my DMM. What do you think of this implementation? I think my power supply is the culprit, however I wanted to get your input. Thanks

what do you expect?

What DMM? (link?)

I was expecting it to behave as it does in the vehicle, the voltage drops to close to zero when I press the switch and then jumps right back up when I let go of the switch, however it stays at zero volts for a minute or so like I have tripped an overload on the power supply. I tried using a 1k resistor in place of the 10 ohm, but it did not pull the voltage down.

This is the meter

https://www.amazon.com/INNOVA-3340-Automotive-Digital-Multimeter/dp/B000KIMHRQ/ref=asc_df_B000KIMHRQ/?tag=hyprod-20&linkCode=df0&hvadid=242027088707&hvpos=&hvnetw=g&hvrand=12996503745926790110&hvpone=&hvptwo=&hvqmt=&hvdev=m&hvdvcmdl=&hvlocint=&hvlocphy=9007840&hvtargid=pla-437308582715&psc=1&mcid=8764b1b4e9c23e51bdfb45eb99cedc12&gclid=Cj0KCQiAxOauBhCaARIsAEbUSQR7SnUjKvgKKIafKZXYZd8ev5Tep0AgPjBsGekgXcCq7QLimHyrz3UaAq4lEALw_wcB

That is probably exactly what happened.

If you are substituting a power supply for the ECU with pullup than you need a pullup resistor between the power supply and your "analog voltage" connection in your circuit

Hi Jim,

Why wouldn't the power supply do the function of the pull up resistor? With the power supply providing it with 15v, so what is the advantage to having a second path through the resistor to the same power supply source?

Should I instead limit current from the power supply through the 5.1k and have that be the only power source into the circuit? Or is that what you mean?

There is only one path. The 12V thouugh the 5.1K.

Should I instead limit current from the power supply through the 5.1k and have that be the only power source into the circuit?

Yes exactly.

1 Like

Be flexible with 5.1k...
4k7 and 5k6 will also do the job and might be in your box with standard resistors...

If not: 2 10k resistors in parallel.

1 Like

It's so obvious now that you pointed it out :man_facepalming:

I'll have to remember that when it is after midnight and I can't figure something out, to try looking at it in the morning haha