readADC

giuseppe

Member
Hello I have a question about the readADC command and the circuit for it. I understand you can attached a potentiometer to the ADC pin and get some value out of 256 but how can I know what value it will be? Say we took a 1k resistor and hooked it up to the pin and read its value with readADC, what value would I get? What if I used a 2k instead, would it bedouble the value ? or is there a set value internally for th epicaxe for a 2k resistor? Is there a table or a formula I can refer to so I can predict the value before actually empirically obtaining it? Thanks.
 

goom

Senior Member
readADC measures the voltage applied to the pin specified. It will return a reading of 256 when the applied voltage is the same as the PICAXE supply voltage, a value of zero if the pin is connected to ground, and a linear range for voltages in between (e.g. 128 for 2.5v with a 5V supply voltage).
If you connect a potentiometer to the supply voltage on one leg, and ground on the other, with the central (wiper) leg to the PICAXE ADC pin, then rotating the potentiometer will give readings between 0 and 256.
If you simply connect a resistor to the ADC pin, it will read whatever voltage is on the other end of the resistor. Do not connect to a voltage greater than the supply voltage or you may release the "magic smoke", i.e. destroy the PICAXE, or at least that particular input.
ReadADC10 (for those microcontrollers that support it) is similar, but gives finer resolution (0 to 1024). You will need a word variable to store the reading.
 

westaust55

Moderator
Hello I have a question about the readADC command and the circuit for it. I understand you can attached a potentiometer to the ADC pin and get some value out of 256 but how can I know what value it will be? Say we took a 1k resistor and hooked it up to the pin and read its value with readADC, what value would I get? What if I used a 2k instead, would it bedouble the value ? or is there a set value internally for th epicaxe for a 2k resistor? Is there a table or a formula I can refer to so I can predict the value before actually empirically obtaining it? Thanks.
While you mention initially a potentiometer, you then proceed to discuss a single resistor such as 1K Ohm or 2 kOhm.

A single resistor does not give a voltage divider that thus typically you will tie one end to ground or +5V so that is what the analog input will see and register as (0 or 255 respectively).

Going back to your potentiometer, the voltage at the PICAXE input and thus the value you will receive from the READADC command is a function of the ratio between the resistances on either side of the potentiometer wiper contact.

The READADC value uses the Vcc (PICAXE supply voltage) as a reference so the value can vary with the PICAXE voltage if the input voltage is using a different supply source.

By way of example/calculation:

The potentiometer voltage = Vcc
The potentiometer total resistance (across outer terminals) = 10 kOhm (10,000 Ohms)

The potentiometer wiper is at 60% of the travel towards the Vcc terminal

So resistance ground to wiper = Low_Side_Res = 10,000 * 0.6 = 6,000 Ohms
The resistance wiper to Vcc = High_Side_Res = 10,000 * (1 - 0.4) = 4,000 Ohms

Then the input volatge to the PICAXE ADC pin = Vcc * Low_Side_Res / ( Low_Side_Res + High_Side_Res)

Vin = 5 * 6000 / (6000 + 4000) = 3.0 Volts


READADC value (within accuracy) = Vin * 255 / PICAXE Vcc
= 3 * 255 / 5 = 153
 
Last edited:

westaust55

Moderator
readADC measures the voltage applied to the pin specified. It will return a reading of 256 when the applied voltage is the same as the PICAXE supply voltage, a value of zero if the pin is connected to ground, and a linear range for voltages in between (e.g. 128 for 2.5v with a 5V supply voltage).
If you connect a potentiometer to the supply voltage on one leg, and ground on the other, with the central (wiper) leg to the PICAXE ADC pin, then rotating the potentiometer will give readings between 0 and 256.
If you simply connect a resistor to the ADC pin, it will read whatever voltage is on the other end of the resistor. Do not connect to a voltage greater than the supply voltage or you may release the "magic smoke", i.e. destroy the PICAXE, or at least that particular input.
ReadADC10 (for those microcontrollers that support it) is similar, but gives finer resolution (0 to 1024). You will need a word variable to store the reading.
READADC has a range of 256 steps which equates to values from 0 to 255.

Likewise READADC10 ghas a range of 1024 steps from 0 to 1023.
 

Dippy

Moderator
It's a pity there isn't half a page devoted to ADCs and potential-dividers and the simple equation to convert ADC value to volts.
But look at how LDRs are connected in Manuals 1 and 3.

It should also be stressed that the quality of the ADC value depends on the quality of the Vsupply to PICAXE and other things.

Giuseppe, think of the ADC as a voltmeter.
It behaves very much like one.

So, get your DMM out and set it to volts.
If you connected +5V --- 2kRes ---- RedDMMProbe & BlackDMMProbe to--- Ground/0V what would it display?
Replace 2kres with 10kRes and measure, what would it show?

Read the previous posts about voltage divider and calculations.
If you connect a poptentiometer between 5V and ground and then measure with your DMM between Wiper and ground you can see the voltage. The voltage will vary as you twiddle the Pot. - it is a variable potential divider.
That is the basis for the name 'potentiometer'.


