There have been a number of recent posts concerning accuracy, resolution, battery level sensing and so on.
ReadADC(10) is described as ratiometric and referenced to the PICAXE supply voltage. This means that as the supply voltage changes, the ADC value of the analogue sensor being read will also change. This works well for simple analogue devices that function as a voltage divider would and are operated from the same supply as the PICAXE.
However if the analogue output from the device being monitored is independent of the PICAXE supply Voltage, this is no longer the case.
There are four different scenarios to be considered.
1. Fixed supply, voltage divider type sensor from PICAXE supply
2. Fixed supply, sensor output from separate (stable) supply
3. Changing supply, voltage divider type sensor from PICAXE supply
4. Changing supply, sensor output from separate (stable) supply
Cases 1 & 2 work as expected as the the ADC value will be consistent as the voltage of both the PICAXE and the device do not change.
Case 3 also works as expected as the supplies to the PICAXE and the analogue device both change together and the ratiometric effect means the ADC value will be the same for the same set point on the sensor even though the voltage has changed.
Case 4 however is different as the voltage delivered from the analogue device will be constant for a constant set point, but the PICAXE will read a different ADC value as it's supply voltage changes. A compensating calculation will be required for this arrangement.
Different calculations will be needed depending on whether the voltage or the ADC is required as the results of readADC may be used for different purposes;
a. Acted upon in further calculations - e.g.
shut down if supply too low,
turn on an indicator,
where the compensated ADC value is of interest,
or,
b. Used to indicate the voltage - e.g.
Battery level indicator.
where the compensated voltage is of interest.
It may be argued that no supply is fixed as even with regulated supplies, small variations will occur with changing loads and creep with temperature changes and shifting component values over time. When considering whether the additional overhead in program size and speed is worth the effort, the amount of change in PICAXE supply and the effect of integer math on every step of compensating calculations should be carefully considered as significant variations may arise in the math introducing inaccuracies which may be many times greater than minor variations in supply.
So how would we make the adjustment should we wish to do so?
A known fixed voltage reference is required, against which to compare the supply, and from which to make the adjustment.
The reference can be obtained either from
1. CalibADC(10) if an X1 chip is used,
or,
2. an external reference which requires an additional ADC port.
A 28X1 was used in developing the following code.
It was found that the 0.6V specified as the reference voltage was not very precise, varying from chip to chip by up to 8%.
It was further found that this value also changed with supply voltage.
These variations would be acceptable for establishing a cut off for a battery level but not very accurate for sensor compensation.
Also it was found that a simple Resistor/Diode reference was similarly affected.
In developing the following code, separate batteries were used for both the reference voltage, and the independent analogue signal.
The following circuit is an example of the Resistor/Diode reference. Note, that a relatively low value resistor must be used to drive sufficient current through the Diode to ensure it has a reasonably stable volt drop.
It was found in developing these code snippets, that a variety of techniques had to be applied to obtain usable results.
The issue is integer math.
Whenever a multiplication occurs, the result must be less than 65535, the maximum value of a word variable, else overflow will occur.
With Division, the result must contain enough significant digits to provide the accuracy/resolution for the application.
A number techniques have been applied but even so, some forms of calculation are not as precise as one might want.
a. Pre-calculate constants to provide suitably scaled numbers for subsequent use.
b. Multiply the number by a suitable factor to increase its size before dividing (and divide by the same factor later).
c. Use the modulus function to get the numbers after the decimal
d. Use 32bit math.
There are two blocks of code supplied. They show the use of these different techniques.
They also differ in that;
one block uses a reference voltage of 1.58V with an analogue sensor of 3.17V
The other uses a reference voltage of 3.17V with an analogue sensor of 1.58V
These code blocks are not fully interchangeable as some techniques provide better results that others depending on the size of the constants used (which depend on the size of the reference voltage).
The two blocks of code follow in the next two posts.
ReadADC(10) is described as ratiometric and referenced to the PICAXE supply voltage. This means that as the supply voltage changes, the ADC value of the analogue sensor being read will also change. This works well for simple analogue devices that function as a voltage divider would and are operated from the same supply as the PICAXE.
However if the analogue output from the device being monitored is independent of the PICAXE supply Voltage, this is no longer the case.
There are four different scenarios to be considered.
1. Fixed supply, voltage divider type sensor from PICAXE supply
2. Fixed supply, sensor output from separate (stable) supply
3. Changing supply, voltage divider type sensor from PICAXE supply
4. Changing supply, sensor output from separate (stable) supply
Cases 1 & 2 work as expected as the the ADC value will be consistent as the voltage of both the PICAXE and the device do not change.
Case 3 also works as expected as the supplies to the PICAXE and the analogue device both change together and the ratiometric effect means the ADC value will be the same for the same set point on the sensor even though the voltage has changed.
Case 4 however is different as the voltage delivered from the analogue device will be constant for a constant set point, but the PICAXE will read a different ADC value as it's supply voltage changes. A compensating calculation will be required for this arrangement.
Different calculations will be needed depending on whether the voltage or the ADC is required as the results of readADC may be used for different purposes;
a. Acted upon in further calculations - e.g.
shut down if supply too low,
turn on an indicator,
where the compensated ADC value is of interest,
or,
b. Used to indicate the voltage - e.g.
Battery level indicator.
where the compensated voltage is of interest.
It may be argued that no supply is fixed as even with regulated supplies, small variations will occur with changing loads and creep with temperature changes and shifting component values over time. When considering whether the additional overhead in program size and speed is worth the effort, the amount of change in PICAXE supply and the effect of integer math on every step of compensating calculations should be carefully considered as significant variations may arise in the math introducing inaccuracies which may be many times greater than minor variations in supply.
So how would we make the adjustment should we wish to do so?
A known fixed voltage reference is required, against which to compare the supply, and from which to make the adjustment.
The reference can be obtained either from
1. CalibADC(10) if an X1 chip is used,
or,
2. an external reference which requires an additional ADC port.
A 28X1 was used in developing the following code.
It was found that the 0.6V specified as the reference voltage was not very precise, varying from chip to chip by up to 8%.
It was further found that this value also changed with supply voltage.
These variations would be acceptable for establishing a cut off for a battery level but not very accurate for sensor compensation.
Also it was found that a simple Resistor/Diode reference was similarly affected.
In developing the following code, separate batteries were used for both the reference voltage, and the independent analogue signal.
The following circuit is an example of the Resistor/Diode reference. Note, that a relatively low value resistor must be used to drive sufficient current through the Diode to ensure it has a reasonably stable volt drop.
Code:
V+ ----/\/\/\/\----+----|>|---- V0
R = 1k | BAT85
|
|
ADC
Reference Voltage
ReadADC10 here in place of CalibADC10
The issue is integer math.
Whenever a multiplication occurs, the result must be less than 65535, the maximum value of a word variable, else overflow will occur.
With Division, the result must contain enough significant digits to provide the accuracy/resolution for the application.
A number techniques have been applied but even so, some forms of calculation are not as precise as one might want.
a. Pre-calculate constants to provide suitably scaled numbers for subsequent use.
b. Multiply the number by a suitable factor to increase its size before dividing (and divide by the same factor later).
c. Use the modulus function to get the numbers after the decimal
d. Use 32bit math.
There are two blocks of code supplied. They show the use of these different techniques.
They also differ in that;
one block uses a reference voltage of 1.58V with an analogue sensor of 3.17V
The other uses a reference voltage of 3.17V with an analogue sensor of 1.58V
These code blocks are not fully interchangeable as some techniques provide better results that others depending on the size of the constants used (which depend on the size of the reference voltage).
The two blocks of code follow in the next two posts.
Last edited: