On Sun, 10 Nov 2019 18:17:13 -0800 (PST), in
gmane.comp.hardware.beagleboard.user Mala Dies
<[email protected]> wrote:

        Just some stuff I scratched together -- all untested (read the
comments)

-=-=-=- adc-1.py
#!/usr/bin/env python3
"""
    adc-1.py        ADC example using MCP3008 and mysterious LDRII library
    November 11, 2019           Dennis L Bieber

    *** UNTESTED **
    Not only do I not have access to an MCP3008 ADC chip, I have not
    been able to find the LDRII (Light Dependent Resistor?) library
    supporting said chip.
    The API used is based upon postings in comp.lang.python by one
    "Mala Dies" who, it seems, is having a truly bad day

    This code reads all 8 channels of the MCP3008. It may be worth
    checking the data sheet for the chip to determine which state
    draws the least current and using a 10KOhm or higher resister to
    pull-up/pull-down the unused inputs.
"""

import time
from LDRII import MCP3008

#constants from spec sheets
ADC_BITS = 10
ADC_REF = 3.3
ADC_CHANNELS = 8

T_DELTA = 5.0

#derived constants
ADC_MAX = (2 ** ADC_BITS) - 1
ADC_VSTEP = ADC_REF / ADC_MAX

#create ADC instance
adc = MCP3008()

t0 = t = time.time()
while True:
    print("\nT: %16.2f" % (time.time() - t0))
    for channel in range(ADC_CHANNELS):
        raw = adc.read(channel = channel)
        print("\tChannel: %2d\tCount: %4d\tVoltage: %6.3f"
              % (channel, raw, raw * ADC_VSTEP))
    t += T_DELTA
    time.sleep(t - time.time())
-=-=-=-
-=-=-=- adc-2.py
#!/usr/bin/env python3
"""
    adc-2.py        ADC example using MCP3008 and Adafruit_Blinka
                    and Adafruit_CircuitPython_MCP3xxx libraries
    November 11, 2019           Dennis L Bieber

    *** UNTESTED **
    I do not have access to an MCP3008 ADC chip

    *** PREREQUISITES ***
        sudo pip3 install adafruit_blinka (might be adafruit-blinka)
        sudo pip3 install adafruit-circuitpython-mcp3xxx

    This code reads all 8 channels of the MCP3008. It may be worth
    checking the data sheet for the chip to determine which state
    draws the least current and using a 10KOhm or higher resister to
    pull-up/pull-down the unused inputs.
"""

import time

import busio
import digitalio
import board
import adafruit_mcp3xxx.mcp3008 as MCP
from adafruit_mcp3xxx.analog_in import AnalogIn

#constants from spec sheets
ADC_REF = 3.3
ADC_CHANNELS = 8

T_DELTA = 5.0

#create the SPI bus and ChipSelect
spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)
cs = digitalio.DigitalInOut(board.DS)

#create ADC instance (note: ref_voltage default is 3.3 if not supplied)
adc = MCP.MCP3008(spi, cs, ref_voltage = ADC_REF)

#create ADC channel instances -- relies on MCP.P0 = 0, etc.
channels = [ AnalogIn(adc, chan) for chan in range(ADC_CHANNELS) ]

t0 = t = time.time()
while True:
    print("\nT: %16.2f" % (time.time() - t0))
    for channel in channels:
        #while a 10-bit ADC chip, the raw counts are scaled to 65472
        raw, voltage = channels[channel].value, channels[channel].voltage
        print("\tChannel: %2d\tCount: %4d\tVoltage: %6.3f"
              % (channel, raw, voltage))
    t += T_DELTA
    time.sleep(t - time.time())
-=-=-=-
-=-=-=- adc-3.py
#!/usr/bin/env python3
"""
    adc-3.py        ADC example using native BBB ADC and
                    adafruit_bbio library
    November 11, 2019           Dennis L Bieber

    *** UNTESTED **
    While I do have BBB, I have not taken time to wire a circuit

    *** WARNING ***
        BBB ADC inputs can be damaged by signals greater than 1.8V
        It would perhaps be best to use a voltage divider circuit
        with the 3.3V yielding a maximum of 1.65V [giving up a few
        counts for the last 0.15V]

    This code reads all 7 channels of the BBB. It may be worth
    checking the data sheet for the chip to determine which state
    draws the least current and using a 10KOhm or higher resister to
    pull-up/pull-down the unused inputs.
"""

import time
import Adafruit_BBIO.ADC as ADC

#constants from spec sheets
ADC_REF = 1.8

#pins equivalent to AIN0..AIN6
CHANNELS = [    "P9_39",    "P9_40",    "P9_37",
                "P9_38",    "P9_33",    "p9_36",
                "P9_35" ]

T_DELTA = 5.0

#initialize ADC system
ADC.setup()

t0 = t = time.time()
while True:
    print("\nT: %16.2f" % (time.time() - t0))
    for channel in channels:
        #a documented bug in ADC driver recommends one needs to
        #read twice to get latest value, I'm presuming that does
        #not mean both .read() and .read_raw() need to be repeated
        jnk = ADC.read(channel)
        raw, voltage = ADC.read_raw(channel), ADC.read(channel)
        #"voltage" is scaled 0.0 .. 1.0, so multiply by reference
        print("\tChannel: %2d\tCount: %4d\tVoltage: %6.3f"
              % (channel, raw, voltage * ADC_REF))
    t += T_DELTA
    time.sleep(t - time.time())
-=-=-=-
-- 
Dennis L Bieber

-- 
For more options, visit http://beagleboard.org/discuss
--- 
You received this message because you are subscribed to the Google Groups 
"BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/beagleboard/mkrisehuc4f66715arh4ki5f07tvj6vof1%404ax.com.

Reply via email to