I'm working on a project with a RGB LED which has to indicate what the status of the internet connection is. It is running as a service. I'm focused on two files, one to check if there is an internet connection and one for controlling the LED.

The following code is where we check if there is an internet connection and trying to call the LED function:

import logging

from status_server import status_led

def testInternetConnection():
    logging.info("Testing the internet connection...")

    while True:
            r = requests.get(https://google.com)

            # HTTP 200 OK
            if r.status_code == 200:
                logging.info("Internet connection is OK!")
        except KeyboardInterrupt:
        except requests.exceptions.RequestException:
            logging.warning("Re-trying the internet connection....")

It is working fine with the following code in the LED file:

import RPi.GPIO as GPIO

class _RGBLed:
    def __init__(self, red_pin, green_pin, blue_pin):
        self.RED_PIN = red_pin
        self.GREEN_PIN = green_pin


    def init_pins(self):
        GPIO.setup(self.RED_PIN, GPIO.OUT)
        GPIO.setup(self.GREEN_PIN, GPIO.OUT)

    def turn_red(self)
        GPIO.output(self.RED_PIN, GPIO.HIGH)

    def turn_green(self):
        GPIO.output(self.GREEN_PIN, GPIO.HIGH)

_rgb_led = _RGBLed(red_pin=26, green_pin=19)

def turn_red():

def turn_green():

The LED is quite bright so I wanted to add PWM to control the LED and lower the duty cycle. I've tested a bit with PWM in the Python live environment and that wasn't that hard. I've changed the turn_green and turn_red functions with the following:

def turn_green():
    turn_green_on = GPIO.PWM(GREEN_PIN, 100)

def turn_red():
    turn_red_led_on = GPIO.PWM(RED_PIN, 100)

I'm calling those functions from another When I do this it says name GREEN_PIN is not defined. When I change GREEN_PIN to 19 it doesn't give me warnings anymore, but the LED does nothing too. I've checked if the function is called by adding a log statement so that's not the problem either.

Can someone help me with this? Thanks in advance :)

  • We can't tell as the question has inconsistent code snippets. Could you post the complete code you are actually trying to use?
    – joan
    Commented Nov 18, 2016 at 14:38
  • I've added some code, hope this makes more sense Commented Nov 18, 2016 at 15:04
  • Not really. You have three definitions of turn_red and three of turn_green. Some may be in the _RBGLed class but we can't tell because the indentation is wrong.
    – joan
    Commented Nov 18, 2016 at 15:10
  • Yes, you're right. It should be more clear now. Commented Nov 18, 2016 at 15:16
  • GREEN_PIN/RED_PIN are class variables only available within the class. You have setmode() within the class. Are you still calling the class at all? Otherwise where are you calling setmode()?
    – joan
    Commented Nov 18, 2016 at 15:24

2 Answers 2


Per PWM's Wiki: (https://sourceforge.net/p/raspberry-gpio-python/wiki/PWM/)

Note that PWM will also stop if the instance variable 'p' goes out of scope.

Your variables are going out of scope (the scope of the method) as soon as you call it, to make them "permanent" basically in your class make both turn_green_on and turn_red_led_on instance variables, most likely defined in your class constructor:

def __init__(self):
    self._turn_green_on = None
    self._turn_red_led_on = None

And in your class' method:

def turn_green():
   self._turn_green_on = GPIO.PWM(GREEN_PIN, 0.5)

If I understand it correctly, your PWM-frequency is 100Hz and your duty cycle is 20%. This would mean, that your LED is on for 1s/100*0.2 = 2ms and off for about 1s/100*0.8 = 8ms (and so on). Are you sure you can see that? Maybe you should start with

def turn_green():
   turn_green_on = GPIO.PWM(GREEN_PIN, 0.5)

to be sure to be able to visually detect a working setup.

  • I've tried it, it doesn't do anything with the led. I've tried this in the Python live environment and that does give me a blinking green LED, so it is something in the bigger code. Commented Nov 18, 2016 at 15:18

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.