adc( pinname )

Syntax: adc( pinname)
Description: Reads an analog input input.
Parameters:  pinname Name of an input pin. The name should be in the form Px_y. Where x is the port number, and y is the bit number which is between 0 and 7. (e.g. P1_3). Only pins P1_0 through P1_7 can be used for the ADC input.
The input pinname can also be the name of TEMPERATURE which will measure the temperature of the chip. The input pinname can also be the name VCC which will measure the supply voltage for the chip.
(macro substituted pinname is allowed)
Returns: A 10 bit integer number in the range 0 through 1023.
Related: adc_enable()


Example:

adc_enable()                           # Enable the ADC block
while 1:
   val = adc( P1_4 )                   # Use the ADC to convert voltage at ppin P1_4 into a 10 bit number
   print( 'ADC P1_4 is:', val )
   wait( 1000 )

8 pins from P1_0 through to P1_7 can be measured.

The value returned is measured with reference to the chip reference voltage which is set to 2.5v

The voltage is is calculated using the following equation

V = (adc( pinname ) * 32) / xxxx    # result is in millivolts, (thousandths of a volt)

It is best to convert the number into millivolts because the we can only use integer arithmetic in mpy, and our voltage measurement would be rounded to the nearest volt.

If the name TEMPERATURE is used temperature of the chip will be measured. The temperature is calculated using the following equations

C = adc(TEMPERATURE) * xxxx / yyyy + zzzz  # this will give the temperature in Celcius
F = adc(TEMPERATURE) * xxxx / yyyy + zzzz  # this will give the temperature in Farenheit

If the name VCC is used then the power supply voltage of the chip will be measured. The actual voltage is calculated using the following equation

Vsupply = (adc(VCC) * 20) / 4  # the result will be in millivolts