Previous in Forum: Coronal Mass Ejections . . . What If??   Next in Forum: MOSFETs and Drivers
Close
Close
Close
14 comments
Rate Comments: Nested
Associate

Join Date: Jan 2009
Location: Cape Town
Posts: 54

10 Bit ADC Math

09/13/2010 11:06 AM

I am trying to use an ATmEGA32's 10 bit ADC to monitor over/under voltage on my PSU. My reference voltage is 2.5V. How would one convert 3.3V,5V and 12V voltages for the ADC?

__________________
"I never expect to lose. Even when I'm the underdog, I still prepare a victory speech"
Register to Reply
Interested in this topic? By joining CR4 you can "subscribe" to
this discussion and receive notification when new comments are added.

Good Answers:

These comments received enough positive votes to make them "good answers".

"Almost" Good Answers:

Check out these comments that don't yet have enough votes to be "official" good answers and, if you agree with them, vote them!
Guru
Technical Fields - Technical Writing - New Member Engineering Fields - Piping Design Engineering - New Member

Join Date: May 2009
Location: Richland, WA, USA
Posts: 21017
Good Answers: 795
#1

Re: 10 Bit ADC Math

09/13/2010 12:47 PM

Ten bits gives a range of 0-1023. If you can set it up for 80 units per volt, 2.5v = 200, 3.5v = 280, 5v = 400, and 12v = 960; which pretty well uses the full range.

__________________
In vino veritas; in cervisia carmen; in aqua E. coli.
Register to Reply
Guru
Engineering Fields - Electrical Engineering - Been there, done that. Engineering Fields - Control Engineering - New Member

Join Date: Dec 2008
Location: Long Island NY
Posts: 15600
Good Answers: 981
#2

Re: 10 Bit ADC Math

09/13/2010 2:12 PM

I'm a little apprehensive that you say that you have a reference of 2.5V. If this is the reference for the ADC then 2.5V will be the full scale voltage value. To then read any of the three voltages you list, these voltages will have to be scaled down to a voltage less than 2.5 VDC.

__________________
"Don't disturb my circles." translation of Archimedes last words
Register to Reply
Commentator

Join Date: Mar 2008
Posts: 78
Good Answers: 2
#3

Re: 10 Bit ADC Math

09/13/2010 11:02 PM

Using 3 inputs, you need to divide each by some arbritrary amount to give you a suitable voltage to compare with the reference while allowing some overhead.

ideally you never want the input voltages to exceed the 2.5v reference, divide the 12v by 6, the 5v by 2.5 and the 3.3v by 1.5 say with resistor dividers. Since these are power supply rails a fairly low value resitance could be used but you do not want the loss of the bottom resistor (say a dry joint) to cause the input current to exceed the chips protection capabilitys.

applying a current limit of 1ma or 2ma or so seems acceptable.

so 12v rail . 10k in series with 2.2k, (divider is 2.2/12.2 = 5.54)

5v rail 4.7k and 2.2k .. and so on.

working on the 12v rail as an example.

since 1023 counts is 2.5v then for the 12v rail it is now 1023 counts is 13.86v.

if you just need to monitor under and over voltage it is easy.

say under is 11.5v and over is 12.5v then the suitable counts are 11.5/13.86 * 1023 = 848, 12.5 is 922.

Register to Reply
Associate

Join Date: Jan 2009
Location: Cape Town
Posts: 54
#12
In reply to #3

Re: 10 Bit ADC Math

09/15/2010 3:56 AM

"...working on the 12v rail as an example.

since 1023 counts is 2.5v then for the 12v rail it is now 1023 counts is 13.86v."

Can yo please elaborate on this calculation. I'm getting ADC_count of more than 1023 for the 3V3 over_voltage

__________________
"I never expect to lose. Even when I'm the underdog, I still prepare a victory speech"
Register to Reply
Guru
Engineering Fields - Electrical Engineering - Been there, done that. Engineering Fields - Control Engineering - New Member

