# Battery Voltage Scaling

#### GeorgeB

##### Member
Hello everyone

I'm trying to scale an analogue input ranging from 153 to 168 where 153 corresponds to 100% and 168 corresponds to 0% but the point is when trying to scale them by the usual way you would encounter two of picaxe's major problems which are negative numbers and fractions. Did anyone encounter this before or has a solution for it?

#### erco

##### Senior Member
B1=168-B0 'NOW B1=RANGE 0-15
W1=B1*100/15

#### GeorgeB

##### Member
but I want a bigger precision than that. This gives 6 percent for each step I want at least 1% for each step!!

#### hippy

##### Technical Support
Staff member
How are you going to get greater precision when you only have 16 values representing 0% through 100% ... ?

153 100%
154 93%
155 86%
156 80%
157 73%
158 66%
159 60%
160 53%
161 46%
162 40%
163 33%
164 26%
165 20%
166 13%
167 6%
168 0%

The only way to have greater precision in the percentage result is to have a larger number range input.

You can achieve that using READADC10 if you are using READADC and there may be other hardware tricks available if you could let us know what your circuit is.

#### GeorgeB

##### Member
How are you going to get greater precision when you only have 16 values representing 0% through 100% ... ?

153 100%
154 93%
155 86%
156 80%
157 73%
158 66%
159 60%
160 53%
161 46%
162 40%
163 33%
164 26%
165 20%
166 13%
167 6%
168 0%

The only way to have greater precision in the percentage result is to have a larger number range input.

You can achieve that using READADC10 if you are using READADC and there may be other hardware tricks available if you could let us know what your circuit is.
it is just a 360 ohm resistor with a 2.7 V zener as a reference connected to one of the pins. all I want is a way to measure the battery voltage powering the pic with a fixed reference voltage that's all. so if it drops below 4 V that's 0% for me and 4.5 V is 100%.
Do you have a better idea??!!

#### BESQUEUT

##### Senior Member
it is just a 360 ohm resistor with a 2.7 V zener as a reference connected to one of the pins. all I want is a way to measure the battery voltage powering the pic with a fixed reference voltage that's all. so if it drops below 4 V that's 0% for me and 4.5 V is 100%.
Do you have a better idea??!!
Please, can you say what percentages do you expect for each of the 16 possibles values ?

#### AllyCat

##### Senior Member
Hi,
it is just a 360 ohm resistor with a 2.7 V zener as a reference connected to one of the pins.
A "classic" 2.7 volt Zener diode is almost useless as a reference voltage, with far too high a source resistance and poor temperature coefficient. There are of course integrated circuits that emulate a "zener" (shunt) reference, including the FVR_2048 (2.048v Fixed Voltage Reference) within most PICaxes.
.. all I want is a way to measure the battery voltage powering the pic with a fixed reference voltage ....
Do you have a better idea??!!
Certainly.
In that case you should be able to use the CALIBADC10 facility within PICaxe Basic.
If that's not high enough resolution, then I've posted several alternative methods in the Code Snippets section:

Cheers, Alan.

#### hippy

##### Technical Support
Staff member

So you can get almost a four-fold increase in resolution by going to READADC10 rather than using READADC which I guess you are currently using.

