1

I already know that using for example GPIO.output(led_pin, led_state) I can turn on and off an LED.

What I would like to have, is to adjust the brightness of an LED, either by an absolute value (in whatever measurement unit) or by a percentage. Something like led_brightness(led_pin, 50) # 50% brightness.

5
  • Have you looked into the PWM class? Some examples here
    – Seamus
    Mar 9, 2021 at 16:14
  • I actually did but I didn't know what the values of the variables were in those examples.
    – topkek
    Mar 9, 2021 at 16:19
  • 3
    Mentioning that might have made a better question?
    – Seamus
    Mar 9, 2021 at 16:35
  • How? Because I didn't take wild guesses what the value of a variable is, which I never saw being initialized or any value assigned to it? All the other cases ware usage of booleans.
    – topkek
    Mar 10, 2021 at 7:26
  • To let us know what you've tried. This is covered under the "How to ask a good question" topic
    – Seamus
    Mar 10, 2021 at 23:53

2 Answers 2

3

The gpiozero library allows led brightness using pulse-width-modulation:

https://gpiozero.readthedocs.io/en/stable/recipes.html#led-with-variable-brightness

1

pigpio lets you use hardware timed PWM to change LED brightness.

The default frequency is 800 Hz which will be fine for LEDs (you can change the frequency with set_PWM_frequency).

To change the LED brightness change the PWM dutycycle with set_PWM_dutycycle between 0 (off) to 255 (fully on).

As hardware timing is used the LED will not occasionally noticeably glitch as fully on or off.

Your Answer

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

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