Linux LED class for GPIO (and PWM) controlled LEDs

GPIO's which are connected directly to LED's on are registered through the Linux LED class sysfs interface and you can control them via /sys/class/leds.

Mapping GPIO's to LED's (as well as defining their logic level and default trigger) is done in the board support file or in the case of more modern kernels such as Ventana in the device-tree. See the led node in the ​gw54xx device-tree for example.

LED brightness is represented as an integer value and for LED's connected to PWM signals this truly controls the brightness. However for LED's connected to GPIO's a brightness of 0 is off and any other value is on.

To list available LED's by name (see product hardware manual to match name to board LED):

ls /sys/class/leds

To enable/disable LEDs:

echo 0 > /sys/class/leds/user1/brightness ;# turn off user1 LED
echo 255 > /sys/class/leds/user1/brightness ;# turn on user1 LED

Some boards (consult the board user manual) use a bi-color LED (typical green/red) where 2 GPIO's are used (connected to each side of the LED). In this scenario setting one LED on and the other Off would produce one color and flipping them would produce the other (while turning them both on or both off would result in no LED's lit):

# turn bi-color LED Red 
echo 0 > /sys/class/leds/user1/brightness
echo 255 > /sys/class/leds/user2/brightness
# turn bi-color LED Green
echo 255 > /sys/class/leds/user1/brightness
echo 0 > /sys/class/leds/user2/brightness

LED Names and default configuration

The LED names and default configuration (on, off, polarity, and trigger) are controlled by the Linux device-tree. This is documented in the kernel source ​Documentation/devicetree/bindings/leds

While most Gateworks boards have device-trees that call out the front-panel LED's by a name from the hardware manual (user1 for green front-panel, user2 for red front-panel, user3 for surface mount green LED) describing the names specifically in the device-tree via the 'label' properly is no longer supported for new device-trees. New board device-trees must instead define led 'function' and 'color' properties and that is used to create a name. Note that the 'label' property was deprecated in Linux 5.4 with the introduction of the 'color' and 'function' properties.

For example, a Ventana board will show the following LED's:

/sys/class/leds/user1 # front panel green
/sys/class/leds/user2 # front panel red

And the same LED's on a Venice board:

/sys/class/leds/green:status # front panel green
/sys/class/leds/red:status # front panel red

See also:

LED Triggers

Through the LED class LED's can be triggered by certain events / items depending on kernel configuration.

To show what triggers are available in the current kernel:

# cat /sys/class/leds/user1/trigger 
[none] nand-disk mmc0 timer default-on netdev gpio heartbeat morse usbdev 
  • the one shown in brackets is the current trigger - to disable triggers set to none (as the example above shows)
  • the default trigger is specified in the board support file or device-tree

To set a trigger:

echo heartbeat > /sys/class/leds/user1/trigger
  • the hearbeat trigger blinks the LED twice at the end of every second
  • depending on peripherals installed other triggers may be available such as network or disk activity

Other References

Here are some other useful references from our wiki:

Last modified 3 years ago Last modified on 06/18/2021 05:04:42 PM
Note: See TracWiki for help on using the wiki.