Version 3 (modified by 3 years ago) ( diff ) | ,
---|
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 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: