Forum Index
HomeZBasic Home   Forum RulesForum Rules   Forum FAQForum FAQ   MemberlistMemberlist   UsergroupsUsergroups   RSS FeedRSS Feed
Site SearchSite Search   LinksLinks   DownloadDownload   Digests and SubscriptionsDigests and Subscriptions
ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in   RegisterRegister
Using ADC to measure battery voltage

 
Post new topic   Reply to topic    Forum Index -> ZBasic Language
Author Message
victorf



Joined: 01 Jan 2006
Posts: 342
Location: Schenectady, New York

Posted: 19 February 2006, 14:18 PM    Post subject: Using ADC to measure battery voltage Reply with quote

Perhaps someone would help me out here. I have a 12 volt battery and i would like to measure the it's voltage from time to time. I have the following circuit set up on my system board:

VBAT ---R1------R2---GND

The junction between R1 and R2 goes to Pin 20 of my ZX-24. R1 = 8.06K 1% and R2 = 4.02K 1%. The values were chosen so that the voltage at Pin 20 ranged from 0 to 5V. If the voltage at Pin 2o is VADC and the voltage of the battery is VBAT, I wrote VADC = (VBAT * R2) / (R1 + R2). Using this equation I computed that for an VBAT of 15.02 volts I would see a VADC of 5 Volts and for a VBAT of 0 volts I would see VADC = 0 volts.

Here is my function to compute the battery voltage:
Code:

Function BatteryVoltage(byval adcpin as byte) as Single   'Compute battery voltage

'VBAT = (ADC count / 4096) * VMAX
'for R1 = 8.06k and R2 = 4.02k
'VMAX = (5V * (R2 + R1)) / R2 = 15.02
   
   dim VoltCount as integer
   VoltCount = GetADC(adcpin)
   Debug.Print "The ADC Count is: ";CStr(VoltCount)
   
   BatteryVoltage = (CSng(GetADC(adcPin)) / 4096.0) * 15.02   'volts
   
End Function   'BatteryVoltage


Running this I get VoltCount = 824 and a battery voltage of 3.03 which is about 1/4 of the voltage measured with my multimeter. Doesn't a 12 bit ADC resolve to 4096 counts? Thats why I used 4096 in the function.

Obviously my analysis is faulty Exclamation I could use some help here.

Any enlightenment will be appreciated. Confused

Vic
Back to top
pdubinsky



Joined: 25 Nov 2005
Posts: 66
Location: South Carolina

Posted: 19 February 2006, 15:38 PM    Post subject: Re: Using ADC to measure battery voltage Reply with quote

The ZX-24 ADC is 10 bit not 12 bit. You need to use 1024 NOT 4096. That will give you a final voltage of 12.12 (3.03 * 4096/ 1024).

Paul

victorf wrote:
Perhaps someone would help me out here.

<...>

Code:

Function BatteryVoltage(byval adcpin as byte) as Single   'Compute battery voltage

'VBAT = (ADC count / 1024) * VMAX ' <== Note Change
'for R1 = 8.06k and R2 = 4.02k
'VMAX = (5V * (R2 + R1)) / R2 = 15.02
   
   dim VoltCount as integer
   VoltCount = GetADC(adcpin)
   Debug.Print "The ADC Count is: ";CStr(VoltCount)
   
   BatteryVoltage = (CSng(GetADC(adcPin)) / 1024.0) * 15.02 'volts <== Note Change
   
End Function   'BatteryVoltage



Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Posts: 2499
Location: Portland, OR

Posted: 19 February 2006, 16:36 PM    Post subject: Reply with quote

In reality, a 10-bit ADC has only 1023 quantization levels (plus zero) so each step is equal to Vcc/1023, or 4.89mV per step at 5V.
Back to top
pdubinsky



Joined: 25 Nov 2005
Posts: 66
Location: South Carolina

Posted: 19 February 2006, 16:52 PM    Post subject: Reply with quote

I agree: 4.882813 mV per step is different than 4.887586 mV per step <gg>. I was staying with the "count" (4096) that Vic had used for 12 bit resolution - mostly so that the 4096 he used in his equation divided by 1024 would give an integer mulitplier of 4.

Thx for, as usual, keeping us on our toes(all 10 to the 1 of them).

C8-)

dkinzer wrote:
In reality, a 10-bit ADC has only 1023 quantization levels (plus zero) so each step is equal to Vcc/1023, or 4.89mV per step at 5V.
Back to top
victorf



Joined: 01 Jan 2006
Posts: 342
Location: Schenectady, New York

Posted: 19 February 2006, 21:09 PM    Post subject: Reply with quote

Here is the discussion of GetADC from the SysLib.

Quote:

This function performs an analog-to-digital conversion on the signal present on the specified pin which
must be one of the PortA pins (13-20). The return value will be a 12-bit digital approximation of the input
voltage with a range from zero to the processor supply voltage (usually +5 volts).



The way I read it the ADC operation can quantize the value on the pin in question to 12 bits which my calculator says is 4096 (2 exp 12). Confused What am I missing here. Is 12 bits NOT 12 bits Question

I agree with pdubinsky when he says using 1023 instead of 4095 (I guess this is the correct value) make my function return 12.2 volts which is proper. I just don't know why the divisor should NOT be 4095 as implied int the discussion of the function GetADC()

Any enlightenment will be appreciated.

Vic
Back to top
mikep



Joined: 24 Sep 2005
Posts: 765
Location: Austin, TX

Posted: 19 February 2006, 21:44 PM    Post subject: Reply with quote

I would suggest that the documentation has an error on both GetADC pages and it should read 10 not 12 bits. The underlying ATmega32 only supports 10 bits. I'm sure Don will fix this mistake asap. I never caught it in proof-reading the drafts either Sad
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Posts: 2499
Location: Portland, OR

Posted: 19 February 2006, 23:21 PM    Post subject: Reply with quote

Quote:
I would suggest that the documentation has an error on both GetADC pages and it should read 10 not 12 bits.


It is true that the documentation is in error. It should refer to 10 bit resolution.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Forum Index -> ZBasic Language Time synchro. with the server - Timezone/DST with your computer
Page 1 of 1

 


All content Copyright © 2005-2012 Elba Corp. All Rights Reserved.
Opinions expressed in posts are those of the author and not necessarily those of Elba Corp.
Powered by phpBB © 2001, 2005 phpBB Group