import RPi.GPIO as GPIO, time progName="Test_readCD2.py" print("progName:",progName) #Purpose: to sense if 2CD photocells "see" Light or Dark # using an internal pulldown resistor #By D@CC on 2023JOct30 # tested with PPin=40 on a Raspberrry Pi400 #Cover the CDs with a thumb to "Make it Dark" print("always works with 2 CD in series (covering both), ") print("works with 1 CD sometimes. . . ") print("from phys pin 38 (with output High).") print(" to phys pin 40 (as input with PUD_DOWN)") #PPout=38 #phys pin 38 #GPIO 20 output #PPin =40 #phys pin 40 #GPIO 21 input PPin=40 #This also works with PPin = 37 (But not PPin=3 nor 19) # It probably works with some other adjacent pin pairs def initCD2(PPin): #PPin-2 is PPout (tested with PPin = 40) GPIO.setmode(GPIO.BOARD) #physical Board numbers # GPIO.BMB BroadCoM numbering eg GPIO #s isBadPin= ((PPin-2==1) or (PPin-2==17)) # they are 3v3 # Ground pins won't work either if not isBadPin: GPIO.setup(PPin-2,GPIO.OUT) print("turn On phys pin:", PPin-2) GPIO.output(PPin-2, True) #if end GPIO.setup(PPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # on a Raspberry Pi, PullUpDown is a 10k resistor # a photoresistor photocell (CD) when lit is 5k0. # when dark is 0k0 return #def end def readCD2(PPin): #Purpose: to return True if both CDs are covered #By: D@CC on 2023JOct30 #Tested with PPin=40 reading=GPIO.input(PPin) # reading is True if the CD photoCells are Dark if reading: state=False else: state=True #if end #time.sleep(1) return state #def end turnOn=initCD2(PPin) while True: if readCD2(PPin): print("Dark") else: print("Light") #if end time.sleep(1) #while end #/Test_readCD2.py