User Tools

Site Tools


led_pwm_control

This is an old revision of the document!


LED PWM Control

Jan 2017


Turning on LEDs using the GPIO on the Raspberry Pi is pretty simple. You just turn on or off the relevant GPIO pin, and your LED (connected via a 330r resistor) will come on and go off.

This is great until you want to control the brightness of the LED. You can control the LED Brightness using timers in Python (the slower/faster you pulse the GPIO, the brightness changes). There are a few issues though:

As you slow the frequency, the LED flickers. As you speed up the LED, it does eventually start to dim, but uses all the Pi cpu resources (well on zero it does).

So the most elegant solution is to use the PWM function. Its very simple to use, however there are only a couple of pins on the GPIO that support it. This is hardware based PWM, so it doesn't consume CPU resource.


Using PWM from the Command Line


I have not used this in Python yet, so for now I am doing it from the command line, using the WiringPi libraries. Check out the WiringPi page if you need to set this up (takes about 20 seconds).

led_pwm_control.1485193441.txt.gz · Last modified: 2023/03/09 22:35 (external edit)