Controlling leds status in OpenWrt

This is a quick how-to about controlling the status of the leds from userspace in OpenWrt.

If you have installed OpenWrt on your router the leds should be controlled by the leds_gpio kernel module. Every led will have an entry in sysfs under /sys/class/leds

To manually control a led you should first set the trigger file to none, otherwise the led will be controlled for its original function, i.e. ethernet activity indicator:
echo none > trigger
Then you can set the value by writing to the brightness file. This file represents the brightness level which should range from 0 to the value contained in the max_brightness file. However, in most cases there will not be hardware brightness control for the leds (like with GPIO), so 0 will turn OFF the led and any non-zero value will turn it ON:
echo 0 > brightness
echo 1 > brightness

An example on my TP-Link MR3020:
root@OpenWrt:~# cd /sys/class/leds/tp-link\:green\:3g/
root@OpenWrt:/sys/devices/platform/leds-gpio/leds/tp-link:green:3g# echo none > trigger
root@OpenWrt:/sys/devices/platform/leds-gpio/leds/tp-link:green:3g# echo 1 > brightness
root@OpenWrt:/sys/devices/platform/leds-gpio/leds/tp-link:green:3g# echo 0 > brightness

Note: if you don’t already know, those files are not actual files in flash, they’re virtual files that act as an interface from userland to kernel.
Note 2: if you want more control on the GPIO lines you may prefer to unload the leds_gpio module (with #rmmod leds_gpio) and directly control the GPIO and/or configure a bus on some lines, like i2c or spi.

Reference: <linux source>/Documentation/leds/leds-class.txt


4 thoughts on “Controlling leds status in OpenWrt

  1. Pingback: SPI over GPIO in OpenWrt | Randomcoderdude's blog

  2. If you just want to turn off all LEDs on an openwrt router, you can use this shell script (e.g. you can add to this to the file /etc/rc.local to disable them on boot):

    for p in `find /sys/devices/platform/leds-gpio/leds -name trigger`
    echo none > $p

    for p in `find /sys/devices/platform/leds-gpio/leds -name brightness`
    echo 0 > $p

    thanks for the helpful post, got me most of the way to solve my problem.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: