what is ADC / readadc

westaust55

Moderator
ADC / readadc

ADC is an abbreviation/Acronym for "Analogue to Digital Conversion

READADC is a PIXACE BASIC command which reads the value of the analogue signal.

Within the picaxe, there are A to D converters that take an analogue signal and create a digital number as 8 bits for the range 0 to 255. Different Picaxe models have different numbers of ADC channels. See Manual 2 opage 120.

An analogue signal is a continuously/infinitely variable signal such as from a potentiometer connect between the supply (typically 4.5 to 5Vdc) and ground (0V).
By comparison, an 8-bit (digital) number can only hold 256 discrete steps as an equivalent value.
So if the range of the signal is 0 to 5Vdc, then 5V / 256 = 0.019V (= 19mV). The ADC must make an approximation and round values to match the available “steps”.

For READADC you need to specify the channel/pin to be used and a variable into which the digital value will be placed. So the command becomes:
READADC pin, variable

The later versions of PICAXE also have a READADC10 command which uses a 10 bit digital value for greater (4x ) resolution. Since the value is now more than 8 bits a word variable must be used to hold the number coming from the ADC channel.



EDIT:
You would use an ADC channel when you want to measure the signal from such things as:
1. potentiomenter (variable resistor - gives a voltage)
2. Light dependant resistor – resistor value varies with light intensity
3. voltage
4. current (needs to be transformed to a voltage
5. magnetic field strength (using a hall effect sensor)
6. other devices

If the output of these devices is not a voltage then it needs to be converted to a voltage for the PICAXE to analyse.
 
Last edited:

Michael 2727

Senior Member
Any varying resistance device, e.g. LDR, POT, Thermistor used in
a voltage divider arrangement ( another fixed resistor in series )
will vary the voltage at the junction point of the two components.

This is the point where the ADC input is taken from.
See Below -
 

Attachments

sputz

Member
Westaust, that was one of the best explanations describing a feature! In my opinion, very few people possess the skill to express technical information into easy-to-comprehend sentences, you are one of them!
 

GBlair

Member
I would have to agree! That is a very good explanation and I appreciate it very much.
I have read manual 1 and skipped around manual 3, I guess I should rip into manual 2 today and maybe tomorrow. Sometimes that manuals are very vague and assuming that we know what they are trying to tell us. This was a very good explanation and I am sure that I will have other questions.
Thanks
 

westaust55

Moderator
Any varying resistance device, e.g. LDR, POT, Thermistor used in
a voltage divider arrangement ( another fixed resistor in series )
will vary the voltage at the junction point of the two components.

This is the point where the ADC input is taken from.
See Below -
For the LDR example, yes you do need a resistor in series with the LDR to form a voltage divider.

For a potentiometer or voltage signal there is no need for the 10 kOhm resistor. The wiper of the potentiometer is effectively the voltage divider point/connection. This resistor will only reduce the range for the signal as input to the PICAXE will never get down to 0V so the lowest part of the 0 to 5V (or 0 to 255 into a variable) will not occur.
 

Michael 2727

Senior Member
Westaus55 a very good explanation for beginners, there should be MANY more of them.

The reason I posted was because I was not entirely happy with the last line, although true.
If the output of these devices is not a voltage then it needs to be converted to a voltage for the PICAXE to analyse.
Taken in the context of GB's original question (bare bones newbie, and there's nothing wrong with that )
I thought the quote may confuse some.
e.g. How the hell does an LDR or Thermistor change its voltage ? they are resistive.

Also the drawings were made to the Picaxe standard input specs, (in context)
that's not to say there are other ways to go about it. See attached .gif.

An internet search on Voltage Dividers and how they work would be a good
starting place to understand how various devices could be used with ADC inputs.

NOTE: Also that if a device is used on an ADC which has its own separate power supply
then if either supply (Picaxe / Device) is not regulated, the ADC can change if either
supply voltage changes. ( Battery - 3x AAA goes down etc )
The Picaxe uses its own supply for a Voltage Reference when making ADC readings.

Never put voltages above 5.5V onto any picaxe pin, read the Power Supply notes picaxe_manual1.pdf

EDIT: Tha attached .gif will allow ADC readings of the full range
0 to 255 to be registered, use the limiting resistor to avoid sadness. ;)
 

Attachments

Last edited:

Dippy

Moderator
ADC Speeds and source impedances (and their relationship) deserve a mention too if you want to be all-encompassing. These are very important aspects.