Join Date: Dec 2008
Location: Long Island NY
Posts: 15600
Good Answers: 981
#13
In reply to #12

Re: 10 Bit ADC Math

09/15/2010 12:59 PM

You cannot get more than 1023 from only ten bits. So there must be something wrong with your coding.

A two resistor attenuator is one of the simplest of all analog devices. The only thing that needs a possible explanation is that the current through the two resistors must be the same.

I'm sorry to sound offensive here, but if all you can do is repeatedly say that you don't understand when you've been given completed schematics, and a link that explains the theory of an attenuator, then we cannot help you. But it's not because we didn't try.

__________________
"Don't disturb my circles." translation of Archimedes last words
Register to Reply
Commentator

Join Date: Mar 2008
Posts: 78
Good Answers: 2
#14
In reply to #12

Re: 10 Bit ADC Math

09/15/2010 9:32 PM

The divider ratio is 5.54 .. from (R1 + R2)/R2 = (10+2.2)/2.2 = 5.54

so if you are reading 1V then you must have 5.54v at the input.

Since your a/d reference is 2.5v and this will give a full scale output of 1023d or 0x3ff hex. Then to get the maximum at the input to the divider the voltage in must be ..

2.5 x 5.54 = 13.86v

taking an arbritrary number .. say you get 342 .. then the input voltage must be..

13.86 x 342/1023 = 4.63V

The way I would do this calculation in a micro .. (using C)

calculate the voltage for 1024 counts .. 13.86 x 1024/1023 = 13.87

this makes integer math a lot easier as the divide becomes a shift.

need 11 bits for that, and 10 bits for the a/d reading .. so you need 21 bits perform math in a short long int. (24 bits)

void print_reading (unsigned int ad_reading)

{

unsigned short long calculate;

calculate = 1387;

calculate *= ad_reading;

calculate >>= 10; //(divide by 1024)

printf ("voltage = %u.%u",calculate/100,calculate%100)

}

so feed in 4.63v .. a/d count will be 342 and the printf will display "voltage = 4.63"

... now regarding values over 1023 .. as someone pointed out, thats impossible with a 10 bit a/d reading, the highest reading possible is 0x3ff so something is going on, you may need to display the raw a/d result register while varying the input voltage.

Register to Reply
Associate

Join Date: Jan 2009
Location: Cape Town
Posts: 54
#4

Re: 10 Bit ADC Math

09/14/2010 3:02 AM

I still don't understand this...Are you converting the voltages into hex values? What formula are you using to scale down all these voltages to less than 2.5V? What happens if you want to monitor a negetive voltage (like -5V and -12V for my application)?

I do have a voltage devider circuit to scale down all my voltages but I don't understand how they came about to get these equalities:

3V3 = 2.03V

5V = 2.08V

12V = 2.10V

-12V = 0.346V

-5V = 0.329V

__________________
"I never expect to lose. Even when I'm the underdog, I still prepare a victory speech"
Register to Reply
Guru

Join Date: Aug 2005
Location: Hemel Hempstead, UK
Posts: 5826
Good Answers: 322
#5
In reply to #4

Re: 10 Bit ADC Math

09/14/2010 5:44 AM
__________________
If you spend all your time looking for people and things to complain about: trust me, you will find plenty to complain about.
Register to Reply Score 1 for Off Topic
2
Guru
United Kingdom - Member - Not a New Member Hobbies - Musician - New Member Hobbies - Fishing - New Member

Join Date: May 2006
Location: Reading, Berkshire, UK. Going under cover.
Posts: 9684
Good Answers: 468
#6
In reply to #4

Re: 10 Bit ADC Math

09/14/2010 7:38 AM

I can't image what kind of single linear network would give those results.

Here are some circuits which should work:

