Voltage reading formula

MrChips

Joined Oct 2, 2009
34,820
If you look at the original question, @gogo00 asked how to read 12 volts with a 5 volt input ADC. "I know I need to use a voltage divider."

He was satisfied by post #3. The following 36 replies have done nothing to further answer the very BASIC question.

That basic question hasn't really been fully addressed. "Use a voltage divider" is a great toss-off answer, but considering the level of the question, should it be? Is it as simple as grabbing any pair of resistors that produce a 5 volt output from a 12 volt input? How about 180k/130k?

No, it's not that simple, but instead y'all are arguing about the most efficient way to scale (scale, not calibrate) the resulting reading. Key point missed is that the acceptable range of voltage divider resistors depends on the input impedance of the ADC.

Nobody asked about the accuracy needed. This resulted in another key point being missed – any discussion of the voltage reference. If the micro is operating from a regulated supply, using Vdd as the reference is probably fine but perhaps the micro has a built-in fixed voltage reference. Basic questions need basic answers.

There is a tendency here to get lost in the weeds. To go off answering the question you want to answer (and often insisting your way is the only correct way to accomplish a thing), ignoring the gist of the question asked. And these threads go on for dozens if not hundreds of replies after a TS has stopped reading.

Maybe this is something to think about, but I'm afraid the most guilty will continue giving doctorate level "answers" to kindergarten level questions.
I beg to disagree.

This is what happens when someone starts developing code in C or Arduino without fully appreciating the underlying math.
It is akin to someone writing a script in an Excel spreadsheet and declaring themselves to be a "programmer". The tendency with many HLLs today is to hide the implementation details from the user so that they can get on with solving the problem.

In my opinion, the problem lies in understanding basic arithmetic. The outcome is manifested in "code bloat" where the modern solution is to demand faster processors with more memory space. Sorry, I don't subscribe to that philosophy.

With due respect to the TS, this thread in the first place is evidence of the problem. If TS had a strong command of basic arithmetic this thread would have been unnecessary.
 

nsaspook

Joined Aug 27, 2009
16,329
Ada got this right.

Fixed point data types are defined by;

range low .. high delta d

Low, high and d are real numbers.

So, if i want a variable that can represent a temperature range up to 800C I can defind a type as:

type temp is range -273.15 .. 800.0 delta 0.1

And the compiler can choose the representation based on offsetting and scaling. It may chose, for instance, to add 273.15 to each value to make it positive, then we have a range of 0 .. 1273.15 and use an unsigned value. Or it may leave the negative values, requiring a signed value.

It might decide to scale it by 100, the minimum to get that delta, or by 128 to be able to scale with one shift or scale my 256 in order to require no shifting.
Still waiting for that 8-bit Ada compiler I was promised decades ago.
Modula-2 was pretty good too.
PXL_20240112_155626682.jpg
 
Last edited:

nsaspook

Joined Aug 27, 2009
16,329
I beg to disagree.

This is what happens when someone starts developing code in C or Arduino without fully appreciating the underlying math.
It is akin to someone writing a script in an Excel spreadsheet and declaring themselves to be a "programmer". The tendency with many HLLs today is to hide the implementation details from the user so that they can get on with solving the problem.

In my opinion, the problem lies in understanding basic arithmetic. The outcome is manifested in "code bloat" where the modern solution is to demand faster processors with more memory space. Sorry, I don't subscribe to that philosophy.
You can fully understand the underlying math and still decide at shaving bits is a waste of valuable programmer resources. The wise programmer knows when to hold them and, when to fold them.
 

BobTPH

Joined Jun 5, 2013
11,521
Still waiting for that 8-bit Ada compiler I was promised decades ago.
Modula-2 was pretty good too.
View attachment 312567
I wrote my own Ada-ish compiler, but it is 16-bit. Targeting PIC24/33. I did include fixed point since it is so useful in embedded. I use either 16 or 32-bit signed or unsigned, no offset, and scale with the smallest power of two that the delta requires.
 

nsaspook

Joined Aug 27, 2009
16,329
I wrote my own Ada-ish compiler, but it is 16-bit. Targeting PIC24/33. I did include fixed point since it is so useful in embedded. I use either 16 or 32-bit signed or unsigned, no offset, and scale with the smallest power of two that the delta requires.
I liked programming in Modula-2/Ada a lot more than C but you need to be practical in industry. Programming experience in the Pascal based languages make you a much better C programmer because you know there's is a way to structure your software design to be less buggy by defining strict interfaces and modular blocks even in languages like C that don't need it, so we see much more Cowboy coding in C or Python with little thought of design analysis.

1705095743532.png1705095757450.png
 
Last edited:
Top