Like I said before with the recent MOSFET driving threads, a permanent linked pdf Data Sheet should be on this site for subjects like these. A nice description which could well be an expansion of WA's above with a bit more detail, some diagrams and examples.
Experinced people with plenty of spare time could easily knock one up, but it's that old permanent link issue again.

All this kind of documentation ought to be avaialble easily without having to search for old threads - or for people to spend hours Googling for others.

Come on Rev-Ed.... a Wiki .... but please call it a different name as I hate that one, how about a 'Piki'.
 

westaust55

Moderator
Okay, I have bitten the bullet and produced some notes on the effects of source impedance (only a small part of the total picture) and total time for a READADC operation.

While some of this was known to me from my past research, I have delved further. If others have input or corrections these can be added then I will merge it all into a PDF file with the info from the first post to try and keep it all together.

While the various PICAXE versions have differing numbers of ADC inputs, these are in fact multiplexed into a single sample and hold circuit for a single ADC module.

Multiplexing is like an electronic switch that rotates though each of the ADC input channels in turn making the analogue signal available to the actual sample and hold circuit and ADC module within the PICAXE (= Microchip PIC) chips.

In terms of timing there are several steps:
1. The input amplifier settling time
2. The time for the charge holding capacitor (CHOLD) to fully charged to the ADC channel input signal voltage level.
3. Time based upon the temperature coefficient
4. The timer to carry out a conversion from the analogue signal to the digital value
5. The time to transfer the result of the ADC to the PICAXE variable specified in the BASIC command.

Items 1,2 and 3 are the ADC acquisition time Tacq

The amplifier settling time is given in the datasheet as 2usec.

The charge holding capacitor time (TC) is a function of the resistance and is expressed as:
TC = –CHOLD * (RIC+RSS+Rs) * ln(1/2047)

Where:
Rs is the source impedance (a function of the external circuit),
Rss is the sampling switch impedance which varies with the voltage Vdd (10 kOhm at 3V, 8 kOhm at 4V and ~6.5 kOhm at 5V), and
Ric is the interconnect resistance, stated in datasheet as < 1 kOhm

The Microchip datasheets recommend a maximum value for RS of 10 kOhm.

So Tc can vary over a range depending upon supply voltage and source impedance.
With Vdd = 5V and Rs = 5 kOhm then Tc = 0.95usec
With Vdd = 4V and Rs= 10 kOhms then Tc = 1.45usec, and
With Vdd = 3V and Rs = 20 kOhms then Tc = 2.3usec

The Temperature Coefficient time TCOFF is stated as &#61480;Temperature - 25°C&#61481;&#61480;0.05&#956;s/°C&#61481;
So, if the PICAXE chip internally is at 50 DegC then TCOFF = (50-25) * 0.05 == > 1.25uSec

Therefore the total Tacq can vary but typically may be in the range 4usec to 6usec



The PICAXE uses the supply voltage Vdd (e.g. 4.5V or 5V) as a reference voltage against which the analogue to digital conversion is done.

The conversion time Tad for a 10-bit conversion is dependant on further factors.

The source of the conversion clock is software selectable via the ADCS bits of the ADCON0 register. ADCON0 is SFR 31 ($1F). Please consult the Microchip datasheet specific to your PICAXE before trying any changes to this SFR.
There are several possible clock options depending upon which PICAXE version used. Some typical options are:
FOSC/2
FOSC/8
FOSC/32
• FRC (dedicated internal oscillator)

The time to complete one bit conversion is defined as TAD. One full 10-bit conversion requires 11 TAD periods

The conversion time TAD for the clock settings relative to PICAXE speed based upon Microchip recommended ADC clock settings are (For the 08M and40X1 at least):

PICAXE Fosc = 4MHz and ADC Fosc/8 ==> 2.0uSec
PICAXE Fosc = 8MHz and ADC Fosc/32 ==> 4.0uSec

So based upon the full 10-bit conversion, the time to complete one A to D conversion will typically be 11 x 2 = 22usec. (when operating at 4MHz)
Note that this is the time to convert one analogue signal.

Internally the PICAXE interpreter will take some further unspecified time to transfer the result of each conversion into the nominated variable.

Based upon the time for each step, the toal time from initiating a READADC command to having the data available in the nominated variable can vary but values in the range 27usec to 30usec could be expected with the PICAXE operating at 4MHz.
These time will rise to 49usec to 51usec with the PICAXE operating at 8MHz and using recommended conversion clock settings.
 
Last edited:

ValueAdd

