# adcRead_test.py # program to test adcRead() to read nChannel on the ADS1115 ADC RPi module from SeeedStuios # by D@CC on 2022CMar16 # needs to have adcRead_Pkg.py in the same folder as adcRead_test.py # needs import time import time # needs from adcRead_Pkg import adcRead,adcReadInit # needs cd Desktop #from adcRead_pkg import adcRead,adcReadInit # (only lets the program directly call the listed routines) # or #from adcRead_pkg import * # (lets the program directly call any of the routines in the pkg) # or #import adcRead_pkg # (to call any imported routine in the pkg using this syntax, # its name must be preceded by "adcRead_pkg." ) from adcRead_pkg import adcRead,adcReadInit #(actually its name is adcRead_pkg.py) #needs to be run as >$ python3 adcRead_test.py if __name__ == "__main__": print("adcRead_pkg.py and adcRead_test.py must be in /Desktop") print("from adcRead_pkg import adcRead,adcReadInit") # is needed # NB cannot use path prefixes in the above print(" python2 or 3 will create adcRead_pkg.pyc") print(">$ cd /home/pi/Desktop") # from this directory print(">$ python3 n.py $ python3 adcRead_test.py") # runs the adc program print(">$ ctrl+c will exit") progName="adcRead_test.py" #this is the main program to test adcRead() print("starting:"+progName) # These 9 parameters are also defined in # adcRead_pkg.py: # mydo_1() RESET_ADDRESS = 0b0000000 RESET_COMMAND = 0b00000110 POINTER_CONVERSION = 0x0 POINTER_CONFIGURATION = 0x1 POINTER_LOW_THRESHOLD = 0x2 POINTER_HIGH_THRESHOLD = 0x3 DEVICE_ADDRESS = 0x48 # device address selected on the IC chip icNumber = "ADS1115" # IC chip used I2C_BUS = 1 # BUS number used by the IC chip # above constants are used with the ADS1115 IC chip: # [they could be defined as global variables in adcRead_init() instead] # These 2 constants must be defined in the test_adcRead.py program dT = .1 # sleep time between readings (set by the user of this program) nChannel = 2 # AINn is the analog pin # to read on the ADS1115 IC chip # # more than 1 channel can be read by the program # # simply use adcRead(nChannel) where nChannel is the channel to read # usage of global variables # global g_adcValue # this variable can be referenced (seen) by any program using adcRead() # it always contains the most-recent-value read by adcRead() #create the BUS object BUS = adcReadInit(I2C_BUS,icNumber) BUS.open(I2C_BUS) while True : # loop value = adcRead(BUS,nChannel) # reading nChannel of the ADS1115 once AINn="AIN"+str(nChannel) AINn=AINn.strip(" ") print ("value of "+AINn+" reading:", value) if dT==0 : break #out of while loop # dT=0 will only read it once else: time.sleep(dT) # dT>0 will read it continuously #if end #while end #end adcRead_test.py