• Connect the cathode (short leg, flat side) of the LED to a ground pin; connect the anode (longer leg) to a limiting resistor; connect the other side of the limiting resistor to a GPIO pin (the limiting resistor can be placed either side of the LED).


  • LED(pin, *, active_high=True, initial_value=False, pin_factory=None)


    1. pin (int or str) – The GPIO pin which the LED is connected to. See Pin Numbering for valid pin numbers. If this is None a GPIODeviceError will be raised.


    2. active_high (bool) – If True (the default), the LED will operate normally with the circuit described above. If False you should wire the cathode to the GPIO pin, and the anode to a 3V3 pin (via a limiting resistor).


    3. initial_value (bool or None) – If False (the default), the LED will be off initially. If None, the LED will be left in whatever state the pin is found in when configured for output (warning: this can be on). If True, the LED will be switched on initially.


  • blink(on_time=1, off_time=1, n=None, background=True) Make the device turn on and off repeatedly.


  • Parameters:


    1. on_time (float) – Number of seconds on. Defaults to 1 second.


    2. off_time (float) – Number of seconds off. Defaults to 1 second.


    3. n (int or None) – Number of times to blink; None (the default) means forever.


    4. background (bool) – If True (the default), start a background thread to continue blinking and return immediately. If False, only return when the blink is finished (warning: the default value of n will result in this method never returning).


  • off() Turns the device off.


  • on() Turns the device on.


  • toggle() Reverse the state of the device. If it’s on, turn it off; if it’s off, turn it on.


  • is_lit Returns True if the device is currently active and False otherwise. This property is usually derived from value. Unlike value, this is always a boolean.


  • pin The Pin that the device is connected to. This will be None if the device has been closed (see the close() method). When dealing with GPIO pins, query pin.number to discover the GPIO pin (in BCM numbering) that the device is connected to.


  • value - Returns 1 if the device is currently active and 0 otherwise. Setting this property changes the state of the device.


  •  
    
    #Turn on LED
    from gpiozero import LED
    
    led = LED(17)
    led.on()
    
    
    
    
     
    
    #Turn off LED
    from gpiozero import LED
    
    led = LED(17)
    led.off()
    
    
    
    
     
    
    #Switch it on initially
    from gpiozero import LED
    
    led = LED(17,initial_value=True)
    
    
    
    
    
     
    
    #Switch it off initially
    from gpiozero import LED
    
    led = LED(17,initial_value=False)
    
    
    
    
    
     
    
    #Blink 10 times, on for 1 sec and off for 1 sec
    from gpiozero import LED
    
    led = LED(17)
    led.blink(on_time=1, off_time=1, n=10)
    
    
    
    
     
    
    #Reverse the state of the device. If it’s on, turn it off; if it’s off, turn it on.
    from gpiozero import LED
    
    led = LED(17)
    led.on()
    led.toggle()
    
    
     
    
    #Get state of LED 
    from gpiozero import LED
    
    led = LED(17)
    led.on()
    print("is it on",led.is_lit)
    
    
     
    
    #Returns 1 if the device is currently on and 0 otherwise
    from gpiozero import LED
    
    led = LED(17)
    print("is it on or off",led.value())
    
    
     
    
    #Turn it on and off
    from gpiozero import LED
    
    led = LED(17)
    led.value = 1
    led.value = 0
    
    
  • Connect a GPIO pin to the anode (long leg) of the LED, and the cathode (short leg) to ground, with an optional resistor to prevent the LED from burning out.


  • gpiozero.PWMLED(pin, *, active_high=True, initial_value=0, frequency=100, pin_factory=None)


  • Parameters:


    1. pin (int or str) – The GPIO pin which the LED is connected to.


    2. active_high (bool) – If True (the default), the on() method will set the GPIO to HIGH. If False, the on() method will set the GPIO to LOW (the off() method always does the opposite).


    3. initial_value (float) – If 0 (the default), the LED will be off initially. Other values between 0 and 1 can be specified as an initial brightness for the LED. Note that None cannot be specified (unlike the parent class) as there is no way to tell PWM not to alter the state of the pin.


    4. frequency (int) – The frequency (in Hz) of pulses emitted to drive the LED. Defaults to 100Hz.


  • blink(on_time=1, off_time=1, fade_in_time=0, fade_out_time=0, n=None, background=True) Make the device turn on and off repeatedly.


  • Parameters:


    1. on_time (float) – Number of seconds on. Defaults to 1 second.


    2. off_time (float) – Number of seconds off. Defaults to 1 second.


    3. fade_in_time (float) – Number of seconds to spend fading in. Defaults to 0.


    4. fade_out_time (float) – Number of seconds to spend fading out. Defaults to 0.


    5. n (int or None) – Number of times to blink; None (the default) means forever.


    6. background (bool) – For advanced users.


  • off() Turns the device off.


  • on() Turns the device on.


  • pulse(fade_in_time=1, fade_out_time=1, n=None, background=True) Make the device fade in and out repeatedly.


  • Parameters:


    1. fade_in_time (float) – Number of seconds to spend fading in. Defaults to 1.


    2. fade_out_time (float) – Number of seconds to spend fading out. Defaults to 1.


    3. n (int or None) – Number of times to pulse; None (the default) means forever.


    4. background (bool) – For advanced users.


  • toggle() Toggle the state of the device. If the device is currently off (value is 0.0), this changes it to “fully” on (value is 1.0). If the device has a duty cycle (value) of 0.1, this will toggle it to 0.9, and so on.


  • is_lit Returns True if the device is currently active (value is non-zero) and False otherwise.


  • pin The Pin that the device is connected to. This will be None if the device has been closed (see the close() method). When dealing with GPIO pins, query pin.number to discover the GPIO pin (in BCM numbering) that the device is connected to.


  • value The duty cycle of the PWM device. 0.0 is off, 1.0 is fully on. Values in between may be specified for varying levels of power in the device.


  •  
    #If 0 (the default), the LED will be off initially. 
    #Other values between 0 and 1 can be specified as an initial brightness for the LED.
    from gpiozero import PWMLED
    
    led1 = PWMLED(17, initial_value=0)
    led2 = PWMLED(14, initial_value=1)
    led3 = PWMLED(15, initial_value=0.5)
    
    
     
    
    #LED should blink for 10 times and stay on for 1 sec, stay off for 1 sec
    from gpiozero import PWMLED
    
    led1 = PWMLED(17)
    led1.blink(on_time=1, off_time=1, fade_in_time=0, fade_out_time=0, n=10)
    
    
    
    
     
    
    #LED should blink for 10 times and stay on for 1 sec, stay off for 1 sec
    from gpiozero import PWMLED
    
    led1 = PWMLED(17)
    led1.blink(on_time=1, off_time=1, fade_in_time=0, fade_out_time=0, n=10)
    
    
    
    
     
    
    #LED should fade in and fade out for 10 times
    from gpiozero import PWMLED
    
    led1 = PWMLED(17)
    led1.pulse(fade_in_time=1, fade_out_time=1, n=10)
    
    
    
     
    
    #LED should be on and then off
    from gpiozero import PWMLED
    
    led1 = PWMLED(17)
    led1.on()
    led1.off()
    
    
    
     
    
    #LED if currently off (value is 0.0), this changes it to “fully” on (value is 1.0). If the device has a duty cycle (value) of 0.1, this will toggle it to 0.9, and so on.
    from gpiozero import PWMLED
    
    led1 = PWMLED(17)
    led1.on()
    led1.toggle() #off
    led1.toggle() #on
    led1.value = 0.1
    led1.toggle() #0.9
    led1.value = 0.9
    led1.toggle() #0.1
    
    
    
     
    
    #LED is lit or not. What is the brightness of LED and to which pin is it connected
    
    from gpiozero import PWMLED
    
    led1 = PWMLED(17)
    print("is it lit or not",led1.is_lit)
    print("to which pin is it connected",led1.pin)
    print("What is the brightness of LED",led1.value)
    
    
  • RGBLED(red, green, blue, *, active_high=True, initial_value=(0, 0, 0), pwm=True, pin_factory=None)


  • Represents a full color LED component (composed of red, green, and blue LEDs).


  • Connect the common cathode (longest leg) to a ground pin; connect each of the other legs (representing the red, green, and blue anodes) to any GPIO pins. You should use three limiting resistors (one per anode).


  • Parameters:


    1. red (int or str) – The GPIO pin that controls the red component of the RGB LED. See Pin Numbering for valid pin numbers.


    2. green (int or str) – The GPIO pin that controls the green component of the RGB LED.


    3. blue (int or str) – The GPIO pin that controls the blue component of the RGB LED.


    4. active_high (bool) – Set to True (the default) for common cathode RGB LEDs. If you are using a common anode RGB LED, set this to False.


    5. initial_value (Color or tuple) – The initial color for the RGB LED. Defaults to black (0, 0, 0).


    6. pwm (bool) – If True (the default), construct PWMLED instances for each component of the RGBLED. If False, construct regular LED instances, which prevents smooth color graduations.


  • blink(on_time=1, off_time=1, fade_in_time=0, fade_out_time=0, on_color=(1, 1, 1), off_color=(0, 0, 0), n=None, background=True) - Make the device turn on and off repeatedly.


  • Parameters:


    1. on_time (float) – Number of seconds on. Defaults to 1 second.


    2. off_time (float) – Number of seconds off. Defaults to 1 second.


    3. fade_in_time (float) – Number of seconds to spend fading in. Defaults to 0. Must be 0 if pwm was False when the class was constructed (ValueError will be raised if not).


    4. fade_out_time (float) – Number of seconds to spend fading out. Defaults to 0. Must be 0 if pwm was False when the class was constructed (ValueError will be raised if not).


    5. on_color (Color or tuple) – The color to use when the LED is “on”. Defaults to white.


    6. off_color (Color or tuple) – The color to use when the LED is “off”. Defaults to black.


    7. n (int or None) – Number of times to blink; None (the default) means forever


    8. background (bool) – For advanced users.


  • off() Turn the LED off. This is equivalent to setting the LED color to black (0, 0, 0).


  • on() Turn the LED on. This equivalent to setting the LED color to white (1, 1, 1).


  • pulse(fade_in_time=1, fade_out_time=1, on_color=(1, 1, 1), off_color=(0, 0, 0), n=None, background=True) Make the device fade in and out repeatedly.


  • Parameters:


    1. fade_in_time (float) – Number of seconds to spend fading in. Defaults to 1.


    2. fade_out_time (float) – Number of seconds to spend fading out. Defaults to 1.


    3. on_color (Color or tuple) – The color to use when the LED is “on”. Defaults to white.


    4. off_color (Color or tuple) – The color to use when the LED is “off”. Defaults to black.


    5. n (int or None) – Number of times to pulse; None (the default) means forever.


    6. background (bool) – Advanced users .


  • toggle() Toggle the state of the device. If the device is currently off (value is (0, 0, 0)), this changes it to “fully” on (value is (1, 1, 1)). If the device has a specific color, this method inverts the color.


  • blue Represents the blue element of the LED as a Blue object.


  • color Represents the color of the LED as a Color object.


  • green Represents the green element of the LED as a Green object.


  • is_lit Returns True if the LED is currently active (not black) and False otherwise.


  • red Represents the red element of the LED as a Red object.


  • value Represents the color of the LED as an RGB 3-tuple of (red, green, blue) where each value is between 0 and 1 if pwm was True when the class was constructed (and only 0 or 1 if not). For example, red would be (1, 0, 0) and yellow would be (1, 1, 0), while orange would be (1, 0.5, 0).


  •  
    #Gives yellow color
    from gpiozero import RGBLED
    
    led = RGBLED(2, 3, 4)
    led.color = (1, 1, 0)
    
    
     
    #Set to True (the default) for common cathode RGB LEDs. If you are using a common anode RGB LED, set this to False.
    from gpiozero import RGBLED
    
    led1 = RGBLED(2, 3, 4, active_high=True)
    led2 = RGBLED(12, 13, 14, active_high=False)
    
    
     
    #Initially RGBLED will give white light
    from gpiozero import RGBLED
    
    led1 = RGBLED(2, 3, 4, intial_value = (1,1,1))
    
    
    
     
    #Initially RGBLED will give white light at half its maximum brightness and initial value can be float as LEDs are PWM
    from gpiozero import RGBLED
    
    led1 = RGBLED(2, 3, 4, intial_value = (0.5,0.5,0.5), pwm = True)
    
    
    
     
    #RGBLED shall blink and be on for 1 sec, off for 1 sec, spend 0.5sec fading in and out. 
    
    from gpiozero import RGBLED
    
    led1 = RGBLED(2, 3, 4)
    led1.blink(on_time=1, off_time=1, fade_in_time=0.5, fade_out_time=0.5, on_color=(1, 1, 1), off_color=(0, 0, 0), n=10)
    
    
    
    
     
    #RGBLED turn it off and on
    from gpiozero import RGBLED
    
    led1 = RGBLED(2, 3, 4)
    led1.off()
    led1.on()
    
    
    
    
     
    #RGBLED fade in and fade out ten times with given specifications
    from gpiozero import RGBLED
    
    led1 = RGBLED(2, 3, 4)
    led1.pulse(fade_in_time=1, fade_out_time=1, on_color=(1, 1, 1), off_color=(0, 0, 0), n=10)
    
    
    
    
     
    #RGBLED fade in and fade out ten times with given specifications
    from gpiozero import RGBLED
    
    led1 = RGBLED(2, 3, 4)
    led1.on()
    led1.toggle() #off
    led1.toggle() #on
    led1.value = (0,1,1)
    led1.toggle() # (1,0,0)
    
    
    led.red = 1  # full red
    sleep(1)
    led.red = 0.5  # half red
    sleep(1)
    
    led.color = (0, 1, 0)  # full green
    sleep(1)
    led.color = (1, 0, 1)  # magenta
    sleep(1)
    led.color = (1, 1, 0)  # yellow
    sleep(1)
    led.color = (0, 1, 1)  # cyan
    sleep(1)
    led.color = (1, 1, 1)  # white
    sleep(1)
    
    
    print("is it on", led1.is_lit)