This also shows, as a double check, how you can use your DMM to check the volt level going into the PICAXE ADCx pin.
REMEMBER for the future: the volts going into the PICAXE ADC pin must never be greater than the supply voltgae to PICAXE (typically 4.5 to 5V DC).
 

celticones

New Member
* symbol meaning

hi this calculation is very relevent to my project at present , but i dont understand what the * symbol means in the calculations.
Iam sure theres a very obvious answer:)
 

westaust55

Moderator
hi this calculation is very relevent to my project at present , but i dont understand what the * symbol means in the calculations.
Iam sure theres a very obvious answer:)
Please read PICAXE manual 2 page 20:

+ ; add
- ; subtract
* ; multiply (returns low word of result)
** ; multiply (returns high word of result)
/ ; divide (returns quotient)
// % ; modulus divide (returns remainder)​

and there are many more . . . . . maths functions
 

BeanieBots

Moderator
The number returned by the ReadADC function will be:-
The voltage on the ADC pin multiplied by 256 divided by the supply voltage.
For example 2.5v would give 2.5 X 256 / 5 = 128. (for a 5v powered PICAXE).

You will also see (as pointed out by Dippy) how important the supply voltage is in the equation.
 

hippy

Ex-Staff (retired)
Actually 255 :)

ReadAdcReading = ( Vin / Vpsu ) * 255

ReadAdc10Reading = ( Vin / Vpsu ) * 1023
 

westaust55

Moderator
READADC (and READTEMP)

Seems it might be worthwhile to actually explain in the PICAXE manual 2, at the relevant pages, exactly what 8-bit 10-bit and 12-bit represents in the way of values under say the READTEMP, READADC, READADC10 and READTEMP12, command sections.

Several senior members here are getting it wrong in the first instance (even after it was clarified at post 3/4) – so what chance does a newbie have? :confused:
 

premelec

Senior Member
Calibadc

I note that X2 parts have a function CALIBADC which refers to an internal reference voltage [man2 p223] which can be used to correct for chip power voltage supply drift- interesting but I haven't used this function... yet...
 

MartinM57

Moderator
I've been bitten badly in the past by CALIBADC (I'm sure a search would find the post) and found that the results of using CALIBADC resulted in reducing the accuracy of the READADC readings. IIRC even Technical agreed that was the case - all to do with the internal reference voltage inside the chip having a relatively huge tolerance around its defined value.

I think that was before X2 days however, so things might be different with the new underlying PIC chips...
 

westaust55

Moderator
I've been bitten badly in the past by CALIBADC (I'm sure a search would find the post) and found that the results of using CALIBADC resulted in reducing the accuracy of the READADC readings. IIRC even Technical agreed that was the case - all to do with the internal reference voltage inside the chip having a relatively huge tolerance around its defined value.

I think that was before X2 days however, so things might be different with the new underlying PIC chips...
here is the thread Martinm57 refers to:
http://www.picaxeforum.co.uk/showthread.php?t=8559
 

hippy

Ex-Staff (retired)
Post #33 is key, repeated here ...

After a long search I found this, AN1072, "Measuring VDD Using the 0.6V Reference" -

http://ww1.microchip.com/downloads/en/AppNotes/01072A.pdf

For the 16F690 specifically but almost certainly applies to other PICmicro devices with an internal voltage reference. The crucial, revealing sentence ...

There is no way to know exactly what voltage the reference VP6 is before using the device. However, by performing a few calibrations, the value of the 0.6V reference can be removed from the measurement computation.

So, in a nutshell, that 0.6V voltage reference isn't a 0.6V voltage reference and it needs to be calibrated to be used ( details in AN1072 ). Better yet; the 'fixed reference' may alter as supply voltage and temperature changes so an average has to be used to minimise conversion error.
 
Last edited:

premelec

Senior Member
Ref stability for CALIBADC

I would think that the silicon for the ca 1v ref is similar to what's in an LM317 and other such devices and likely _could_ have pretty good stability with temperature and time. I understood the need to use it as a calibration reference like you might use with an external reference - which has been discussed many times on this forum [e.g. to measure battery voltage running the 'AXE].

So the issue is - "does the CALIBADC ref in a 20X2 [my case...] " perform well... :) In time I'll find out - meantime if someone knows for sure please share! Thanks...
 

Andrew Cowan

Senior Member
I've played with CLIBFREQ with a 20X2, and had good results. However, it was using a cheapo multimeter, so that adds an inacuracy of 10/20% :).

A
 

giuseppe

Member
Thanks a bunch guys. I understand how it works now. The example calculations really help a lot and are much appreciated. I am planning to use this feature to poll for 12 buttons by having them send a resistance when pressed. They are not matrixed. The buttons are the buttons on a xbox 360 controller and used to program and save moves via the user for an arcade controller i am building. So I figured I could use one input for a large button poll. I guess the only problem I really have is the tolerance of the resistors I am using and the temperature conditions of the resistors since the change in temperature could change the resistance enough to read as the wrong button. Thanks again.
 
Top