ADC question

eddydde

Member
This code I refer to was originally posted here http://www.picaxeforum.co.uk/showthread.php?t=3179.

My modified version of this code is this:

Code:
edit:


symbol reading = w0
symbol reading1 = w1
symbol gain = 192
symbol zero = 10

loop1:
readadc 0, reading
reading = reading * gain / 100 + zero
reading1 = reading / 100
reading = reading//100
sertxd ("Voltage is ",#reading1,".",#reading,"", 13,10)
goto loop1
My question is what is the purpose of the 'gain = 192' entry. Changing this value makes a large change in the final results. Is this value, 192 a pre-calculated value or does it relate to all PICAX's? It's a real mystery to me.

eddydde
 

westaust55

Moderator
READADC is only an 8 bit value, so maximum value is 255

Reading is a 16 bit value, so maximum value is 65535

the "gain' variable is a scaling factor.
In this case effectively multiplying by 1.92 when the divide by 100 in the calculations is also considered.

so at the max ADC value,
reading = 255 * 192 / 100 + 10
so the answer is 499

and the output to the PE terminal window is 4.99 Volts rather than a somewhat obscure number like "255"

So the 5V max input to a PICAXE ADC input channel is represented by the ADC variable as 255 and scaled to approx 5, so the reading to the terminal window shows the actual value as if a multimeter were used. :)
 

BeanieBots

Moderator
Just to expand a little on what Westy has explained.

It appears that the voltage in that example comes from something which requires both gain and offset. That is, it needs to be scaled and offset.

The normal way of doing that would be with an equation of the form:-
y=m*x + c
However, to make that work with PICAXE integer maths, it's done the way Westy has explained.
 

Benjie

Senior Member
Nice information!
If I understood correctly the offset of 10 is related to a particular condition of the considered application. In general case if I want to measure a voltage ranging from 0 to the supply voltage of 5.0V, the gain should be 1957 and divided by 1000 (approximate result).
thanks
 

BeanieBots

Moderator
You have the right idea but you will have problems with those numbers.
The largest reading from ReadADC will be 255.
255*1957 = 499035 which is too big for a word variable so it will overflow and give the wrong answer.
You could use 255*196 = 49980.
As you can see, this is a little short of 50000 so to avoid rounding errors, it wouldn't hurt to put a little offset of maybe 20 or even 30, but do the offset BEFORE the divison.

255*196 + 30 / 100 = 500
0*196 + 30 / 100 = 0 (because 30/100 = 0)

Alternatively, you could use a slightly different multiplier
eg ADCvalue * 199/101 would give 502 for a 5 volt input.

For 10-bit use ReadADC10 which gives a maximum of 1023.
The largest multiplier you could use would be 64.
 
Last edited:

hippy

Ex-Staff (retired)
Things can get a whole lot easier using an adjustable regulator and running the PICAXE at 5.1V for READADC and 5.115V for READADC10.
 

BeanieBots

Moderator
That's just cheating Hippy;)
Besides, how often is it ever actually required to read in a 0v to 5v signal and display it as 0.00v to 5.00v? More often than not, the voltage represents something which needs to be scaled into other units. Also, many (if not most) sensors do have some sort of offset that needs to be removed.
 

westaust55

Moderator
Also, many (if not most) sensors do have some sort of offset that needs to be removed.
Very true.
Many Industrial Instruments are 4 to 10mA intended to go into a PLC or DCS where a resistor (eg 250 Ohms) converts that to 1 to 5 Volts. Other instruments are directly 1 to 5V output as well.

While other instruments are 0 to 10 Volt etc so no offset required.

Then there is the case with some "non-smart" instruments ( smart = Hart Protocol, etc) where an instrument internal error/fault was indicated by taking the current to 22 mA = 5.5Volts at PLC/DCS input.
 
Last edited:
Top