(The above using stoney's suggestion of resistor values - which seems about right).

It's a bit trickier now you've introduced the negative rails. The ADC inputs (in fact the inputs to any pin) are limited to the range -0.5V to Vcc+0.5V, so you need something to bias the voltages into the positive range. The circuits below will do this (provided that the +5V rail is working as it should!):

The diodes are to prevent damage to the chip inputs if the +5V rail fails.

In each example, the voltage shown above "ADC IN" is the expected voltage when the monitored rail is at its nominal value.

I would suggest using the internal 2.56V reference in the chip.

__________________
"Love justice, you who rule the world" - Dante Alighieri
Register to Reply Good Answer (Score 2)
Associate

Join Date: Jan 2009
Location: Cape Town
Posts: 54
#7
In reply to #6

Re: 10 Bit ADC Math

09/14/2010 8:07 AM

Here's what I have in as my scaling circuit. I assumed that there's a way to calculate the ADC voltages so that "the ADC will recognise them" but from what I see you can design any circuit as long as your ADC input will be less than the reference voltage.

now, for my code, say I want to monitor if 5V is more than 5.65V....how to I do this using the if statement?

__________________
"I never expect to lose. Even when I'm the underdog, I still prepare a victory speech"
Register to Reply
Guru
United Kingdom - Member - Not a New Member Hobbies - Musician - New Member Hobbies - Fishing - New Member

Join Date: May 2006
Location: Reading, Berkshire, UK. Going under cover.
Posts: 9684
Good Answers: 468
#8
In reply to #7

Re: 10 Bit ADC Math

09/14/2010 8:33 AM

Using the 3k3 and 2k2 resistors for scaling, 5V in ≡ 2V to ADC. The ADC full scale is 1023, which occurrs when VADCIN = 2.56V.

By simple scaling, 5.65V in gives VADCIN = 5.65 X (2 ÷ 5) = 2.26V. This will result in an ADC count of 1023 X (2.26 ÷ 2.56) = 903.

So your code would read something like:

if (ADC_COUNT > 903) then
'Take appropriate action for +5V line overvoltage
endif

__________________
"Love justice, you who rule the world" - Dante Alighieri
Register to Reply Score 1 for Good Answer
Associate

Join Date: Jan 2009
Location: Cape Town
Posts: 54
#9
In reply to #8

Re: 10 Bit ADC Math

09/14/2010 8:44 AM

thanks JohnDG ... that's the kick start I needed!

__________________
"I never expect to lose. Even when I'm the underdog, I still prepare a victory speech"
Register to Reply
Associate

Join Date: Jan 2009
Location: Cape Town
Posts: 54
#10
In reply to #8

Re: 10 Bit ADC Math

09/14/2010 8:47 AM

is my linear network incorect?

__________________
"I never expect to lose. Even when I'm the underdog, I still prepare a victory speech"
Register to Reply
Guru
United Kingdom - Member - Not a New Member Hobbies - Musician - New Member Hobbies - Fishing - New Member

Join Date: May 2006
Location: Reading, Berkshire, UK. Going under cover.
Posts: 9684
Good Answers: 468
#11
In reply to #10

Re: 10 Bit ADC Math

09/14/2010 8:59 AM

Can't read the component values - but it looks OK. Using series resistors (to get non-standard values?) seems to be a bit of overkill, as small deviations from ideal input voltage values can be accommodated in the software. The zener diodes for generating the bias for the negative rails isn't a bad idea, as it makes the negative rail readings much less dependent on variations of the positive rail.

There are plenty of ways to skin a cat (sorry, Del).

__________________
"Love justice, you who rule the world" - Dante Alighieri
Register to Reply
Register to Reply 14 comments

Good Answers:

These comments received enough positive votes to make them "good answers".

"Almost" Good Answers:

Check out these comments that don't yet have enough votes to be "official" good answers and, if you agree with them, vote them!
Copy to Clipboard

Users who posted comments:

JohnDG (3); Randall (1); redfred (2); stoney (2); tobzn (5); Tornado (1)

Previous in Forum: Coronal Mass Ejections . . . What If??   Next in Forum: MOSFETs and Drivers

Advertisement