32-Channel ADC Object

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

The jm_analog_in_multi.spin2 object supports up to 32 ADC channels that can measure from 0 to 3.3v. The object is easily configured with a base pin, channel count, and low and high scaling factors that provide meaningful output from ADC channel readings. For example, using 0 and 3300 for the scaling factors provides the application readings from each channel in millivolts. After setup, individual channel scaling factors may be modified if desired. Each ADC channel is configured for 12 bits, using SINC2 filtering of 2048 samples per reading.

If you need more than 32 ADC channels, use a second instance of the object. 

We’ll use an actual Parallax customer request to demonstrate the object in a factory test fixture. The customer requested 16 UV Light Sensors connected to ADCs P0-P15. Shown above is the bench test proof of concept, with the sensors arranged adjacent to each other. The actual test fixture requires organizing the sensors in a 3D space around the device under test to detect UV light leakage. 

Two code archives are provided for download, below. The first one is the original object demo from Jon McPhalen, and the second is the modified version for the customer’s application with 16 UV Light Sensors. 

The following modifications were made for the customer’s application. Identify the first IO (P0) connected to the bank of UV Light Sensors. 

con { app io pins }

  UV_CH0   = 0  { I }                         '16 UV Light Sensors from P0

Sequence the additional ADC inputs from the same starting IO pin. CHANNELS = 16 assigns them from P0 to P15. 

  CHANNELS = 16                                    ' analog channels   

Identify the low and high voltage ranges.

  V_LO =  0_00                                                  ' scaled range for volts
  V_HI =  3_30

The pub setup() method configures the analog inputs by providing the constants and starts the terminal. It’s that easy —  jm_analog_in_multi.spin2 takes care of the measurements. 

pub setup()

  ain.start(UV_CH0, CHANNELS, V_LO, V_HI)                    ' configure analog inputs
  ain.set_range(2, L_LO, L_HI)                                  ' reconfigure ch2 range
  term.tstart(BR_TERM)                                          ' start terminal io

The pub main() method receives the ADC values and displays them on Parallax Serial Terminal. The changes to the demo include the addition of a channels variable (CH ) to write the 16 channels of ADC to the terminal in a loop. This example works with both Parallax Serial Terminal and ANSI-compliant terminals (like the one in FlexProp). 

The waitct calculation stalls the application until 50 ms have elapsed from the first getct() value, making 20 updates per second. If the repeat loop requires more than 50 ms, this value should be increased. 

pub main() | t, x, y, CH



  t := getct()
    if (T_TYPE == T_PST)
      term.fstr2(string("%c%s\r\r"), term.HOME, @Banner)
      term.fstr2(string("%s%s"), ansi.home(), ansi.fgnd(ansi.CYAN))
      term.fstr2(string("%s%s\r\r"), @Banner, ansi.normal())

    repeat CH from 0 to CHANNELS-1
      term.fstr2(string("Volts %2d: %5.2fv   \r"), ch, ain.read(ch))

    waitct(t += (50 * MS_001))

'  term.fstr2 for two values
'  % defines a formatted value with
'  5 places of width and .2 for two digits precision
'  f formats decimal value like floating point
Programming Language
Tools and Operating System
Document Author
Source Code Author
Table of Contents
0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Copyright © 2021 Parallax Inc. All Rights Reserved
Designed and Made in California, USA