Code:
``````ReadADc10 BATTERY, w0
w1 = w0 Max 690
w1 = 690 - w1 * 100 / 77
SerTxd( #w1, "%", CR, LF )``````

#### premelec

##### Senior Member
if for some reason you want to use an external ref check out the TLV431 adjustable shunt 'zener'

#### GeorgeB

##### Member
Thank you that's a lot more than what I actually expected I'll have a read through it but I'm sure it pretty much has everything I want

#### mikeyBoo

##### Senior Member
hi George,
If you are really determined on getting full 10-bit resolution across the range of 4.0v to 4.5v it is possible. However, it would require adding some external hardware. The way it’s done is by subtracting 4.0v from the bottom of your scale & then scaling the remaining 0.5v (i.e. 4.5 - 4.0) to be 0 …+2.047v (= internal Picaxe Vref) into a Picaxe analog input. I have done something similar because I wanted to use the AC712-30 sensors for amp readouts but was not interested in measuring negative current (meaning I had to subtract ~2.5v from the bottom of scale).

I scaled for 0…30 Amps = 0…+3.0 Vout so I could read amps directly on a DVM. I believe I changed Rz to 1.5Meg for this scaling. The 0..+3.0v was then scaled to Picaxe 0…+2.047v into the Picaxe AIN. Why did I use the ACS712s rather than shunts? Well they were dirt cheap on Amazon (sorry, no fancy technical reason).

If it’s really important enough for you to go to that much trouble, see the attached .pdf. If anyone is interested enough, I could post the project. There are some situations where it’s useful to “saw off” the bottom portion of an incoming voltage & scale the remaining voltage to fit the 10-bit resolution offered by the Picaxe. The Picaxe AIN IS very accurate.

Note that since you want to subtract 4.0v (rather than 2.5v) the values in the attached .pdf might have to be re-scaled.
Depending on your background, this might be somewhat interesting or give you a headache.

#### Attachments

• 13.1 KB Views: 23

#### GeorgeB

##### Member
hi George,
If you are really determined on getting full 10-bit resolution across the range of 4.0v to 4.5v it is possible. However, it would require adding some external hardware. The way it’s done is by subtracting 4.0v from the bottom of your scale & then scaling the remaining 0.5v (i.e. 4.5 - 4.0) to be 0 …+2.047v (= internal Picaxe Vref) into a Picaxe analog input. I have done something similar because I wanted to use the AC712-30 sensors for amp readouts but was not interested in measuring negative current (meaning I had to subtract ~2.5v from the bottom of scale).

I scaled for 0…30 Amps = 0…+3.0 Vout so I could read amps directly on a DVM. I believe I changed Rz to 1.5Meg for this scaling. The 0..+3.0v was then scaled to Picaxe 0…+2.047v into the Picaxe AIN. Why did I use the ACS712s rather than shunts? Well they were dirt cheap on Amazon (sorry, no fancy technical reason).

If it’s really important enough for you to go to that much trouble, see the attached .pdf. If anyone is interested enough, I could post the project. There are some situations where it’s useful to “saw off” the bottom portion of an incoming voltage & scale the remaining voltage to fit the 10-bit resolution offered by the Picaxe. The Picaxe AIN IS very accurate.

Note that since you want to subtract 4.0v (rather than 2.5v) the values in the attached .pdf might have to be re-scaled.
Depending on your background, this might be somewhat interesting or give you a headache.

Thanks a lot mikeyBoo I'll have a look at it and see thanks for helping.

#### johnlong

##### Senior Member
Hi
Dont wish to high jack the thread
just a quick question had a look at Allycats post#7
down loaded code there for use with lithium ion battery
but unfortunatly it scrambled all the results from the phote ressistor,htu21d temp %rh sensor and the infer-red sensor
look to the manual CALIBADC10 it states that the function can not be used with 28x2 and 40x2 (5v) is this correct
I ask because when I run the code independently it reads the battery to within 0.1 +- volt correctly
regarde john

#### AllyCat

##### Senior Member
Hi,
it scrambled all the results from the phote ressistor,htu21d temp %rh sensor and the infer-red sensor
If you are making other ADC measurements, then you may need to reset the voltage reference to the default mode (ratiometric - relative to the supply rail) with the ADCCONFIG command.
look to the manual CALIBADC10 it states that the function can not be used with 28x2 and 40x2 (5v) is this correct
Sorry, I don't use X2s myself, but the Command Reference certainly appears to say that CALIBADC{10} cannot be used with those two X2s. For the others, CALIBADC10 should work, but my "(M2) Alternative" and the higher-numbered version (CALIBADC12 - 15) subroutines need to use both the FVR and the "DAC" (voltage divider) , and I believe there is an "issue" with how one of those behaves in (all) X2s.

You might find that simply accumulating (adding together) multiple CALIBADC10 measurements (or averaging the voltage calculated from each) gives some additional resolution.

Cheers, Alan.

#### hippy

##### Technical Support
Staff member
look to the manual CALIBADC10 it states that the function can not be used with 28x2 and 40x2 (5v) is this correct
I ask because when I run the code independently it reads the battery to within 0.1 +- volt correctly
I think this may be a misinterpretation of the manual, which is not referring to universal voltage 28X2 and 40X2 chips being run at 5V.

This is because the 28X2-5V and 40X2-5V chips do not have FVR hardware, so there is no reference voltage to read.

#### inglewoodpete

##### Senior Member
I think this may be a misinterpretation of the manual, which is not referring to universal voltage 28X2 and 40X2 chips being run at 5V.

This is because the 28X2-5V and 40X2-5V chips do not have FVR hardware, so there is no reference voltage to read.
Perhaps a further explanation is needed. The 28X2-5V and 40X2-5V were based on the PIC18F2520 and PIC18F4520 chips, (PICAXE Models) which were available for a relatively short period several years ago. The later (superior) universal voltage 28X2 and 40X2s that hippy refers to, have been supplied for several years now and are based on the PIC18F25K22 and PIC18F45K22 chips respectively.

Have a look at the laser engraving on your 28X2s or 40X2 and you will most likely find they are the later and more capable universal voltage versions, supporting the CALIBADC/10 commands.

Last edited:

Hi