3

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()

1 Answer 1

1

Presumably you have a common anode RGB LED. In these you connect 3V3 to the common anode and connect each leg via a resistor to a GPIO. Current flows when the GPIO is set to 0V and acts as a current sink.

Contrast with a common cathode RGB LED. In these you connect ground to the common cathode and connect each leg via a resistor to a GPIO. Current flows when the GPIO is set to 3V3 and acts as a current source.

2
  • Yup, that's exactly it. I have a canakit and you can't really tell from looking at RGB LED but after reviewing your answer I went back and looked over a tutorial that accompanied the RGB LED. Sure enough I can see the common anode is hooked up to the 3.3Vdc supply. Thanks for the prompt and descriptive answer!
    – Bar
    Commented May 9, 2020 at 22:35
  • @Bar Please accept the answer with a click on the tick on its left side. Only this will finish the question and it will not pop up again year for year.
    – Ingo
    Commented May 10, 2020 at 10:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.