I'm comfortable with the physical side of circuitry etc but a total newbie at python so I've been messing around with a Raspberry Pi 4B and python 3 to help me understand how to control various aspects of components. Here I have a rgb LED and I want to cycle through full white light, red light, green light, and blue light before returning back to the begining of the loop. I finally have it working how I want it but I'm confused about HOW the loop is working: When I set a GPIO.output to 0, it's turning ON the corresponding pin. Logically, this makes no sense to me because I would expect that the value 1 would turn the pin on. I'm thinking I might be misunderstanding how the loop works so I'm hoping someone might be able to enlighten me? I've filled this code with a LOT of comments that explain my understanding of what each line is doing.
import time
redpin = 29 # define redpin to 29
print ("redpin set to 29")
time.sleep(1.0) # 1 second delay
greenpin = 31 # define greenpin to 31
print ("greenpin set to 31")
time.sleep(1.0) # 1 second delay
bluepin = 33 # define bluepin to 33
print ("bluepin set to 33")
time.sleep(1.0) # 1 second delay
def setup():
GPIO.setmode(GPIO.BOARD) # use PHYSICAL GPIO numbering
GPIO.setup(redpin,GPIO.OUT) # set redpin to output mode
GPIO.output(redpin,0) # set redpin output to 0
GPIO.setup(greenpin,GPIO.OUT) # set greenpin to output mode
GPIO.output(greenpin,0) # set greenpin output to 0
GPIO.setup(bluepin,GPIO.OUT) # set bluepin to output mode
GPIO.output(bluepin,0) # set bluepin output to 0
def loop():
while True:
print("start of loop. no LED")
GPIO.output(redpin,1) # set redpin to 1 (off)
GPIO.output(greenpin,1) # set greenpin to 1 (off)
GPIO.output(bluepin,1) # set bluepin to 1 (off)
time.sleep(1.5) # 1.5 second delay
print("White LED")
GPIO.output(redpin,0) # set redpin to 0 (on)
GPIO.output(greenpin,0) # set greenpin to 0 (on)
GPIO.output(bluepin,0) # set bluepin to 0 (on)
time.sleep(4.0) # 4 second delay
print("no LED")
GPIO.output(redpin,1) # set redpin to 1 (off)
GPIO.output(greenpin,1) # set greenpin to 1 (off)
GPIO.output(bluepin,1) # set bluepin to 1 (off)
time.sleep(1.5) # 1.5 second delay
print("red LED only")
GPIO.output(redpin,0) # set redpin to 0 (on)
GPIO.output(greenpin,1) # set green to 1 (off)
GPIO.output(bluepin,1) # set bluepin to 1 (off)
time.sleep(4.0) # 4 second delay
print("no LED")
GPIO.output(redpin,1) # set redpin to 1 (off)
GPIO.output(greenpin,1) # set greenpin to 1 (off)
GPIO.output(bluepin,1) # set bluepin to 1 (off)
time.sleep(1.5) # 1.5 second delay
print("green LED only")
GPIO.output(redpin,1) # set redpin to 1 (off)
GPIO.output(greenpin,0) # set greenpin to 0 (of)
GPIO.output(bluepin,1) # set bluepin to 1 (off)
time.sleep(4.0) # 4 second delay
print("no LED")
GPIO.output(redpin,1) # set redpin to 1 (off)
GPIO.output(greenpin,1) # set greenpin to 1 (off)
GPIO.output(bluepin,1) # set bluepin to 1 (off)
time.sleep(1.5) # 1.5 second delay
print("blue LED only")
GPIO.output(redpin,1) # set redpin to 1 (off)
GPIO.output(greenpin,1) # set greenpin to 1 (off)
GPIO.output(bluepin,0) # set bluepin to 0 (on)
time.sleep(4.0) # 4 second delay
print("end of loop. returning to begining of loop.")
time.sleep(1) # 1 second delay
def destroy():
GPIO.cleanup() # reset all GPIO to 0
if __name__ == '__main__': # Program entrance
print ("LED should be off")
setup()
try:
loop()
except KeyboardInterrupt: # Press ctrl-c to end the program.
destroy()