Simple Analog Input

Share on facebook
Share on twitter
Share on linkedin
Share on email
Share on print
Table of Contents

This example demonstrates how to configure a P2 I/O pin to sample and capture an analog input, and display the raw values using DEBUG.

' Program reads an ADC and displays raw count
  _clkfreq = 100_000_000                'set clock frequency
  _pin     = 0                          'do conversion on this pin
PUB go() | i
  pinstart(_pin, p_adc_1x | p_adc, 13, 0) 'init ADC pin
  repeat                                 'print sample set every 100ms
    i := rdpin(_pin)
    debug(sdec_(i), dly(100))

The PINSTART method is used to configure the I/O pin, in this case for the ADC mode, which is described in the Propeller 2 Spin2 Documentation:

PINSTART(PinField, Mode, Xval, Yval)

The arguments used in the method are:

pinstart(_pin, p_adc_1x | p_adc, 13, 0) 'init ADC pin

The pipe symbol (|) is a logical OR operator and merges the low-level 1x magnification ADC pin configuration with the ADC smart pin mode. The 13 selects 14-bit sampling. The 0 is only a filler for the unused Yval parameter.

The RDPIN command obtains the ADC sample value and puts it into the variable i:

i := rdpin(_pin)

which is output to a DEBUG display with a 100 ms delay between values:

debug(sdec_(i), dly(100))

The values are raw numbers which are proportional to the voltage. To display millivolts you need to record the I/O pin’s values using the ADC’s internal switches to GND and VIO to calculate a scale factor and offset. 

Source Code
Programming Language
Tools and Operating System
Document Author
Source Code Author
Table of Contents
0 0 votes
Article Rating
Notify of
Newest Most Voted
Inline Feedbacks
View all comments
Jose Rullan

Thank you! I was in fact looking into how to configure a pin for analog input. Thanks for this. I hope there would be one Quick Bytes for each feature or keyword or concept. Great initiative!

Copyright © 2021 Parallax Inc. All Rights Reserved
Designed and Made in California, USA