Skip to content

Instantly share code, notes, and snippets.

@troglobit
Created October 19, 2023 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save troglobit/35f4947e573fe191cbcfc81be5638bf4 to your computer and use it in GitHub Desktop.
Save troglobit/35f4947e573fe191cbcfc81be5638bf4 to your computer and use it in GitHub Desktop.
Blinking LEDs in Linux

Blinking LEDs in Linux

First check you have the timer trigger loaded:

root@gimli:~# lsmod |grep led
snd_ctl_led            24576  0
input_leds             16384  0
snd                   135168  45 snd_ctl_led,snd_hda_codec_generic,snd_seq,snd_seq_device,snd_hda_codec_hdmi,snd_hwdep,snd_hda_intel,snd_usb_audio,snd_usbmidi_lib,snd_hda_codec,snd_hda_codec_realtek,snd_sof,snd_timer,snd_compress,thinkpad_acpi,snd_soc_core,snd_pcm,snd_rawmidi
soundcore              16384  2 snd_ctl_led,snd
ledtrig_audio          16384  3 snd_ctl_led,snd_hda_codec_generic,thinkpad_acpi

It wasn't loaded, so modprobe it:

root@gimli:~# modprobe ledtrig-timer

Now you can see it in the list of available triggers for you LED:

root@gimli:~# cat /sys/class/leds/input4\:\:capslock/trigger  |grep timer
[none] usb-gadget usb-host rfkill-any rfkill-none kbd-scrolllock kbd-numlock kbd-capslock kbd-kanalock kbd-shiftlock kbd-altgrlock kbd-ctrllock kbd-altlock kbd-shiftllock kbd-shiftrlock kbd-ctrlllock kbd-ctrlrlock timer

We'll use your CapsLock LED in this example. So let's first turn that LED on. ON or OFF can mean 1 and 0 but may also be 255 and 0. It all depends on the driver, see max_brigtness for the maximum (full brightness) for each LED.

root@gimli:~# echo 1 > /sys/class/leds/input4\:\:capslock/brightness 

It should now be ON. Time to try blinking, set the timer trigger:

root@gimli:~# echo "timer" > /sys/class/leds/input4\:\:capslock/trigger 

The default is to blink at 1 Hz, off 500 ms, then on 500 ms. This can be adjusted with delay_off and delay_on, which appear when you set the timer trigger:

root@gimli:~# ls /sys/class/leds/input4\:\:capslock/
brightness      delay_off       delay_on        device/         max_brightness  power/          subsystem/      trigger         uevent          

Let's blink it quicker!

root@gimli:~# echo "100" > /sys/class/leds/input4\:\:capslock/delay_off
root@gimli:~# echo "100" > /sys/class/leds/input4\:\:capslock/delay_on

wow that's quick!

Turn off blinking, and disable the timer trigger, by:

root@gimli:~# echo 0 > /sys/class/leds/input4\:\:capslock/brightness 

Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment