# vMeterIXA_pico.py #by: D@CC #on: 2022FJun11 #Article: 180.html #uSD: Fla53S import machine from machine import Pin,ADC import time # originally written for ePC Article 165 Mak Pi ADC # MakPiADC board works on 2021FJun14 # CD0 is ADC0 # Pot is ADC1 #CDS0 & pot work if SW2 & SW3 are on # modified (ver B) for ePC Article 180 RAPasc using ixProbe by D@CC # to read pico ADC0 with ADC ixProbe attenuated by 9 # to be used with the INA219 to measure the Tx and Rx signals of a USB connector # refer to ePC Article 146 for more INA219 info def RAPasc(nn,cvrsFactor): # RAPasc protocol means # 100 complex Readings from pico ADC nn with Periods in 7 bit asc (ascii) # see article 150 by D@CC for RAPasc # Purpose to return converted value of ADC0 # in a special format of a complex number where # the real part is the converted voltage # and the imaginary part is the UNIX time of the reading # see Article 165 MAK Pi ADC # version A works fine (it now has high input impedance as of 2022FJun09) # A is used with a 10K0 and a 100k0 resistor to read 9% of actual voltage. # This ensures that the ADC max input voltage is not exceeded. # if nn=1: # returns value of ADCnn * cvrsFactor # plus dt as imaginary value # if conversion factor ==1 then # real component is returned as a hex value # if nn==0: # returns value of ADC0 * (reading * cvrsFactor * ixProbe) ixProbe=9 #ixProbe attenuation is 9 timeNow=time.time() strTime=str(timeNow)+".000" if nn==0: #nn is 0 #ADC0 strN0="0" strReading=str(readADC_pico(0)*ixProbe) print("|"+strReading+"|") strReading=strReading.strip() RAPreading="ADC"+strN0+":("+strReading+"+"+strTime+"j)" print(RAPreading) #if end return RAPreading def readADC_pico(nn,cnvrt=5.035477e-05): #nn is the pico GPnn (ADCnn) to read # if cnvrt is absent: # returns a value from 0 to 3v3 # if cnvrt = 1 # returns the value read from 0 to 65535 # if cnvrt = other # returns a value in range 0 to other*65535 # #cnvrt = 5.035477e-05 converts the reading to volts # when the ixProbe is not used . # that is the default cnvrt factor. # next line is needed to remove pull-up resistor # to produce a high-impedance input # Source: forum.micropython.org/viewtopic.php?f=21&t=9959 adcFirst =Pin(nn+26,Pin.IN) # create the ADC object adcFirst = machine.ADC(nn) # now read the ADC adcValue=adcFirst.read_u16() #print("The value of ADC0 is", adcValue) #conversion_factor = 3.3 / (65535) #print("conversion_factor:",conversion_factor) adcVoltage=adcValue * cnvrt #print("The voltage at ADC0 is", adcVoltage) #print("-bye- from readADC_pico.py") return adcVoltage #def end # main program starts here progName="vMeterIXA_pico.py" print("progName:",progName) print(" by D@CC on 2022FJun10") print() #output is ADC0:(3.24738+1654827017.000j) #ADC0: 3v3 # Unix Time:1654827017 is Thu Jun 09 2022 22:10:17 UTC-0400 (EDST) # ixProbe sensing pico pin 35 ADCref (atten:9) # green pico pin 33 GND # yellow pico pin 31 ADC0 # #cvrsFactor=1.0000 #always 1.0000 in this program ixProbe=10 # Will eventually prompt the user for the following: # n0 : to select ADC0 # rCount : number of readings to gather # time0 : Unix time to start taking the first reading # dt : time between readings n0=0 #ADC number = 0 or 1 usually dt = 1 #time interval #if rCount == 0 : display ADC0 continuously # else : save the readings & times in a matrix strN0=str(n0) while True: timeNow=time.time() #eventually record the time in milliseconds # for now, just append ".000" strTime=str(timeNow)+".000" # process ADC0 # next read ADC0 in volts (because parm2 is absent) # presume that a ixProbe with attenuation of 9 is being used vADC=readADC_pico(n0)*ixProbe strReading=str(vADC) #print("|"+strReading+"|") strReading=strReading.strip() # there is no need for .strip() ???? # RAPasc protocol means # complex Reading from ADC with Periods in 7 bit asc (ascii) # see article 150 by D@CC strRAPasc="ADC"+strN0+":("+strReading+"+"+strTime+"j)" print(strRAPasc) print() # print a blank line time.sleep(dt) # 1 sec #while end #/vMeterIXA_pico.py stored in Raspberry Pi Pico & pi/pico_4f31_on_Fla53