Senior Member
Westaust, that was one of the best explanations describing a feature! In my opinion, very few people possess the skill to express technical information into easy-to-comprehend sentences, you are one of them!
I agree entirely with sputz, a well presented description.

The second post on timing is getting deeper than I am currently into but I still get the idea how volts and resistance will change the timing.
 

westaust55

Moderator
If there are no further comments or obvious errors in what I have prepared, I will later today merge the text from both of my postings decribing the READADC command into a single document and generate a pdf file for future reference.
 

Michael 2727

Senior Member
Without ADC there would be NO digital music, no CDs, no i-Pods etc.
That should get their attention. :)
Analogue sound=voltage, convert it to a digital number, store it,
then convert it back to a voltage/sound with a DAC chip on playback.
 

Dippy

Moderator
Go for it WA.

Just a couple of points. I realise that much of it is a precis of the Microchip Data Sheet, but I think you may have to clarify the timings for PICAXE users. They may assume that the PICAXE READADC command takes about 30 microseconds.... and will be disappointed.

It might also be a good idea to separate what the 'standard' PICAXE ReadADC command does and any adjustments that can actually be made.
What I'm trying to say is the following:-
How much of the information you have provided is usable in PICAXE Basic?
Most of this info is really only usable for Assembler and/or fully compiled languages.

One thing which may be of use, and I couldn't find it in your description, is the ability to select different pins for your ADC reference voltage pins in ADCON1 (Ex: 9Fh<5,4> for 18X). I've never tried it with PICAXE and if it were possible with some PICAXEs it may useful for those using battery supplies.

And a quick spell check may be good as you'll be putting yourself as the Author, with acknowledgements to Microchip of course.
 

westaust55

Moderator
What is ADC and what does READADC do?

Hi all,

okay I have spent some time massaging and expanding on the two main posts I have written on ADC and READADC. The results are atatched as a pdf file for future reference.

There are other posts by others such as BCJKiwi which detail far more about the problems and solutions to inaccuracy/erro in particular when the PICAXE supply voltage is varying. I have not endeavoured to plagarise these threads but have made a direct refernce to one of the threads by BCJKiwi as further reading.

Hopefully newer users of the PICAXE and maybe some intermediate users will find this information useful and interesting.
 

Attachments

Last edited:

Mycroft2152

Senior Member
Westy,

Good writeup!

I put it into my Picaxe grimoire.

Myc

Note; Any expert on the forum has to have a "Y" in their name :)
 

hippy

Ex-Staff (retired)
For some PICAXE ( 18X, 16F88 ) there's also the ANSEL register which has to have bits enabled to make an input analogue rather than digital. READADC will automatically set the required bits but these may have to be user set if reading ADC's which aren't otherwise accessible, such as the 'output pins' of an 18X, AN5 and AN6.

Because AN5 and AN6 are digital output at reset it can be harder to use these as normal ADC inputs because current limiting resistors may be needed but they can be used for monitoring own +V supply and tetecting battery low using the top of a LED with cathode to 0V.
 

westaust55

Moderator
All PICAXE have the ANSEL register.

08M, 14M and 18X have the ANSEL register in the second bank of SFR so that it is accessible using PEEK and POKE commands.

20M, 28X1 and 40X1 have the ANSEL register in higher banks and cannot be accessed using BASIC commands.
 

120ThingsIn20Years

Senior Member
I had a 10k pot being read by a ADC pin, and discovered that the range of the pot was not quite 0-10k but the ADC values were 0-255. This made me think there was something I didnt understand, so I changed the pot to a 1k pot and still got the full 0-255 ADC range.

Have I got a messed up pot, or does READADC deal with this in some way I don't understand?
 
Last edited:

boriz

Senior Member
It may be worth noting here that the effective impedance of a linear pot (to ground) is equivalent to top resistance and bottom resistance in parallel. Exactly the same calculation for parallel resistors. EG:

10K pot with wiper in the middle. 5k top, 5k bottom, impedance is 2k5.
10K pot with wiper 75% towards the top. 2k5 top, 7k5 bottom, impedance is 1k875.

This of course applies to any form of resistive divider arrangement. EG: LDR for the top and fixed resistor for the bottom.
 

inglewoodpete

Senior Member
I had a 10k pot being read by a ADC pin, and discovered that the range of the pot was not quite 0-10k but the ADC values were 0-255. This made me think there was something I didnt understand, so I changed the pot to a 1k pot and still got the full 0-255 ADC range.

Have I got a messed up pot, or does READADC deal with this in some way I don't understand?
Step back and have a think about it. You are using a POTENTIOMETER.

