I have a bit of a dilemma. I am currently using BCM GPIO 14, 15 and 18 to control an RGB status LED using PWM. I have configured my raspberry pi 3 to pull down pin 14 (red) on boot, to indicate the system is booting, but once I run my script the first time, I get this error:

RuntimeError: A PWM object already exists for this GPIO channel

On error, the script shuts down and runs GPIO.cleanup(), turning off the LED. On the next try, it works fine, since the LED is off upon initialization.

The way I initialize the status LED is:

class Status:
    def __init__(self, red_pin, green_pin, blue_pin):
        GPIO.setup(red_pin, GPIO.OUT, initial=GPIO.HIGH)
        GPIO.setup(green_pin, GPIO.OUT, initial=GPIO.HIGH)
        GPIO.setup(blue_pin, GPIO.OUT, initial=GPIO.HIGH)

        self.red_pin = GPIO.PWM(red_pin, 300)
        self.green_pin = GPIO.PWM(green_pin, 300)
        self.blue_pin = GPIO.PWM(blue_pin, 300)

    def set_led_color(self, r, g, b):
        red_cycle = 100 - (r * 100) / 255
        green_cycle = 100 - (g * 100) / 255
        blue_cycle = 100 - (b * 100) / 255

    def __del__(self):
        print("Status led is going offline")

Please note that the reason I'm setting all pins to HIGH on init is that my LED is a common anode LED.

I can understand that since GPIO 14 is HIGH, the GPIO.PWM on that pin can have issues, but I do not know exactly how to reset that pin upon initialization.


Thanks in advance

  • You are using GPIO 14, 15, 18, not pins 14, 15, 18. See pinout.xyz If the script runs first time without error are you sure it's not just a case of calling the RPi.GPIO cleanup() method before you exit?
    – joan
    May 23, 2019 at 9:12
  • There is no point posting code, and omitting vital parts.
    – Milliways
    May 23, 2019 at 9:35
  • @joan sorry for not being clear. The first time it runs, it throws this error. After this error occurs, GPIO.cleanup() is called (on object destruction), and the LED turns off. After a second try, the issue no longer appears because the LED was off. May 23, 2019 at 10:06
  • @Milliways what do you mean? May 23, 2019 at 10:07


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.