If you have it connected correctly (I think you have), it is just a resistor connected between 0v at one end and +5v at the other. This resistor has a wiper that contacts the resistive element. At one end of its travel, the wiper will have a potential of 0v and at the other end, it will be at +5v.

What will happen if you replace your 10k potentiometer with one of 100 ohms? The current flowing through the resistive element will increase by 100 times (I=E/R 5/10000 = 0.5mA to 5/100 = 50mA) However, the potentials at each of the wiper positions will not change. The ADC input of the PICAXE is reading these potentials: from 0 (min of 0v) to 255 (max of +5v)
 

Dippy

Moderator
Just to put IP's description as a picture.

An ADC read is ratiometric.
So, for a pot of value R then if wiper is exactly halfway you can see that:
1. The resistance of the top half of the pot (wiper>+V) = the bottom half i.e. pot R/2.
2. This is equivalent to R1=R2
3. Therefore the voltage at that point = +V/2. (R1/R1+R2)
4. Typically the PICAXE ADC uses +V supply as the 'reference' it will see this as Vref/2 (V+/2)
5. Therefore the result will be ADCbits / 2.

Turn the wiper to the top and the voltage = V+ and ADC will see the result as ADCbits(max).
i.e. the volatge at the ADC input is the same as it's Vsupply/Vreference.
Turn the wiper right down and the voltage = 0 and ADC sees this as 0.

From this, you can see that the absolute resistance value of the wiper pot (mathematically) is unimportant.
It is a simple RATIO.

However, in the Real World, the value of the pot IS important.
1. Too small = smoking pot . I=V/R.
2. Too large = ADC misread as PIC sample cap often can't charge properly esp with fast ADCing.
For reasons that we'll leave for now, use 10K in most simple pot circuits.

Wel'll move onto the physics of the ADC hardware , source impedance and Thevenin's doo-dah at a later date.:)
(And I do hope no-one bombards you with this right now as you need to absorb one stage at a time).

You must have got to at least 50 of your 120 things right now ;)
 

Attachments

Dippy

Moderator
Haha, that didn't even cross my mind when posting.

Haha... that did make me titter... a true Laugh Out Loud ... nice one Pete! :) :)
 

hippy

Ex-Staff (retired)
I had a 10k pot being read by a ADC pin, and discovered that the range of the pot was not quite 0-10k but the ADC values were 0-255. This made me think there was something I didnt understand, so I changed the pot to a 1k pot and still got the full 0-255 ADC range.

Have I got a messed up pot, or does READADC deal with this in some way I don't understand?
Effectively a READADC measures how far you've turned the pot so it doesn't matter what the actual value of pot is. You can always turn a pot between 0% and 100% and the READADC reports that as 0 to 255, READADC10 as 0 to 1023.
 

120ThingsIn20Years

Senior Member
Dippy's picture make me say "Ahhh" and then, "hang on".

But I'll get there :)

I think I have all the info I need now to understand. I just have to re-read some stuff and look into voltage dividers.

Thanks people.

I cant wait to know some stuff :)
 

120ThingsIn20Years

Senior Member
Step back and have a think about it. You are using a POTENTIOMETER.

If you have it connected correctly (I think you have), it is just a resistor connected between 0v at one end and +5v at the other. This resistor has a wiper that contacts the resistive element. At one end of its travel, the wiper will have a potential of 0v and at the other end, it will be at +5v.

What will happen if you replace your 10k potentiometer with one of 100 ohms? The current flowing through the resistive element will increase by 100 times (I=E/R 5/10000 = 0.5mA to 5/100 = 50mA) However, the potentials at each of the wiper positions will not change. The ADC input of the PICAXE is reading these potentials: from 0 (min of 0v) to 255 (max of +5v)
And now strangely, this makes sense! :)

I love the world.

It behaves just as it should every time after all :)
 

John West

Senior Member
Just to be clear on one small point:
Analogue and analog are the same thing, a continuous and quite possibly varying signal.
 

cravenhaven

Senior Member
Getting back to Westy's dissertation on ADC's, can information about how to use the CalibADC (CalibADC10) be included. I thought the output from this command should be used to calibrate the ADC readings against a varying supply voltage but everytime I start to think about it I get confused about how to include it in my calculations.

Just found a very good explanation from Hippy.:
http://www.picaxeforum.co.uk/showthread.php?19217-monitor-a-picaxe-s-08m2-circuit-s-batteries-power-using-code&highlight=calibadc
 
Last edited:
Top